Last active
July 27, 2017 11:05
-
-
Save abhirockzz/a5367cfbafd1a194b2e374e6327759fc to your computer and use it in GitHub Desktop.
JAX-RS 2.1 SSE Client API support in Java EE 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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