Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

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.

@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..");
    }
}


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).

sse-client-output.jpg

To Run Using Docker

Refer to the README.

Further Reading

Cheers!

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

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

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}