Java Code Examples for javax.ws.rs.core.MediaType.SERVER_SENT_EVENTS

The following are Jave code examples for showing how to use SERVER_SENT_EVENTS of the javax.ws.rs.core.MediaType class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Java-EE-8-Sampler   File: ServerSentEventsResource.java   Source Code and License Vote up 6 votes
@POST
@Path("domains/{id}")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void startDomain(@PathParam("id") final String id, @Context SseEventSink eventSink) {

    executorService.submit(() -> {
        try {
            eventSink.send(sse.newEventBuilder().name("domain-progress").data(String.class, "starting domain " + id + " ...").build());
            Thread.sleep(200);
            eventSink.send(sse.newEvent("domain-progress", "50%"));
            Thread.sleep(200);
            eventSink.send(sse.newEvent("domain-progress", "60%"));
            Thread.sleep(200);
            eventSink.send(sse.newEvent("domain-progress", "70%"));
            Thread.sleep(200);
            eventSink.send(sse.newEvent("domain-progress", "99%"));
            Thread.sleep(200);
            eventSink.send(sse.newEvent("domain-progress", "Done."));
            eventSink.close();
        } catch (final InterruptedException e) {
            e.printStackTrace();
        }
    });
}
 
Example 2
Project: Java-EE-8-Sampler   File: ServerSentEventsResource.java   Source Code and License Vote up 6 votes
@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 3
Project: redpipe   File: KafkaResource.java   Source Code and License Vote up 6 votes
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
public Observable<JsonObject> index(@Context AppGlobals globals){
	UUID uuid = UUID.randomUUID();
	Observable<JsonObject> consumer = (Observable<JsonObject>) globals.getGlobal("consumer");
    Observable<JsonObject> ret = consumer
        .buffer(1, TimeUnit.SECONDS)
        .map((List<JsonObject> metrics) -> {
          System.err.println("Metrics for "+uuid);
          JsonObject dashboard = new JsonObject();
          for (JsonObject metric : metrics) {
            dashboard.mergeIn(metric);
          }
          return dashboard;
    }).doOnUnsubscribe(() -> {
    	System.err.println("Unsub for "+uuid);
    });
    
    return ret;
}
 
Example 4
Project: Java-EE-8-Sampler   File: SseResource.java   Source Code and License Vote up 5 votes
@GET
@Path("subscribe")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void subscribe(@Context SseEventSink eventSink) {
    eventSink.send(sse.newEvent("You are subscribed"));
    broadcaster.register(eventSink);
}
 
Example 5
Project: Java-EE-8-Sampler   File: ServerSentEventsResource.java   Source Code and License Vote up 5 votes
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
public void getMessageQueue(@Context SseEventSink eventSink) {
    synchronized (outputLock) {
        if (this.eventSink != null) {
            throw new IllegalStateException("Server sink already served.");
        }
        this.eventSink = eventSink;
    }
}
 
Example 6
Project: redpipe   File: MyResource.java   Source Code and License Vote up 5 votes
@Path("9")
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
public Observable<String> hello9(@Context io.vertx.rxjava.core.Vertx rxVertx){
	System.err.println("Creating timer");
	return rxVertx.periodicStream(1000).toObservable().map(r -> {
		System.err.println("Tick: "+r);
		return "Timer: "+System.currentTimeMillis();
	});
}
 
Example 7
Project: javaee8-applications   File: SSEResource.java   Source Code and License Vote up 5 votes
@GET
@Path("subscribe")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void subscribe(@Context SseEventSink eventSink,
                      @Context Sse sse){
    eventSink.send(sse.newEvent("Welcome to the List!"));
    eventSink.send(sse.newEvent("Message One!"));
    eventSink.send(sse.newEvent("SERVER-NOTIFICATION", "Message Two!"));
    eventSink.send(sse.newEventBuilder()
                    .comment("Nice Test")
                    .name("SERVER-TEST")
                    .data("Some data...could be an object")
                    .build());
    eventSink.close();
}
 
Example 8
Project: Architecting-Modern-Java-EE-Applications   File: EventsResource.java   Source Code and License Vote up 5 votes
@GET
@Lock(READ)
@Produces(MediaType.SERVER_SENT_EVENTS)
public void itemEvents(@HeaderParam(HttpHeaders.LAST_EVENT_ID_HEADER)
                       @DefaultValue("-1") int lastEventId,
                       @Context SseEventSink eventSink) {

    if (lastEventId >= 0)
        replayLastMessages(lastEventId, eventSink);

    sseBroadcaster.register(eventSink);
}