javax.ws.rs.sse.Sse Java Examples

The following examples show how to use javax.ws.rs.sse.Sse. 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: PartyQueue.java    From liberty-bikes with Eclipse Public License 1.0 7 votes vote down vote up
public void add(String playerId, SseEventSink sink, Sse sse) {
    QueuedClient client = new QueuedClient(playerId, sink, sse);
    // If this client was already in the queue, remove them and add them at the end
    if (waitingPlayers.removeFirstOccurrence(client)) {
        party.log("Removed client " + playerId + " from queue before adding at end");
        GameMetrics.counterDec(GameMetrics.currentQueuedPlayersCounter);
    }
    party.log("Adding client " + playerId + " into the queue in position " + client.queuePosition());
    waitingPlayers.add(client);

    GameMetrics.counterInc(GameMetrics.currentQueuedPlayersCounter);

    if (party.getCurrentRound().isOpen())
        promoteClients();
    else
        client.notifyPosition();
}
 
Example #2
Source File: AsynchronousResources.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
@Produces(SERVER_SENT_EVENTS)
@Path("/sse/stream")
@GET
public void getSseStream(@Context final SseEventSink eventSink,
                         @Context final Sse sse) {
    scheduleSseEventSend(new SseEmitter() {
        @Override
        public CompletionStage<?> emit(final OutboundSseEvent event) {
            return eventSink.send(event);
        }

        @Override
        public void close() {
            eventSink.close();
        }
    }, sse, Refs.of(0), ctx.executionContext().executor());
}
 
Example #3
Source File: AsynchronousResources.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
@Produces(SERVER_SENT_EVENTS)
@Path("/sse/broadcast")
@GET
public void getSseBroadcast(@Context final SseEventSink eventSink,
                            @Context final Sse sse) {
    eventSink.send(sse.newEvent("bar"));
    final SseBroadcaster sseBroadcaster = sse.newBroadcaster();
    sseBroadcaster.register(eventSink);

    scheduleSseEventSend(new SseEmitter() {
        @Override
        public CompletionStage<?> emit(final OutboundSseEvent event) {
            return sseBroadcaster.broadcast(event);
        }

        @Override
        public void close() {
            sseBroadcaster.close();
        }
    }, sse, Refs.of(0), ctx.executionContext().executor());
}
 
Example #4
Source File: OutboundSseEventImplTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
/**
 * A user should not need to specify a media type when creating an outbound event. The default
 * should be <code>MediaType.SERVER_SENT_EVENTS_TYPE</code>.
 */
@Test
public void testDefaultMediaType() {
    Sse sse = new SseImpl();

    // test newEvent(data)
    OutboundSseEvent event = sse.newEvent("myData");
    assertNull(event.getName());
    assertEquals("myData", event.getData());
    assertEquals(MediaType.TEXT_PLAIN_TYPE, event.getMediaType());

    // test newEvent(name, data)
    event = sse.newEvent("myName", "myData2");
    assertEquals("myName", event.getName());
    assertEquals("myData2", event.getData());
    assertEquals(MediaType.TEXT_PLAIN_TYPE, event.getMediaType());

    // test newEventBuilder()...build()
    event = sse.newEventBuilder().comment("myComment").data("myData3").build();
    assertEquals("myComment", event.getComment());
    assertEquals("myData3", event.getData());
    assertEquals(MediaType.TEXT_PLAIN_TYPE, event.getMediaType());
}
 
Example #5
Source File: RestSBControllerImplTest.java    From onos with Apache License 2.0 6 votes vote down vote up
@GET
@Path("server-sent-events")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void getServerSentEvents(@Context SseEventSink eventSink, @Context Sse sse) throws InterruptedException {
    new Thread(() -> {
        try {
            for (int i = 0; i < 10; i++) {
                // ... code that waits 0.1 second
                    Thread.sleep(100L);
                final OutboundSseEvent event = sse.newEventBuilder()
                        .id(String.valueOf(i))
                        .name("message-to-rest-sb")
                        .data(String.class, "Test message " + i + "!")
                        .build();
                eventSink.send(event);
                System.out.println("Message " + i + " sent");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }).start();
}
 
Example #6
Source File: ServerSentEventsResource.java    From Java-EE-8-Sampler with MIT License 6 votes vote down vote up
@POST
@Path("progress/{report_id}")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void eventStream(@PathParam("report_id") final String id,
                        @Context SseEventSink es,
                        @Context Sse sse) {
    executorService.execute(() -> {
        try {
            eventSink.send(sse.newEventBuilder().name("report-progress")
                    .data(String.class, "Commencing process for report " + id + " ...").build());
            es.send(sse.newEvent("Progress", "25%"));
            Thread.sleep(500);
            es.send(sse.newEvent("Progress", "50%"));
            Thread.sleep(500);
            es.send(sse.newEvent("Progress", "75%"));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
}
 
Example #7
Source File: CancellableResources.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
private void sendSseUntilFailure(final SseEventSink eventSink, final Sse sse, final Executor executor) {
    try {
        eventSink.send(sse.newEvent("foo"));
        executor.schedule(() -> sendSseUntilFailure(eventSink, sse, executor), 10, MILLISECONDS);
    } catch (final Throwable t) {
        if (eventSink.isClosed()) {
            sseSinkClosedLatch.countDown();
        } else {
            throw new IllegalStateException("SseEventSink should be closed", t);
        }
    }
}
 
Example #8
Source File: StatsRestServiceImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Context 
public void setSse(Sse sse) {
    this.broadcaster = sse.newBroadcaster();
    this.builder = sse.newEventBuilder();
    
    Flowable
        .interval(500, TimeUnit.MILLISECONDS)
        .zipWith(
            Flowable.generate((Emitter<OutboundSseEvent.Builder> emitter) -> emitter.onNext(builder.name("stats"))),
            (id, bldr) -> createStatsEvent(bldr, id)
        )
        .subscribeOn(Schedulers.single())
        .subscribe(broadcaster::broadcast);
}
 
Example #9
Source File: SitemapResource.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void onEvent(SitemapEvent event) {
    final Sse sse = this.sse;
    if (sse == null) {
        logger.trace("broadcast skipped (no one listened since activation)");
        return;
    }

    final OutboundSseEvent outboundSseEvent = sse.newEventBuilder().name("event")
            .mediaType(MediaType.APPLICATION_JSON_TYPE).data(event).build();
    broadcaster.sendIf(outboundSseEvent, info -> {
        String sitemapName = event.sitemapName;
        String pageId = event.pageId;
        if (sitemapName != null && sitemapName.equals(subscriptions.getSitemapName(info.subscriptionId))
                && pageId != null && pageId.equals(subscriptions.getPageId(info.subscriptionId))) {
            if (logger.isDebugEnabled()) {
                if (event instanceof SitemapWidgetEvent) {
                    logger.debug("Sent sitemap event for widget {} to subscription {}.",
                            ((SitemapWidgetEvent) event).widgetId, info.subscriptionId);
                } else if (event instanceof ServerAliveEvent) {
                    logger.debug("Sent alive event to subscription {}.", info.subscriptionId);
                }
            }
            return true;
        }
        return false;
    });
}
 
Example #10
Source File: NewsService.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
@GET
@Path("/update")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void newsUpdate(@Context SseEventSink eventSink, @Context Sse sse) {
	CompletableFuture.runAsync(() -> {
		IntStream.range(1, 6).forEach(c -> {
			JsonObject newsEvent = Json.createObjectBuilder().add("news", String.format("Updated Event %d", newsCounter.incrementAndGet())).build();
			eventSink.send(sse.newEventBuilder().mediaType(MediaType.APPLICATION_JSON_TYPE).data(newsEvent).build());
		});
		//closing only on the client is generating a chunked connection exception that can be troubleshooted at a later date.
		eventSink.close();
	});
}
 
Example #11
Source File: SseResource.java    From ee8-sandbox with Apache License 2.0 5 votes vote down vote up
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
public void eventStream(@Context Sse sse, @Context SseEventSink eventSink) {
    // Resource method is invoked when a client subscribes to an event stream.
    // That implies that sending events will most likely happen from different
    // context - thread / event handler / etc, so common implementation of the
    // resource method will store the eventSink instance and the application 
    // logic will retrieve it when an event should be emitted to the client.

    // sending events:
    eventSink.send(sse.newEvent("event1"));
}
 
Example #12
Source File: OutboundSseEventImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * A user should not need to specify the type of data being sent in an outbound
 * event. In that case the OutboundSseEvent should use the data object's type. Other
 * types may be specified, but the default (if not specified by the user) should be
 * the return value from the object's <code>getClass()</code> method.
 */
@Test
public void testDefaultClass() {
    Sse sse = new SseImpl();

    // test newEvent(string)
    OutboundSseEvent event = sse.newEvent("myData");
    assertNull(event.getName());
    assertEquals("myData", event.getData());
    assertEquals(String.class, event.getType());

    // test newEvent(name, data)
    event = sse.newEvent("myName", "myData2");
    assertEquals("myName", event.getName());
    assertEquals("myData2", event.getData());
    assertEquals(String.class, event.getType());

    // test newEventBuilder()...build()
    event = sse.newEventBuilder().comment("myComment").data("myData3").build();
    assertEquals("myComment", event.getComment());
    assertEquals("myData3", event.getData());
    assertEquals(String.class, event.getType());

    // test that object's class is re-enabled when calling different signatures of the data method
    OutboundSseEvent.Builder builder = sse.newEventBuilder();
    builder.data(TestData.class, new TestDataImpl("1", "2"));
    event = builder.build();
    assertEquals(TestData.class, event.getType());
    builder.data("myString");
    event = builder.build();
    assertEquals(String.class, event.getType());

    // same thing, but don't build in between calls to data
    event = sse.newEventBuilder().data(TestDataImpl.class, new TestDataImpl("3")).data("anotherString").build();
    assertEquals(String.class, event.getType());
    assertEquals("anotherString", event.getData());
}
 
Example #13
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 #14
Source File: PartyService.java    From liberty-bikes with Eclipse Public License 1.0 5 votes vote down vote up
@GET
@Path("/{partyId}/queue")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void joinQueue(@PathParam("partyId") String partyId,
                      @QueryParam("playerId") String playerId,
                      @Context SseEventSink sink, @Context Sse sse) {
    Objects.requireNonNull(playerId, "Client attemted to queue for a party without providing playerId");
    Party p = getParty(partyId);
    if (p != null)
        p.enqueueClient(playerId, sink, sse);
}
 
Example #15
Source File: ServerSentService.java    From javaee8-cookbook with Apache License 2.0 5 votes vote down vote up
@Path("start")
@POST
public Response start(@Context Sse sse) {

    final UserEvent process = new UserEvent(sse);

    POOL.put(process.getId(), process);
    executor.submit(process);

    final URI uri = UriBuilder.fromResource(ServerSentService.class).path("register/{id}").build(process.getId());
    return Response.created(uri).build();
}
 
Example #16
Source File: SseResource.java    From javaee8-cookbook with Apache License 2.0 5 votes vote down vote up
@POST
public void addMessage(final String message, @Context Sse sse) throws IOException {
    if (SINK != null) {
        SINK.send(sse.newEventBuilder()
                .name("sse-message")
                .id(String.valueOf(System.currentTimeMillis()))
                .data(String.class, message)
                .comment("")
                .build());
    }
}
 
Example #17
Source File: CancellableResources.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Produces(SERVER_SENT_EVENTS)
@Path("/sse")
@GET
public void getSseStream(@Context final SseEventSink eventSink,
                         @Context final Sse sse,
                         @Context final ConnectionContext ctx) {
    sendSseUntilFailure(eventSink, sse, ctx.executionContext().executor());
}
 
Example #18
Source File: IssueUpdatesSSE.java    From Java-EE-8-and-Angular with MIT License 5 votes vote down vote up
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
public void subscribe(@Context Sse sse, @Context SseEventSink eventSink) {
    this.sse = sse;
    if(this.broadcaster == null) {
        this.broadcaster = sse.newBroadcaster();
    }
    this.broadcaster.register(eventSink);
}
 
Example #19
Source File: TaskUpdatesSSE.java    From Java-EE-8-and-Angular with MIT License 5 votes vote down vote up
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
public void subscribe(@Context Sse sse, @Context SseEventSink eventSink) {
    this.sse = sse;
    if(this.broadcaster == null) {
        this.broadcaster = sse.newBroadcaster();
    }
    this.broadcaster.register(eventSink);
}
 
Example #20
Source File: EchoHeaders.java    From tutorials with MIT License 5 votes vote down vote up
@GET
@Path("/events")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void getServerSentEvents(@Context SseEventSink eventSink, @Context Sse sse) {
    OutboundSseEvent event = sse.newEventBuilder()
            .name("echo-headers")
            .data(String.class, headers.getHeaderString(AddHeaderOnRequestFilter.FILTER_HEADER_KEY))
            .build();
    eventSink.send(event);
}
 
Example #21
Source File: AsynchronousResources.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Produces(SERVER_SENT_EVENTS)
@Path("/sse/unsupported")
@GET
public void getSseUnsupportedType(@Context final SseEventSink eventSink,
                                  @Context final Sse sse) {
    eventSink.send(sse.newEventBuilder()
            .data(Buffer.class, ctx.executionContext().bufferAllocator().fromAscii("foo"))
            .mediaType(MediaType.TEXT_PLAIN_TYPE)
            .build());
}
 
Example #22
Source File: AsynchronousResources.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
private void scheduleSseEventSend(final SseEmitter emmitter, final Sse sse, final Ref<Integer> iRef,
                                  final Executor executor) {
    executor.schedule(() -> {
        final int i = iRef.get();
        emmitter.emit(sse.newEvent("foo" + i)).whenComplete((r, t) -> {
            if (t == null && i < 9) {
                iRef.set(i + 1);
                scheduleSseEventSend(emmitter, sse, iRef, executor);
            } else {
                emmitter.close();
            }
        });
    }, 10, MILLISECONDS);
}
 
Example #23
Source File: SseContextProvider.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public Sse createContext(Message message) {
    return new SseImpl();
}
 
Example #24
Source File: StatsRestServiceImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Context 
public void setSse(Sse sse) {
    this.sse = sse;
}
 
Example #25
Source File: BookStore.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Context 
public void setSse(Sse sse) {
    this.sse = sse;
    this.broadcaster = sse.newBroadcaster();
}
 
Example #26
Source File: BookStore.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
protected Sse getSse() {
    return sse;
}
 
Example #27
Source File: SseResource.java    From tutorials with MIT License 4 votes vote down vote up
@Context
public void setSse(Sse sse) {
    this.sse = sse;
    this.eventBuilder = sse.newEventBuilder();
    this.sseBroadcaster = sse.newBroadcaster();
}
 
Example #28
Source File: BookStore2.java    From cxf with Apache License 2.0 4 votes vote down vote up
public BookStore2(@Context Sse sse) {
    this.sse = sse;
    this.broadcaster = sse.newBroadcaster();
}
 
Example #29
Source File: BookStore2.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
protected Sse getSse() {
    return sse;
}
 
Example #30
Source File: OutboundSseEventImplTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * Ensure that the <code>SseImpl</code> returns the correct builder class,
 * <code>OutboundSseEventImpl.BuilderImpl.class</code>.
 */
@Test
public void testSseImplReturnsExpectedOutboundSseEventBuilder() {
    Sse sse = new SseImpl();
    assertEquals(sse.newEventBuilder().getClass(), OutboundSseEventImpl.BuilderImpl.class);
}