Search in sources :

Example 6 with EventOutput

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;
}
Also used : EventOutput(org.glassfish.jersey.media.sse.EventOutput) OutboundEvent(org.glassfish.jersey.media.sse.OutboundEvent) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 7 with 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;
}
Also used : EventOutput(org.glassfish.jersey.media.sse.EventOutput) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 8 with 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;
}
Also used : EventOutput(org.glassfish.jersey.media.sse.EventOutput) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

EventOutput (org.glassfish.jersey.media.sse.EventOutput)8 Produces (javax.ws.rs.Produces)7 GET (javax.ws.rs.GET)6 Path (javax.ws.rs.Path)6 ServiceUnavailableException (javax.ws.rs.ServiceUnavailableException)2 DELETE (javax.ws.rs.DELETE)1 NotFoundException (javax.ws.rs.NotFoundException)1 POST (javax.ws.rs.POST)1 OutboundEvent (org.glassfish.jersey.media.sse.OutboundEvent)1