Over a million developers have joined DZone.

JAX-RS 2.1 SSE Client API Using GlassFish 5 on Docker

DZone's Guide to

JAX-RS 2.1 SSE Client API Using GlassFish 5 on Docker

Java EE 8 fans will want to take a look at the client-side API out as part of JAX-RS 2.1. See how the SSE client API works and how you can use Docker to run it.

· Java Zone ·
Free Resource

The CMS developers love. Open Source, API-first and Enterprise-grade. Try BloomReach CMS for free.

Along with the Server API for SSE, JAX-RS 2.1 (part of Java EE 8) also has an equivalent client-side API.

Here is a quick peek – you can grab the project from GitHub.

public class ProgrammaticSSEClient {

    Client sseClient;
    WebTarget target;

    TimerService tsvc;

    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;

    public void client() {
        System.out.println("SSE Client triggered in thread "+ Thread.currentThread().getName());

        try {
                    -> {
                System.out.println("Events received in thread " + Thread.currentThread().getName());
                System.out.println("SSE event recieved ----- " + sseEvent.readData());
                    (e) -> e.printStackTrace());

            System.out.println("Source open ????? " + eventSource.isOpen());
        } catch (Exception e) {


    public void close() {
        System.out.println("Closed SSE Event source..");
        System.out.println("Closed JAX-RS client..");

To Summarize

  • We use a @Singleton EJB with @Startup
  • During initialization
    • Create a single action (one time) Timer, i.e. @PostConstruct
    • And instantiate the SSEEventSource – its a public SSE source.
  • When the timer expires, the @Timeout annotated method gets triggered and
    • Opens the SSE connection.
    • Prints them out – this action is registered as a callback (written as a Java 8 lamda in this case).
  • Execution thread pool(s) – notice this in the logs
    • The timer itself is triggered in the EJB thread pool.
    • SSE event callbacks are executed in the ManagedExecutorService thread pool (thanks to Java EE Concurrency Utilities).


To Run Using Docker

Refer to the README.

Further Reading


BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

java ,jax-rs ,glassfish ,docker ,sse client ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}