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

SseEmitter

open class SseEmitter : ResponseBodyEmitter

A specialization of ResponseBodyEmitter for sending Server-Sent Events.

Author
Rossen Stoyanchev

Author
Juergen Hoeller

Since
4.2

Constructors

<init>

SseEmitter()

Create a new SseEmitter instance.

SseEmitter(timeout: Long)

Create a SseEmitter 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

event

open static fun event(): SseEventBuilder

send

open fun send(object: Any): Unit

Send the object formatted as a single SSE "data" line. It's equivalent to:

 // static import of SseEmitter.* SseEmitter emitter = new SseEmitter(); emitter.send(event().data(myObject)); 

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

Send the object formatted as a single SSE "data" line. It's equivalent to:

 // static import of SseEmitter.* SseEmitter emitter = new SseEmitter(); emitter.send(event().data(myObject, MediaType.APPLICATION_JSON)); 

open fun send(builder: SseEventBuilder): Unit

Send an SSE event prepared with the given builder. For example:

 // static import of SseEmitter SseEmitter emitter = new SseEmitter(); emitter.send(event().name("update").id("1").data(myObject)); 

toString

open fun toString(): String

Inherited 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.