incoming Server-Sent Events.
SSE event source instances of this class are thread safe. To build a new instance, you can use the
SseEventSource.target(endpoint) factory method to get a new event
source builder that can be further customised and eventually used to create a new SSE event source.
Once a SseEventSource is created, it can be used to open a connection to the associated
web target. After establishing the connection, the event source starts processing any incoming
inbound events. Whenever a new event is received, an Consumer<InboundSseEvent>#accept(InboundSseEvent) method is invoked on any registered event consumers.
The SseEventSource supports automated recuperation from a connection loss, including
negotiation of delivery of any missed events based on the last received SSE event id field value, provided
this field is set by the server and the negotiation facility is supported by the server. In case of a connection loss,
the last received SSE event id field value is sent in the
Last-Event-ID HTTP
request header as part of a new connection request sent to the SSE endpoint. Upon a receipt of such reconnect request, the SSE
endpoint that supports this negotiation facility is expected to replay all missed events. Note however, that this is a
best-effort mechanism which does not provide any guaranty that all events would be delivered without a loss. You should
therefore not rely on receiving every single event and design your client application code accordingly.
By default, when a connection to the SSE endpoint is lost, the event source will wait 500 ms
before attempting to reconnect to the SSE endpoint. The SSE endpoint can however control the client-side retry delay
by including a special retry field value in any sent event. JAX-RS SseEventSource tracks any
received SSE event retry field values set by the endpoint and adjusts the reconnect delay accordingly,
using the last received retry field value as the reconnect delay.
In addition to handling the standard connection loss failures, JAX-RS SseEventSource behaves differently to various
HTTP response status codes and headers:
Content-Type header of "text/event-stream": This is normal
operation. onEvent is invoked for each event. onComplete is invoked when there are no more
events. onError is invoked only if an unrecoverable error occurs during processing.Content-Type header: This is an
error condition. onError is invoked.onComplete is invoked.Retry-After header set to a valid value: This indicates
that the server is unavailable, but that the client should reconnect later. No consumers are invoked unless the client
event source is closed, prior to reconnecting (resulting in onComplete invocation). After the specified
delay, the client should automatically attempt to reconnect which will result in a new response.Retry-After header: This is an error
condition. onError is invoked.onError is invoked.
In the case of an error condition response, the Throwable passed to the onError consumer
should be a WebApplicationException containing the invalid Response object.
Note that if, for any of the registered event consumers, an invocation of Consumer<InboundSseEvent>#accept(InboundSseEvent) method throws an exception, this is not an error condition.
Thus onError is not invoked and event processing is not stopped.
Users are encouraged to handle exceptions on their own as part of the event processing logic.
| Modifier and Type | Class and Description |
|---|---|
| public abstract static class | SseEventSource.Builder
JAX-RS |
| Modifier and Type | Method and Description |
|---|---|
| public default void | |
| public boolean | Returns: true if this executor terminated and false if the timeout elapsed before termination or the
termination was interrupted.the maximum time to wait. timeout, final TimeUnit the time unit of the timeout argument. unit)Close this event source and wait for the internal event processing task to complete for up to the specified amount of wait time. |
| public boolean | |
| public void | open()
Open the connection to the supplied SSE underlying |
| public void | |
| public void | register(Consumer<InboundSseEvent>
event consumer. onEvent, Consumer<Throwable> error consumer. onError)Register |
| public void | register(Consumer<InboundSseEvent>
event consumer. onEvent, Consumer<Throwable> error consumer. onError, Runnable onComplete handler. onComplete)Register |
| public static SseEventSource. | Returns: a builder of a new event source instance pointing at the specified SSE streaming endpoint.SSE streaming endpoint. Must not be endpoint)null.Create a new |
| close | back to summary |
|---|---|
| public default void close() Implements java. Close this event source. The method will wait up to 5 seconds for the internal event processing tasks to complete.
| |
| close | back to summary |
|---|---|
| public boolean close(final long timeout, final TimeUnit unit) Close this event source and wait for the internal event processing task to complete for up to the specified amount of wait time. The method blocks until the event processing task has completed execution after a shutdown request, or until the timeout occurs, or the current thread is interrupted, whichever happens first.
In case the waiting for the event processing task has been interrupted, this method restores the
| |
| isOpen | back to summary |
|---|---|
| public boolean isOpen() Check if this event source instance has already been
| |
| open | back to summary |
|---|---|
| public void open() Open the connection to the supplied SSE underlying
| |
| register | back to summary |
|---|---|
| public void register(Consumer<InboundSseEvent> onEvent) Register a Given consumer is invoked once per each received event.
| |
| register | back to summary |
|---|---|
| public void register(Consumer<InboundSseEvent> onEvent, Consumer<Throwable> onError) Register
Event consumer is invoked once per each received event,
| |
| register | back to summary |
|---|---|
| public void register(Consumer<InboundSseEvent> onEvent, Consumer<Throwable> onError, Runnable onComplete) Register
Event consumer is invoked once per each received event,
| |
| target | back to summary |
|---|---|
| public static SseEventSource. Create a new
| |
SseEventSource builder class.
Event source builder provides methods that let you conveniently configure and subsequently build a new
SseEventSource instance. You can obtain a new event source builder instance using a static
SseEventSource.target(endpoint) factory method.
For example:
SseEventSource es = SseEventSource.target(endpoint)
.reconnectingEvery(5, SECONDS)
.build();
es.register(System.out::println);
es.open();
| Modifier and Type | Field and Description |
|---|---|
| public static final String | JAXRS_DEFAULT_SSE_BUILDER_PROPERTY
Name of the property identifying the |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public abstract SseEventSource | Returns: new event source instance, ready to be connected to the SSE endpoint.Build new SSE event source pointing at a SSE streaming |
| pack-priv static SseEventSource. | Returns: new SSE event source builder instance.Create a new SSE event source instance using the default implementation class provided by the JAX-RS implementation provider. |
| public abstract SseEventSource. | Returns: updated event source builder instance.the default time to wait before attempting to recover from a connection loss. delay, TimeUnit time unit of the reconnect delay parameter. unit)Set the initial reconnect delay to be used by the event source. |
| protected abstract SseEventSource. |
| JAXRS_DEFAULT_SSE_BUILDER_PROPERTY | back to summary |
|---|---|
| public static final String JAXRS_DEFAULT_SSE_BUILDER_PROPERTY Name of the property identifying the | |
| Builder | back to summary |
|---|---|
| protected Builder() Allows custom implementations to extend the SSE event source builder class. | |
| build | back to summary |
|---|---|
| public abstract SseEventSource build() Build new SSE event source pointing at a SSE streaming
The returned event source is ready, but not
Once the event source is open, the incoming events are processed by the event source in an asynchronous task that
runs in an internal single-threaded
| |
| newBuilder | back to summary |
|---|---|
| pack-priv static SseEventSource. Create a new SSE event source instance using the default implementation class provided by the JAX-RS implementation provider.
| |
| reconnectingEvery | back to summary |
|---|---|
| public abstract SseEventSource. Set the initial reconnect delay to be used by the event source.
Note that this value may be later overridden by the SSE endpoint using either a
| |
| target | back to summary |
|---|---|
| protected abstract SseEventSource. Set the SSE streaming endpoint.
| |