4

Is there a annotation or something in spring-mvc which lets me call something as soon as a client closes connection or cancels a request in any way? I got a rest service which is normally very quick but can dependent on user input go for some seconds. I want to cancel the processing on server side as soon as the client breaks its connection.

I could implement something where the client has to poll from time to time to get results but I want to cancel the process when the client closes its browser for example or just cancels a curl request. Any hook provided by spring here?

Dennis Ich
  • 3,585
  • 6
  • 27
  • 44
  • That doesn't sound much like a REST service. – chrylis -cautiouslyoptimistic- Sep 27 '16 at 14:58
  • HTTP is stateless you can't do that. You could check however if the TCP connection has been interrupted, because TCP is stateful. – vtor Sep 27 '16 at 15:11
  • Well we don't need to call it REST service. I got a process that may take a Minute and which costs resources. If a user is impatient and cancels by leaving the side I want to have the resources back. So I need something which tells me whether or not the client is still waiting. But it should also work with curl without having a user to poll several times if its finished in the right time or his request gets killed. So there must be some type of connection or check between client and server which I should be able to intercept. The question is how? – Dennis Ich Sep 27 '16 at 15:12

2 Answers2

2

Currently it's not implemented.

It cannot be implemented on Spring side because Spring cannot access to Socket for detect closing connection.

It can be implemented only on container side (Jetty, Tomcat, etc...) It can be detected only with socket's input stream read in separated thread. When read throws IOException which can be interpreted as "Connection closed" state it shall call interrupt() for thread which started for process request. I wanted the same function and actually know how to do it. Guys from tomcat dev team don't wants to listen me.

Andrew
  • 130
  • 1
  • 5
  • 1
    Andrew @Andrew could you please provide an example of such implementation. Thanks – Sam Jan 27 '21 at 12:37
0

Once the request has been sent to the server it cannot be cancel.

You can send to the server another request to cancel the process of the previous one. You can use the js/html event beforeunload.

You can see more here

http://www.w3schools.com/tags/ev_onbeforeunload.asp

jquery beforeunload when closing (not leaving) the page?

Community
  • 1
  • 1
reos
  • 8,766
  • 6
  • 28
  • 34
  • What happens if the user uses another client? Curl for example. Its open and should be used in that way but it may get cancelled by lost connections or something. In this case I want to call a special Method which cancels the backend operations. – Dennis Ich Sep 27 '16 at 15:14
  • If the connection was lost, the request never gets to the server. Once the connection reaches the server is over. It's important to understand that http is an asynchronous protocol, the requests are not attached to others request, every one is independent. – reos Sep 27 '16 at 15:19
  • yes but curl for example blocks until the request comes back. So how does it wait for it? If I kill the service while I run a curl request it tells me "connection reset by server" if curl can capture that I should be able to do that on the server too and just cancel a Thread in which the request is running so it returns the resources. – Dennis Ich Sep 27 '16 at 15:23
  • ok, I got it. I think this functionality is not part o Servlet specification and it depends on the appserver if it implement some event listener of the socket lifecycle. Probably Java websocket could help JSR-356 but your app server needs to support it. – reos Sep 27 '16 at 15:38