public class ResponseBodyEmitter
extends java.lang.Object
DeferredResult
is used to produce a single result, a ResponseBodyEmitter
can be used
to send multiple objects where each object is written with a compatible
HttpMessageConverter
.
Supported as a return type on its own as well as within a
ResponseEntity
.
@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 |
---|---|
(package private) static class |
ResponseBodyEmitter.DataWithMediaType
Simple struct for a data entry.
|
(package private) static interface |
ResponseBodyEmitter.Handler
Handle sent objects and complete request processing.
|
Modifier and Type | Field and Description |
---|---|
private boolean |
complete |
private java.lang.Runnable |
completionCallback |
private java.util.Set<ResponseBodyEmitter.DataWithMediaType> |
earlySendAttempts |
private java.lang.Throwable |
failure |
private ResponseBodyEmitter.Handler |
handler |
private java.lang.Long |
timeout |
private java.lang.Runnable |
timeoutCallback |
Constructor and Description |
---|
ResponseBodyEmitter()
Create a new ResponseBodyEmitter instance.
|
ResponseBodyEmitter(java.lang.Long timeout)
Create a ResponseBodyEmitter with a custom timeout value.
|
Modifier and Type | Method and Description |
---|---|
void |
complete()
Complete request processing.
|
void |
completeWithError(java.lang.Throwable ex)
Complete request processing with an error.
|
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.
|
java.lang.Long |
getTimeout()
Return the configured timeout value, if any.
|
(package private) void |
initialize(ResponseBodyEmitter.Handler handler) |
void |
onCompletion(java.lang.Runnable callback)
Register code to invoke when the async request completes.
|
void |
onTimeout(java.lang.Runnable callback)
Register code to invoke when the async request times out.
|
void |
send(java.lang.Object object)
Write the given object to the response.
|
void |
send(java.lang.Object object,
MediaType mediaType)
Write the given object to the response also using a MediaType hint.
|
private void |
sendInternal(java.lang.Object object,
MediaType mediaType) |
private final java.lang.Long timeout
private final java.util.Set<ResponseBodyEmitter.DataWithMediaType> earlySendAttempts
private ResponseBodyEmitter.Handler handler
private boolean complete
private java.lang.Throwable failure
private java.lang.Runnable timeoutCallback
private java.lang.Runnable completionCallback
public ResponseBodyEmitter()
public ResponseBodyEmitter(java.lang.Long timeout)
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.
timeout
- timeout value in millisecondspublic java.lang.Long getTimeout()
protected void extendResponse(ServerHttpResponse outputMessage)
The default implementation is empty.
void initialize(ResponseBodyEmitter.Handler handler) throws java.io.IOException
java.io.IOException
public void send(java.lang.Object object) throws java.io.IOException
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.
object
- the object to writejava.io.IOException
- raised when an I/O error occursjava.lang.IllegalStateException
- wraps any other errorspublic void send(java.lang.Object object, MediaType mediaType) throws java.io.IOException
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.
object
- the object to writemediaType
- a MediaType hint for selecting an HttpMessageConverterjava.io.IOException
- raised when an I/O error occursjava.lang.IllegalStateException
- wraps any other errorsprivate void sendInternal(java.lang.Object object, MediaType mediaType) throws java.io.IOException
java.io.IOException
public void complete()
A dispatch is made into the app server where Spring MVC completes asynchronous request processing.
public void completeWithError(java.lang.Throwable ex)
A dispatch is made into the app server where Spring MVC will pass the exception through its exception handling mechanism.
public void onTimeout(java.lang.Runnable callback)
public void onCompletion(java.lang.Runnable callback)
ResponseBodyEmitter
instance is no longer usable.