Skip to content

Instantly share code, notes, and snippets.

@abhirockzz
Last active July 27, 2017 11:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhirockzz/a5367cfbafd1a194b2e374e6327759fc to your computer and use it in GitHub Desktop.
Save abhirockzz/a5367cfbafd1a194b2e374e6327759fc to your computer and use it in GitHub Desktop.
JAX-RS 2.1 SSE Client API support in Java EE 8
@Singleton
@Startup
public class ProgrammaticSSEClient {
Client sseClient;
WebTarget target;
@Resource
TimerService tsvc;
@PostConstruct
public void init() {
this.sseClient = ClientBuilder.newClient();
this.target = this.sseClient.target("https://sse.now.sh");
tsvc.createSingleActionTimer(15000, null);
System.out.println("SSE client timer created");
eventSource = SseEventSource.target(target).build();
System.out.println("SSE Event source created........");
}
SseEventSource eventSource;
@Timeout
public void client() {
System.out.println("SSE Client triggered in thread "+ Thread.currentThread().getName());
try {
eventSource.register((sseEvent)
-> {
System.out.println("Events received in thread " + Thread.currentThread().getName());
System.out.println("SSE event recieved ----- " + sseEvent.readData());
},
(e) -> e.printStackTrace());
eventSource.open();
System.out.println("Source open ????? " + eventSource.isOpen());
} catch (Exception e) {
e.printStackTrace();
}
}
@PreDestroy
public void close() {
eventSource.close();
System.out.println("Closed SSE Event source..");
sseClient.close();
System.out.println("Closed JAX-RS client..");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment