Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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