Skip to content

Instantly share code, notes, and snippets.

@abhirockzz
Created August 15, 2017 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhirockzz/0e49e86b76c6ac3e0682a1dfe8905e27 to your computer and use it in GitHub Desktop.
Save abhirockzz/0e49e86b76c6ac3e0682a1dfe8905e27 to your computer and use it in GitHub Desktop.
Handling custom types with JAX-RS SSE API
@GET
@Produces("text/event-stream")
public void fetch(@Context Sse sse, @Context SseEventSink eSink) {
OutboundSseEvent stringEvent = sse.newEventBuilder()
.name("stringEvent")
.data(new Date().toString()).build();
eSink.send(stringEvent);
OutboundSseEvent primitiveTypeEvent = sse.newEventBuilder()
.name("primitiveTypeEvent")
.data(System.currentTimeMillis()).build();
eSink.send(primitiveTypeEvent);
OutboundSseEvent jsonbType = sse.newEventBuilder()
.name("jsonbType")
.data(new Employee("test@test", "test", 42))
.mediaType(MediaType.APPLICATION_JSON_TYPE)
.build();
eSink.send(jsonbType);
OutboundSseEvent jaxbType = sse.newEventBuilder()
.name("jaxbType")
.data(new Customer("testcut@test", "king"))
.mediaType(MediaType.APPLICATION_XML_TYPE)
.build();
eSink.send(jaxbType);
OutboundSseEvent customObjWithMBW = sse.newEventBuilder()
.name("customObjWithMBW")
.data(new Student("stud@test", "stud-007")).build();
eSink.send(customObjWithMBW);
System.out.println("events sent");
eSink.close();
System.out.println("sink closed");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment