Download Jersey 2.1 User Guide

Transcript
Server-Sent Events (SSE) Support
way and passed to the broadcaster. The broadcaster internally invokes write(OutboundEvent)
on all registered EventOutputs. After that the method just return a standard text response to the
POSTing client to inform the client that the message was successfully broadcast. As you can see, the
broadcastMessage(String) resource method is just a simple JAX-RS resource method.
In order to implement such a scenario, you may have noticed, that the Jersey SseBroadcaster is
not mandatory to complete the use case. individual EventOutput [http://jersey.java.net/nonav/apidocs/
snapshot/jersey/org/glassfish/jersey/media/sse/EventOutput.html]s can be just stored in a collection and
iterated over in the broadcastMessage method. However, the SseBroadcaster internally
identifies and handles also client disconnects. When a client closes the connection the broadcaster detects
this and removes the stale connection from the internal collection of the registered EventOutput [http://
jersey.java.net/nonav/apidocs/snapshot/jersey/org/glassfish/jersey/media/sse/EventOutput.html]s as well
as it frees all the server-side resources associated with the stale connection. Additionally, the
SseBroadcaster is implemented to be thread-safe, so that clients can connect and disconnect in any
time and SseBroadcaster will always broadcast messages to the most recent collection of registered
and active set of clients.
13.5. Consuming SSE events with Jersey
clients
On the client side, Jersey exposes APIs that support receiving and processing SSE events using two
programming models:
Pull model - pulling events from a EventInput [http://jersey.java.net/nonav/apidocs/snapshot/jersey/org/
glassfish/jersey/media/sse/EventInput.html], or
Push model - listening for asynchronous notifications of EventSource
Both models will be described.
13.5.1. Reading SSE events with EventInput
The events can be read on the client side from a EventInput [http://jersey.java.net/nonav/apidocs/snapshot/
jersey/org/glassfish/jersey/media/sse/EventInput.html]. See the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Client client = ClientBuilder.newBuilder()
.register(SseFeature.class).build();
WebTarget target = client.target("http://localhost:9998/events");
EventInput eventInput = target.request().get(EventInput.class);
while (!eventInput.isClosed()) {
final InboundEvent inboundEvent = eventInput.read();
if (inboundEvent == null) {
// connection has been closed
break;
}
System.out.println(inboundEvent.getName() + "; "
+ inboundEvent.getData(String.class));
}
In this example, a client connects to the server where the SseResource from the Example 13.1,
“Simple SSE resource method” is deployed. At first, a new JAX-RS/Jersey client instance is created
with a SseFeature registered. Then a WebTarget [http://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/
ws/rs/client/WebTarget.html] instance is retrieved from the client and is used to invoke a HTTP
141