use of org.glassfish.jersey.media.sse.EventOutput in project jersey by jersey.
the class ServerSentEventsResource method startDomain.
@POST
@Path("domains/{id}")
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput startDomain(@PathParam("id") final String id) {
final EventOutput seq = new EventOutput();
new Thread() {
public void run() {
try {
seq.write(new OutboundEvent.Builder().name("domain-progress").data(String.class, "starting domain " + id + " ...").build());
Thread.sleep(200);
seq.write(new OutboundEvent.Builder().name("domain-progress").data(String.class, "50%").build());
Thread.sleep(200);
seq.write(new OutboundEvent.Builder().name("domain-progress").data(String.class, "60%").build());
Thread.sleep(200);
seq.write(new OutboundEvent.Builder().name("domain-progress").data(String.class, "70%").build());
Thread.sleep(200);
seq.write(new OutboundEvent.Builder().name("domain-progress").data(String.class, "99%").build());
Thread.sleep(200);
seq.write(new OutboundEvent.Builder().name("domain-progress").data(String.class, "done").build());
seq.close();
} catch (final InterruptedException | IOException e) {
e.printStackTrace();
}
}
}.start();
return seq;
}
use of org.glassfish.jersey.media.sse.EventOutput in project jersey by jersey.
the class ItemStoreResource method itemEvents.
/**
* Connect or re-connect to SSE event stream.
*
* @param lastEventId Value of custom SSE HTTP <tt>{@value SseFeature#LAST_EVENT_ID_HEADER}</tt> header.
* Defaults to {@code -1} if not set.
* @return new SSE event output stream representing the (re-)established SSE client connection.
* @throws InternalServerErrorException in case replaying missed events to the reconnected output stream fails.
* @throws ServiceUnavailableException in case the reconnect delay is set to a positive value.
*/
@GET
@Path("events")
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput itemEvents(@HeaderParam(SseFeature.LAST_EVENT_ID_HEADER) @DefaultValue("-1") int lastEventId) {
final EventOutput eventOutput = new EventOutput();
if (lastEventId >= 0) {
LOGGER.info("Received last event id :" + lastEventId);
// decide the reconnect handling strategy based on current reconnect delay value.
final long delay = reconnectDelay;
if (delay > 0) {
LOGGER.info("Non-zero reconnect delay [" + delay + "] - responding with HTTP 503.");
throw new ServiceUnavailableException(delay);
} else {
LOGGER.info("Zero reconnect delay - reconnecting.");
replayMissedEvents(lastEventId, eventOutput);
}
}
if (!broadcaster.add(eventOutput)) {
LOGGER.severe("!!! Unable to add new event output to the broadcaster !!!");
// let's try to force a 5s delayed client reconnect attempt
throw new ServiceUnavailableException(5L);
}
return eventOutput;
}
use of org.glassfish.jersey.media.sse.EventOutput in project jersey by jersey.
the class ItemStoreResource method itemEvents.
/**
* Connect or re-connect to SSE event stream.
*
* @param lastEventId Value of custom SSE HTTP <tt>{@value SseFeature#LAST_EVENT_ID_HEADER}</tt> header.
* Defaults to {@code -1} if not set.
* @return new SSE event output stream representing the (re-)established SSE client connection.
* @throws InternalServerErrorException in case replaying missed events to the reconnected output stream fails.
* @throws ServiceUnavailableException in case the reconnect delay is set to a positive value.
*/
@GET
@Path("events")
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput itemEvents(@HeaderParam(SseFeature.LAST_EVENT_ID_HEADER) @DefaultValue("-1") int lastEventId) {
final EventOutput eventOutput = new EventOutput();
if (lastEventId >= 0) {
LOGGER.info("Received last event id :" + lastEventId);
// decide the reconnect handling strategy based on current reconnect delay value.
final long delay = reconnectDelay;
if (delay > 0) {
LOGGER.info("Non-zero reconnect delay [" + delay + "] - responding with HTTP 503.");
throw new ServiceUnavailableException(delay);
} else {
LOGGER.info("Zero reconnect delay - reconnecting.");
replayMissedEvents(lastEventId, eventOutput);
}
}
if (!broadcaster.add(eventOutput)) {
LOGGER.severe("!!! Unable to add new event output to the broadcaster !!!");
// let's try to force a 5s delayed client reconnect attempt
throw new ServiceUnavailableException(5L);
}
return eventOutput;
}
Aggregations