stopping/canceling disconnected GET request threads as soon as possible

此生再无相见时 提交于 2019-12-12 10:03:56

问题


I am using jetty, version 7.0.1 if that matters.

Sometimes I have some quite long running tasks on a server which I would like to cancel/stop if the client disconnects (in case of GET requests, not e.g. POST file uploads). It seems this is not the case, and that tasks continue to run to completion.

Perhaps I can use ServletRequestListener.requestDestoryed listener to get notification of such tasks but what is recommended approach for stoping the request thread? What about releasing resources like database connections, file handles or running tasks (executor service)?

What is the recommended approach in stopping such tasks as soon as possible?


回答1:


first I would recommend updating to the latest versions of jetty, we have fixed a ton since 7.0 series

second, your best bet to solve this problem is by design using either jetty-continuations to get async servlet support with servlet 2.5 spec (which is jetty7) or update to servlet 3.0 (jetty 8) and not rely on the get methods of the servlet api to block waiting for a response to send. Instead process the request and then spawn a thread or use an executor future to process the actions, then calling back to the request when you have a payload or success message to return. Reason being that while your in the servlet api blocking on the request process you are consuming resources and threads from your servlet thread pool...you'll be able to scale up much cleaner by using continuations or the async servlets of 3.0...

Also you'll be able to design a proper mechanism for managing these threads and things like timeouts and the proper notification mechanism for exceptional conditions, and it will be testable outside of a servlet container that way.

imo at least :)



来源:https://stackoverflow.com/questions/9750060/stopping-canceling-disconnected-get-request-threads-as-soon-as-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!