I'm using spring boot to create a restful backend application and the frontend is using vue. When someone sends a rest request to my backend application via my frontend webpage, is it possible to stop the backend processing thread after the webpage or the web browser is closed?
Asked
Active
Viewed 8,081 times
1 Answers
5
HTTP Request cannot be cancelled. General guideline is, the REST calls should be very short. If in case, your REST calls are long running, recommendation is to break into granular calls.
If that is not an option and if you want to cancel a back-end processing, following option can be tried
For every back-end call, return a job id using which server can uniquely identify and return it to the client
Detect browser close
- Expose a new Service to cancel based on the Unique Job Id
- Handle logic in Server
This will require considerable amount of change!

CGS
- 2,782
- 18
- 25
-
Thanks Chids, I'm a newbie of spring boot and I'm using the classic controller-service-dao structure to implement my rest api. I'm not aware of how Spring internally works when my controller gets a request from my frontend, what I was guessing previously was that spring starts a thread whenever it receives a rest request and I could detect the http connection is closed by the front-end side in spring and then abort the back-end thread. – ben Jan 12 '18 at 04:29
-
Your understanding is correct, but spring will not be able to detect that HTTP connection is closed, hence you need to expose a Service with Put/Delete and gracefully cancel the request programmatically – CGS Jan 12 '18 at 06:03
-
Thanks Chids, I got it. – ben Jan 12 '18 at 07:07
-
1http2 request can be canceled https://stackoverflow.com/questions/54020329/is-there-any-feature-in-jee8-for-cancelling-http-2-request-processing – Александр Дубинин Aug 25 '22 at 11:31