Receiving Server Sent Events (SSE) with JAX-RS 2.1 and Java EE 8

To receive Server Sent Events (SSE) with a Java JAX-RS 2.1 (Java EE 8) client you will need an additional dependency:


For the initialization of a SseEventSource you need a WebTarget which comes from a "stock" JAX-RS client:

import static java.util.concurrent.TimeUnit.SECONDS;

public class SSEClientIT {

    private Client client;
    private WebTarget tut;

    public void initClient() {
        this.client = ClientBuilder.newClient();
        this.tut ="http://localhost:8080/sse/resources/beats");
...with the target you can create a SseEventSource to register an instance of: Consumer<InboundSseEvent> as listener:

    public void init() throws IOException {
        SseEventSource sse = SseEventSource.
                .reconnectingEvery(2, SECONDS)
        //block here, otherwise the test method will complete

    void onMessage(InboundSseEvent event) {
        String id = event.getId();
        String name = event.getName();
        String payload = event.readData();
        String comment = event.getComment();

See you at Java EE 8 on Java 9, at Munich Airport, Terminal 2

Cloudy Jakarta EE and MicroProfile: Microservices, Clouds and Beyond Jakarta EE / MicroProfile airhacks workshops at MUC airport, Winter Edition the podcast:

Stay in touch:


Hi Adam,

can you please show, how to provide cookies?
Neither at WebTarget nor at SseEventSource I can find any methods for providing headers.

Thanks, Frank

Posted by Frank on March 21, 2019 at 08:02 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments