While org.
is used to produce a single result, a ResponseBodyEmitter can be used
to send multiple objects where each object is written with a compatible
org..
Supported as a return type on its own as well as within a
org..
@RequestMapping(value="/stream", method=RequestMethod.GET)
public ResponseBodyEmitter handle() {
ResponseBodyEmitter emitter = new ResponseBodyEmitter();
// Pass the emitter to another component...
return emitter;
}
// in another thread
emitter.send(foo1);
// and again
emitter.send(foo2);
// and done
emitter.complete();
| Modifier and Type | Class and Description |
|---|---|
| public static class | ResponseBodyEmitter.
A simple holder of data to be written along with a MediaType hint for selecting a message converter to write with. |
| private class | |
| private class | |
| pack-priv static interface | ResponseBodyEmitter.
Contract to handle the sending of event data, the completion of event sending, and the registration of callbacks to be invoked in case of timeout, error, and completion for any reason (including from the container side). |
| Modifier and Type | Field and Description |
|---|---|
| private boolean | complete
Store successful completion before the handler is initialized. |
| private final ResponseBodyEmitter. | |
| private final Set | earlySendAttempts
Store send data before handler is initialized. |
| private final ResponseBodyEmitter. | |
| private Throwable | failure
Store an error before the handler is initialized. |
| private ResponseBodyEmitter. | |
| private boolean | ioErrorOnSend
After an I/O error, we don't call |
| private final Long | |
| private final ResponseBodyEmitter. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | ResponseBodyEmitter(Long
the timeout value in milliseconds timeout)Create a ResponseBodyEmitter with a custom timeout value. |
| Modifier and Type | Method and Description |
|---|---|
| public synchronized void | complete()
Complete request processing by performing a dispatch into the servlet container, where Spring MVC is invoked once more, and completes the request processing lifecycle. |
| public synchronized void | |
| protected void | extendResponse(ServerHttpResponse outputMessage)
Invoked after the response is updated with the status code and headers, if the ResponseBodyEmitter is wrapped in a ResponseEntity, but before the response is committed, i.e. before the response body has been written to. |
| public Long | |
| pack-priv synchronized void | |
| pack-priv synchronized void | |
| public synchronized void | |
| public synchronized void | |
| public synchronized void | |
| public void | |
| public synchronized void | send(Object
the object to write object, MediaType a MediaType hint for selecting an HttpMessageConverter mediaType)Overloaded variant of |
| public synchronized void | send(Set<ResponseBodyEmitter.
the object and media type pairs to write itemsWrite a set of data and MediaType pairs in a batch. |
| private void | |
| public String |
| complete | back to summary |
|---|---|
| private boolean complete Store successful completion before the handler is initialized. | |
| completionCallback | back to summary |
|---|---|
| private final ResponseBodyEmitter. | |
| earlySendAttempts | back to summary |
|---|---|
| private final Set<ResponseBodyEmitter. Store send data before handler is initialized. | |
| errorCallback | back to summary |
|---|---|
| private final ResponseBodyEmitter. | |
| failure | back to summary |
|---|---|
| private Throwable failure Store an error before the handler is initialized. | |
| handler | back to summary |
|---|---|
| private ResponseBodyEmitter. | |
| ioErrorOnSend | back to summary |
|---|---|
| private boolean ioErrorOnSend After an I/O error, we don't call | |
| timeout | back to summary |
|---|---|
| private final Long timeout
| |
| timeoutCallback | back to summary |
|---|---|
| private final ResponseBodyEmitter. | |
| ResponseBodyEmitter | back to summary |
|---|---|
| public ResponseBodyEmitter() Create a new ResponseBodyEmitter instance. | |
| ResponseBodyEmitter | back to summary |
|---|---|
| public ResponseBodyEmitter(Long timeout) Create a ResponseBodyEmitter with a custom timeout value. By default not set in which case the default configured in the MVC Java Config or the MVC namespace is used, or if that's not set, then the timeout depends on the default of the underlying server.
| |
| complete | back to summary |
|---|---|
| public synchronized void complete() Complete request processing by performing a dispatch into the servlet container, where Spring MVC is invoked once more, and completes the request processing lifecycle. Note this method should be called by the application
to complete request processing. It should not be used after container
related events such as an error while | |
| completeWithError | back to summary |
|---|---|
| public synchronized void completeWithError(Throwable ex) Complete request processing with an error. A dispatch is made into the app server where Spring MVC will pass the exception through its exception handling mechanism. Note however that at this stage of request processing, the response is committed and the response status can no longer be changed. Note this method should be called by the application
to complete request processing with an error. It should not be used after
container related events such as an error while
| |
| extendResponse | back to summary |
|---|---|
| protected void extendResponse(ServerHttpResponse outputMessage) Invoked after the response is updated with the status code and headers, if the ResponseBodyEmitter is wrapped in a ResponseEntity, but before the response is committed, i.e. before the response body has been written to. The default implementation is empty. | |
| getTimeout | back to summary |
|---|---|
| public Long getTimeout() Return the configured timeout value, if any.
| |
| initialize | back to summary |
|---|---|
| pack-priv synchronized void initialize(ResponseBodyEmitter. | |
| initializeWithError | back to summary |
|---|---|
| pack-priv synchronized void initializeWithError(Throwable ex) | |
| onCompletion | back to summary |
|---|---|
| public synchronized void onCompletion(Runnable callback) Register code to invoke when the async request completes. This method is
called from a container thread when an async request completed for any
reason including timeout and network error. This method is useful for
detecting that a | |
| onError | back to summary |
|---|---|
| public synchronized void onError(Consumer<Throwable> callback) Register code to invoke for an error during async request processing. This method is called from a container thread when an error occurred while processing an async request.
| |
| onTimeout | back to summary |
|---|---|
| public synchronized void onTimeout(Runnable callback) Register code to invoke when the async request times out. This method is called from a container thread when an async request times out. | |
| send | back to summary |
|---|---|
| public void send(Object object) throws IOException Write the given object to the response. If any exception occurs a dispatch is made back to the app server where Spring MVC will pass the exception through its exception handling mechanism. Note if the send fails with an IOException, you do
not need to call
| |
| send | back to summary |
|---|---|
| public synchronized void send(Object object, MediaType mediaType) throws IOException Overloaded variant of
| |
| send | back to summary |
|---|---|
| public synchronized void send(Set<ResponseBodyEmitter. Write a set of data and MediaType pairs in a batch. Compared to
| |
| sendInternal | back to summary |
|---|---|
| private void sendInternal(Set<ResponseBodyEmitter. | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non- | |
| Access | Constructor and Description |
|---|---|
| public |
| data | back to summary |
|---|---|
| private final Object data | |
| mediaType | back to summary |
|---|---|
| private final MediaType mediaType
| |
| DataWithMediaType | back to summary |
|---|---|
| public DataWithMediaType(Object data, MediaType mediaType) | |
| getData | back to summary |
|---|---|
| public Object getData() | |
| getMediaType | back to summary |
|---|---|
public MediaType getMediaType()
| |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void |
| delegate | back to summary |
|---|---|
| private Runnable delegate
| |
| DefaultCallback | back to summary |
|---|---|
| private DefaultCallback() | |
| run | back to summary |
|---|---|
| public void run() Implements java. Doc from java. Runs this operation.
| |
| setDelegate | back to summary |
|---|---|
| public void setDelegate(Runnable delegate) | |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | accept(Throwable
the input argument t)Implements java. Performs this operation on the given argument. |
| public void |
| delegate | back to summary |
|---|---|
| private Consumer<Throwable> delegate
| |
| ErrorCallback | back to summary |
|---|---|
| private ErrorCallback() | |
| accept | back to summary |
|---|---|
| public void accept(Throwable t) Implements java. Doc from java. Performs this operation on the given argument. | |
| setDelegate | back to summary |
|---|---|
| public void setDelegate(Consumer<Throwable> callback) | |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | send(Set<ResponseBodyEmitter.
Immediately write all data items then flush to the network. |
| complete | back to summary |
|---|---|
| public void complete() | |
| completeWithError | back to summary |
|---|---|
| public void completeWithError(Throwable failure) | |
| onCompletion | back to summary |
|---|---|
| public void onCompletion(Runnable callback) | |
| onError | back to summary |
|---|---|
| public void onError(Consumer<Throwable> callback) | |
| onTimeout | back to summary |
|---|---|
| public void onTimeout(Runnable callback) | |
| send | back to summary |
|---|---|
| public void send(Object data, MediaType mediaType) throws IOException Immediately write and flush the given data to the network. | |
| send | back to summary |
|---|---|
| public void send(Set<ResponseBodyEmitter. Immediately write all data items then flush to the network.
| |