Interface SseEventSource
-
- All Superinterfaces:
AutoCloseable
public interface SseEventSource extends AutoCloseable
Client for reading and processingincoming 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
SseEventSourceis created, it can be used toopen a connectionto the associatedweb target. After establishing the connection, the event source starts processing any incoming inbound events. Whenever a new event is received, anConsumer<InboundSseEvent>#accept(InboundSseEvent)method is invoked on any registered event consumers.Reconnect support
The
SseEventSourcesupports automated recuperation from a connection loss, including negotiation of delivery of any missed events based on the last received SSE eventidfield 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 eventidfield 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
retryfield value in any sent event. JAX-RSSseEventSourcetracks any received SSE eventretryfield values set by the endpoint and adjusts the reconnect delay accordingly, using the last receivedretryfield value as the reconnect delay.In addition to handling the standard connection loss failures, JAX-RS
SseEventSourcebehaves differently to various HTTP response status codes and headers:- 200 - with
"Content-Type"header of "text/event-stream": This is normal operation.onEventis invoked for each event.onCompleteis invoked when there are no more events.onErroris invoked only if an unrecoverable error occurs during processing. - 200 - with unsupported or missing
"Content-Type"header: This is an error condition.onErroris invoked. - 204 - This indicates that server has no events to send. Only
onCompleteis invoked. - 503 - with
"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 inonCompleteinvocation). After the specified delay, the client should automatically attempt to reconnect which will result in a new response. - 503 - with invalid or missing
"Retry-After"header: This is an error condition.onErroris invoked. - Any other status code: This is an error condition.
onErroris invoked.
In the case of an error condition response, the
Throwablepassed to theonErrorconsumer should be a WebApplicationException containing the invalidResponseobject.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. ThusonErroris not invoked and event processing is not stopped. Users are encouraged to handle exceptions on their own as part of the event processing logic.- Since:
- 2.1
- Author:
- Marek Potociar
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classSseEventSource.BuilderJAX-RSSseEventSourcebuilder class.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default voidclose()Close this event source.booleanclose(long timeout, 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.booleanisOpen()Check if this event source instance has already beenopened.voidopen()Open the connection to the supplied SSE underlyingweb targetand start processing incomingevents.voidregister(Consumer<InboundSseEvent> onEvent)Register aInboundSseEventconsumer.voidregister(Consumer<InboundSseEvent> onEvent, Consumer<Throwable> onError)RegisterInboundSseEventandThrowableconsumers.voidregister(Consumer<InboundSseEvent> onEvent, Consumer<Throwable> onError, Runnable onComplete)RegisterInboundSseEventandThrowableconsumers and onComplete callback.static SseEventSource.Buildertarget(WebTarget endpoint)Create a newevent source builderthat provides convenient way how to configure and fine-tune various aspects of a newly prepared event source instance.
-
-
-
Method Detail
-
register
void register(Consumer<InboundSseEvent> onEvent)
Register aInboundSseEventconsumer.Given consumer is invoked once per each received event.
- Parameters:
onEvent- event consumer.- Throws:
IllegalArgumentException- when the provided parameter isnull.
-
register
void register(Consumer<InboundSseEvent> onEvent, Consumer<Throwable> onError)
RegisterInboundSseEventandThrowableconsumers.Event consumer is invoked once per each received event,
Throwableconsumer is invoked invoked upon a unrecoverable error encountered by aSseEventSource.- Parameters:
onEvent- event consumer.onError- error consumer.- Throws:
IllegalArgumentException- when the any of the provided parameters isnull.
-
register
void register(Consumer<InboundSseEvent> onEvent, Consumer<Throwable> onError, Runnable onComplete)
RegisterInboundSseEventandThrowableconsumers and onComplete callback.Event consumer is invoked once per each received event,
Throwableconsumer is invoked invoked upon a unrecoverable error encountered by aSseEventSource, onComplete callback is invoked after a successful connection and when there are no further events to be received. Note that the onComplete callback will not be invoked if the onError callback has been invoked.- Parameters:
onEvent- event consumer.onError- error consumer.onComplete- onComplete handler.- Throws:
IllegalArgumentException- when the any of the provided parameters isnull.
-
target
static SseEventSource.Builder target(WebTarget endpoint)
Create a newevent source builderthat provides convenient way how to configure and fine-tune various aspects of a newly prepared event source instance.- Parameters:
endpoint- SSE streaming endpoint. Must not benull.- Returns:
- a builder of a new event source instance pointing at the specified SSE streaming endpoint.
- Throws:
NullPointerException- in case the supplied web target isnull.
-
open
void open()
Open the connection to the supplied SSE underlyingweb targetand start processing incomingevents.- Throws:
IllegalStateException- in case the event source has already been opened earlier.
-
isOpen
boolean isOpen()
Check if this event source instance has already beenopened.- Returns:
trueif this event source is open,falseotherwise.
-
close
default void close()
Close this event source.The method will wait up to 5 seconds for the internal event processing tasks to complete.
- Specified by:
closein interfaceAutoCloseable
-
close
boolean close(long timeout, 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
interruptflag on the thread before returningfalse.- Parameters:
timeout- the maximum time to wait.unit- the time unit of the timeout argument.- Returns:
trueif this executor terminated andfalseif the timeout elapsed before termination or the termination was interrupted.
-
-