类 ResponseBodyEmitter
- 直接已知子类:
SseEmitter
While 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.
@GET("/stream")
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();
- 从以下版本开始:
- 4.0 2022/4/8 23:54
- 作者:
- Rossen Stoyanchev, Juergen Hoeller, Harry Yang
-
嵌套类概要
嵌套类修饰符和类型类说明static classA simple holder of data to be written along with a MediaType hint for selecting a message converter to write with.private classprivate class(专用程序包) static interfaceContract 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). -
字段概要
字段修饰符和类型字段说明private booleanStore successful completion before the handler is initialized.private final ResponseBodyEmitter.DefaultCallbackprivate final ArrayList<ResponseBodyEmitter.DataWithMediaType>Store send data before handler is initialized.private final ResponseBodyEmitter.ErrorCallbackprivate ThrowableStore an error before the handler is initialized.private ResponseBodyEmitter.Handlerprivate booleanAfter an I/O error, we don't callcompleteWithError(java.lang.Throwable)directly but wait for the Servlet container to call us viaAsyncListener#onErroron a container thread at which point we call completeWithError.private final Longprivate final ResponseBodyEmitter.DefaultCallback -
构造器概要
构造器构造器说明Create a new ResponseBodyEmitter instance.ResponseBodyEmitter(Long timeout) Create a ResponseBodyEmitter with a custom timeout value. -
方法概要
修饰符和类型方法说明voidcomplete()Complete request processing by performing a dispatch into the servlet container, where Web MVC is invoked once more, and completes the request processing lifecycle.voidComplete request processing with an error.protected voidextendResponse(RequestContext 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.Return the configured timeout value, if any.(专用程序包) voidinitialize(ResponseBodyEmitter.Handler handler) (专用程序包) voidvoidonCompletion(Runnable callback) Register code to invoke when the async request completes.voidRegister code to invoke for an error during async request processing.voidRegister code to invoke when the async request times out.voidWrite the given object to the response.voidOverloaded variant ofsend(Object)that also accepts a MediaType hint for how to serialize the given Object.voidWrite a set of data and MediaType pairs in a batch.private voidtoString()
-
字段详细资料
-
timeout
-
handler
-
earlySendAttempts
Store send data before handler is initialized. -
complete
private boolean completeStore successful completion before the handler is initialized. -
failure
Store an error before the handler is initialized. -
sendFailed
private boolean sendFailedAfter an I/O error, we don't callcompleteWithError(java.lang.Throwable)directly but wait for the Servlet container to call us viaAsyncListener#onErroron a container thread at which point we call completeWithError. This flag is used to ignore further calls to complete or completeWithError that may come for example from an application try-catch block on the thread of the I/O error. -
timeoutCallback
-
errorCallback
-
completionCallback
-
-
构造器详细资料
-
ResponseBodyEmitter
public ResponseBodyEmitter()Create a new ResponseBodyEmitter instance. -
ResponseBodyEmitter
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.
- 参数:
timeout- the timeout value in milliseconds
-
-
方法详细资料
-
getTimeout
Return the configured timeout value, if any. -
initialize
- 抛出:
IOException
-
initializeWithError
-
extendResponse
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.
-
send
Write the given object to the response.If any exception occurs a dispatch is made back to the app server where Web MVC will pass the exception through its exception handling mechanism.
Note: if the send fails with an IOException, you do not need to call
completeWithError(Throwable)in order to clean up. Instead the Servlet container creates a notification that results in a dispatch where Web MVC invokes exception resolvers and completes processing.- 参数:
object- the object to write- 抛出:
IOException- raised when an I/O error occursIllegalStateException- wraps any other errors
-
send
Overloaded variant ofsend(Object)that also accepts a MediaType hint for how to serialize the given Object.- 参数:
object- the object to writemediaType- a MediaType hint for selecting an HttpMessageConverter- 抛出:
IOException- raised when an I/O error occursIllegalStateException- wraps any other errors
-
send
Write a set of data and MediaType pairs in a batch.Compared to
send(Object, MediaType), this batches the write operations and flushes to the network at the end.- 参数:
items- the object and media type pairs to write- 抛出:
IOException- raised when an I/O error occursIllegalStateException- wraps any other errors
-
sendInternal
private void sendInternal(Collection<ResponseBodyEmitter.DataWithMediaType> items) throws IOException - 抛出:
IOException
-
complete
public void complete()Complete request processing by performing a dispatch into the servlet container, where Web 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
sending. -
completeWithError
Complete request processing with an error.A dispatch is made into the app server where Web 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
sending. -
onTimeout
Register code to invoke when the async request times out. This method is called from a container thread when an async request times out. -
onError
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. -
onCompletion
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 aResponseBodyEmitterinstance is no longer usable. -
toString
-