I am following the example given in the Jersey documentation, "Consuming SSE Events within Jersey clients":
import javax.ws.rs.sse.SseEventSource;
...
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target("http://example.com/events");
SseEventSource sseEventSource = SseEventSource.target(target).build();
sseEventSource.subscribe((event) -> System.out.println(event.getName() + "; "
+ event.readData(String.class)));
sseEventSource.open();
First, there seems to be an error in the documentation. Based on the JAX-RS API JavaDoc the method is register
, not subscribe
.
My client code looks like this:
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.sse.InboundSseEvent;
import javax.ws.rs.sse.SseEventSource;
...
Client client = ClientBuilder.newClient();
WebTarget statsTarget = client.target( "http://localhost:8080/notifications/stats");
SseEventSource statsES = SseEventSource.target( statsTarget).build();
statsES.register( (statsEvent) -> handleStatsEvent( statsEvent),
(t) -> handleStatsError( t));
statsES.open();
boolean isOpen = statsES.isOpen();
Thread.sleep( 60000);
statsES.close();
isOpen
is false. No exception is thrown. No logs are created. The error handler is not called.
I have logging in the server, and I can see that a request is made (and an error returned - that is a separate issue). If I change the URL to a host that does not exist, there still is no feedback.
Using curl -v -H "Accept: text/event-stream" http://localhost:8080/notifications/stats
I see the request in the server log, and I get the error response back.
I am using Jersey 2.26. When I use 2.25.1 I get a runtime exception (ClassNotFoundException) because there is no jersey-hk2
before 2.26.
Is there anything wrong in my client code that I do not get any kind of error feedback?