Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Is there a pattern to know when a client disconnects?


On Tue, May 2, 2023 at 10:33 PM Adam Fine <adamcfine@xxxxxxxxx> wrote:
> Hi all, My use case is that  I need to  cancel a long running  database execution if the client disconnects.
> Setting setMaxInactiveInterval in combination with   a HttpSessionListener  sessionDestroyed event  seems promising but the events only generate   on the next  attempt to connect with the expired session.   I could probably implement a thread which periodically checked every session but I'm wondering if there are any common patterns for this use case.   I see AsyncContext raised as an option but the listeners on the  AsyncContext    do not detect  if a client disconnects and leave me in the same place as using a HttpSessionListener.

There is no pattern available for HTTP/1.1, because the server must
stop reading after the request (so it cannot read the -1 as the result
of the client closing), otherwise it is open to infinite buffering
It can be done by the server returning a response with URIs that the
client can "poll" to check whether the request finished or "poll to

In HTTP/2 the client may send a RST_STREAM so the server can detect it
and notify AsyncListener.onError(), so way easier, but only HTTP/2

Simone Bordet
Developer advice, training, services and support
from the Jetty & CometD experts.

Back to the top