rest icon indicating copy to clipboard operation
rest copied to clipboard

Add a `onClose()` method to the client to indicate the connection has been closed

Open jamezp opened this issue 1 year ago • 5 comments

It would be nice for users, and implementations, to have something like an onClose() method on the Client API. It could look something like:

CompletionStage<Void> onClose();

The CompletionStage would be completed when the close() method is invoked. While in most cases the user is in control of when the Client gets closed, there are cases like CDI where the client may be injected and the user wants to know then the client has been closed.

Implementations could also use this if the connection has been reset and/or a reconnect is not possible. In this case the CompletionStage can end exceptionally.

jamezp avatar Sep 09 '24 22:09 jamezp

If there is interest, this is something I can look at adding/implementing in RESTEasy. I'll be on PTO for starting Thursday through the following week, but I can likely look in late September or early October.

jamezp avatar Sep 09 '24 22:09 jamezp

Closing a connection is an event like many others. Would it make sense to consider the addition of a more generic event listener instead?

spericas avatar Sep 16 '24 13:09 spericas

I agree with @jamezp that adding a method to indicate the connection has been closed will be a nice feature.
Maybe the method to do so should be more like : SseBroadcaster.onClose(Consumer<SseEventSink> onClose) (https://github.com/jakartaee/rest/blob/main/jaxrs-api/src/main/java/jakarta/ws/rs/sse/SseBroadcaster.java#L62).

@spericas, is that what you have in mind when you said "more generic event listener instead" ?

-- Nicolas

NicoNes avatar Sep 16 '24 13:09 NicoNes

@NicoNes Not necessary, my point is that if we identify other useful events to listen for, these should all be grouped in a proper listener instead of creating a method for each one. I'm also not a big fan of using CompletionStage for this type of event reporting.

spericas avatar Sep 16 '24 14:09 spericas

@spericas I would be fine with some kind of event listener too. The idea with a CompletionStage is just the API was already there.

Did the top of my head the events I could see are:

  • Connected
  • Closed
  • Connection Failed
  • Reconnecting (possibly)

Closed and Connection Failed seem like the most important though which is why I thought of a CompletionStage.

jamezp avatar Sep 16 '24 14:09 jamezp