spring-framework / org.springframework.web.servlet.mvc.method.annotation / ResponseBodyEmitter

ResponseBodyEmitter

open class ResponseBodyEmitter

A controller method return value type for asynchronous request processing where one or more objects are written to the response.

While org.springframework.web.context.request.async.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 org.springframework.http.converter.HttpMessageConverter.

Supported as a return type on its own as well as within a org.springframework.http.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(); 

Author
Rossen Stoyanchev

Author
Juergen Hoeller

Since
4.2

Constructors

<init>

ResponseBodyEmitter()

Create a new ResponseBodyEmitter instance.

ResponseBodyEmitter(timeout: Long)

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.

Functions

complete

open fun complete(): Unit

Complete request processing.

A dispatch is made into the app server where Spring MVC completes asynchronous request processing.

completeWithError

open fun completeWithError(ex: Throwable): Unit

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.

getTimeout

open fun getTimeout(): Long

Return the configured timeout value, if any.

onCompletion

open fun onCompletion(callback: Runnable): Unit

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 ResponseBodyEmitter instance is no longer usable.

onError

open fun onError(callback: Consumer<Throwable>): Unit

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

open fun onTimeout(callback: Runnable): Unit

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

open fun send(object: Any): Unit

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.

open fun send(object: Any, mediaType: MediaType): Unit

Write the given object to the response also using a MediaType hint.

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.

toString

open fun toString(): String

Inheritors

SseEmitter

open class SseEmitter : ResponseBodyEmitter

A specialization of ResponseBodyEmitter for sending Server-Sent Events.