2

Optimally, I would like to be able to do something like this

Request request = new Request("http://someendpoint")
request.onMessage(payload -> System.out.println(payload))
request.onError(error -> error.printStackTrace())
request.onClose(() -> System.out.println("closed"))
request.onConnect(isReconnect -> System.out.println("connected"))

Obviously doesn't have to be exactly like that, just thought it would help explain what I was trying to do.

I have seen that there is some sort of support for this in Jersey. I would like to find an alternative but will use that if nothing else is available.

user1803551
  • 12,965
  • 5
  • 47
  • 74
Caleb Bassham
  • 1,874
  • 2
  • 16
  • 33
  • I hope you know that you will lose 50 reputation points when you put a bounty on a question – its.david Dec 06 '17 at 02:56
  • If you look for something specific to “Apache HTTP Components” you should tag your question as such. And consider removing irrelevant tags, i.e. I don’t see any Kotlin specific in your question. Tagging your question right raises the chances of getting the attention of someone who knows an answer. – Holger Dec 06 '17 at 09:22
  • @Holger I have removed the Kotlin tag, forgot to remove it earlier. It doesn't have to be specific to Apache HTTP Components. The only reason I don't want to use Jersey is because it is complete other HTTP client as well and I am already using Apache. Sorry for the confusion. – Caleb Bassham Dec 06 '17 at 12:11
  • @ezzzCash You get a confirmation box telling you that the bounty points are removed and cannot be regained whenever you try to put a bounty. If it's a good question though, the bounty attracts upvotes so there's some compensation. – user1803551 Dec 07 '17 at 15:01

1 Answers1

2

SSEs are available in JavaEE 8 through JAX-RS 2.1 in the javax.ws.rs.sse package. Unlike requests and responses which can be synchronous or asynchronous, SSEs are asynchronous by nature.

For a demonstration of SSEs have a look at this video by David Delabassee. Your code (which is client-side only) would looks something like this:

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://someendpoint");

SseEventSource eventSource = SseEventSource.target(target)
                                           .reconnectingEvery(5, TimeUnit.SECONDS)
                                           .build();
eventSource.register(payload -> System.out.println(payload),   // Consumer<InboundSseEvent>
                     error -> error.printStackTrace(),         // Consumer<Throwable>
                     () -> System.out.println("no more events"));
eventSource.open();
// and eventually
eventSource.close();

There are no built-in handlers for (re)connection and closing, but you could customize something to have the same effect.

JAX-RS 2.1 is implemented in (at least):

  • Jersey 2.26, which is included in Glassfish 5 and Payara 5 alpha/snapshot/pre-release.
  • RESTEasy 4.0.0.Beta1 (released less than a month ago) and is reported to work on Wildfly 10 and above, though I think only Wildfly 12 will implement the full JavaEE 8 (announcements change with time...).
user1803551
  • 12,965
  • 5
  • 47
  • 74
  • If "There are no built-in handlers for (re)connection and closing," what does the `reconnectingEvery()` do? – Caleb Bassham Dec 09 '17 at 03:37
  • Also, am I not able to just use JAX-RS directly, when I try to create a new client I get `java.lang.ClassNotFoundException: org.glassfish.jersey.client.JerseyClientBuilder` – Caleb Bassham Dec 09 '17 at 03:51
  • @CalebBassham I meant that there is no "on reconnect" event handler that you can plug something into (code to execute on every reconnect) like "on event received" or "on error received". – user1803551 Dec 09 '17 at 04:54
  • @CalebBassham Not sure what you mean by "use JAX-RS directly". JAX-RS is a specification, you have to use a library implementing it like Jersey or RESTEasy. It looks like you're using Jersey, but if you have a specific problem with the code then ask a new question. You can give me the link here if you want. – user1803551 Dec 09 '17 at 05:13
  • When I said "use it directly" I mean like get the maven dependency and just use that instead of Jersey or some other thing on top of it. When I try to open an SseEventSource it gives me the exception. My question is, do I have to use Jersey (or some other implementation) to use JAX-RS? – Caleb Bassham Dec 09 '17 at 05:43
  • @CalebBassham Yes, that's exactly what I said. – user1803551 Dec 09 '17 at 05:59
  • @user1803551 - Nice demonstration. If you wanted to send requests to this particular endpoint, how would you do that? Lets say in a functional test? – Don Code May 20 '20 at 16:01
  • @DonCode You should ask this as a new question. – user1803551 May 22 '20 at 19:34