Java Code Examples for javax.ws.rs.sse.SseEventSink#close()

The following examples show how to use javax.ws.rs.sse.SseEventSink#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServerSentEventResource.java    From quarkus with Apache License 2.0 8 votes vote down vote up
@GET
@Path("/stream-xml")
@SseElementType(MediaType.TEXT_XML)
@Produces(MediaType.SERVER_SENT_EVENTS)
public void sendXmlData(@Context SseEventSink sink) {
    // send a stream of few events
    try {
        for (int i = 0; i < 3; i++) {
            final OutboundSseEvent.Builder builder = this.sse.newEventBuilder();
            builder.id(String.valueOf(i)).mediaType(MediaType.TEXT_XML_TYPE)
                    .data("<settings><foo bar=\"" + i + "\"/></settings>")
                    .name("stream of XML data");

            sink.send(builder.build());
        }
    } finally {
        sink.close();
    }
}
 
Example 2
Source File: TestResourceForConstructorProperties.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/sse")
@SseElementType(MediaType.APPLICATION_JSON)
@Produces(MediaType.SERVER_SENT_EVENTS)
public void serverSentEvents(@Context SseEventSink sink) {
    VanillaJavaImmutableData data = new VanillaJavaImmutableData("sse", "ssevalue");
    try {
        OutboundSseEvent.Builder builder = sse.newEventBuilder();
        builder.id(String.valueOf(1))
                .mediaType(MediaType.APPLICATION_JSON_TYPE)
                .data(data)
                .name("stream of json data");

        sink.send(builder.build());
    } finally {
        sink.close();
    }
}
 
Example 3
Source File: ServerSentEventResource.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/stream")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void sendData(@Context SseEventSink sink) {
    // send a stream of few events
    try {
        for (int i = 0; i < 3; i++) {
            final OutboundSseEvent.Builder builder = this.sse.newEventBuilder();
            builder.id(String.valueOf(i)).mediaType(MediaType.TEXT_PLAIN_TYPE)
                    .data(Integer.class, i)
                    .name("stream of numbers");

            sink.send(builder.build());
        }
    } finally {
        sink.close();
    }
}
 
Example 4
Source File: ServerSentEventResource.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/stream-html")
@Produces(MediaType.SERVER_SENT_EVENTS)
@SseElementType("text/html")
public void sendHtmlData(@Context SseEventSink sink) {
    // send a stream of few events
    try {
        for (int i = 0; i < 3; i++) {
            final OutboundSseEvent.Builder builder = this.sse.newEventBuilder();
            builder.id(String.valueOf(i)).mediaType(MediaType.TEXT_HTML_TYPE)
                    .data("<html><body>" + i + "</body></html>")
                    .name("stream of pages");

            sink.send(builder.build());
        }
    } finally {
        sink.close();
    }
}
 
Example 5
Source File: SseBroadcaster.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
private void close(final SseEventSink sink) {
    try {
        sink.close();
    } catch (final RuntimeException ex) {
        logger.debug("Closing a SSE event sink failed. Nothing we can do here...", ex);
    }
}
 
Example 6
Source File: SseEndpoint.java    From hammock with Apache License 2.0 5 votes vote down vote up
@GET
@Path("/{uuid}")
@Produces(SERVER_SENT_EVENTS)
public void doSseCall(@PathParam("uuid") String uuid, @Context SseEventSink sink, @Context Sse sse) {
    final OutboundSseEvent.Builder builder = sse.newEventBuilder();
    OutboundSseEvent event = builder.id(uuid)
            .data(SseModel.class, new SseModel("some model "+uuid))
            .build();
    sink.send(event);
    sink.close();
}
 
Example 7
Source File: BookStore.java    From cxf with Apache License 2.0 4 votes vote down vote up
@GET
@Path("nodata")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void nodata(@Context SseEventSink sink) {
    sink.close();
}
 
Example 8
Source File: BookStore2.java    From cxf with Apache License 2.0 4 votes vote down vote up
@GET
@Path("nodata")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void nodata(@Context SseEventSink sink) {
    sink.close();
}
 
Example 9
Source File: SseResource.java    From tutorials with MIT License 4 votes vote down vote up
@GET
@Path("prices")
@Produces("text/event-stream")
public void getStockPrices(@Context SseEventSink sseEventSink,
                           @HeaderParam(HttpHeaders.LAST_EVENT_ID_HEADER) @DefaultValue("-1") int lastReceivedId) {

    int lastEventId = 1;
    if (lastReceivedId != -1) {
        lastEventId = ++lastReceivedId;
    }
    boolean running = true;
    while (running) {
        Stock stock = stockService.getNextTransaction(lastEventId);
        if (stock != null) {
            OutboundSseEvent sseEvent = this.eventBuilder
                    .name("stock")
                    .id(String.valueOf(lastEventId))
                    .mediaType(MediaType.APPLICATION_JSON_TYPE)
                    .data(Stock.class, stock)
                    .reconnectDelay(3000)
                    .comment("price change")
                    .build();
            sseEventSink.send(sseEvent);
            lastEventId++;
        }
        //Simulate connection close
        if (lastEventId % 5 == 0) {
            sseEventSink.close();
            break;
        }

        try {
            //Wait 5 seconds
            Thread.sleep(5 * 1000);
        } catch (InterruptedException ex) {
            // ...
        }
        //Simulatae a while boucle break
        running = lastEventId <= 2000;
    }
    sseEventSink.close();
}