尽快停止/取消断开连接的GET请求线程 [英] stopping/canceling disconnected GET request threads as soon as possible

查看:108
本文介绍了尽快停止/取消断开连接的GET请求线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jetty,版本7.0.1,如果这很重要。

I am using jetty, version 7.0.1 if that matters.

有时我在服务器上有一些很长时间运行的任务,我想取消/停止如果客户端断开连接(在GET请求的情况下,不是例如POST文件上传)。似乎情况并非如此,并且任务继续运行到
完成。

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.

也许我可以使用ServletRequestListener.requestDestoryed监听器来获取此类任务的通知但是什么建议
方法停止请求线程?如何释放数据库连接,文件句柄或运行任务等资源
(执行程序服务)?

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?

推荐答案

首先我建议更新到最新版本的码头,自7.0系列以来我们已经修好了一吨

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

第二,解决此问题的最佳方法是设计使用jetty-continuations获取servlet 2.5规范的异步servlet支持(即jetty7)或更新到servlet 3.0(jetty 8)并且不依赖于servlet api的get方法来阻止等待响应发送。而是处理请求然后生成线程或使用执行程序future来处理操作,然后在有有效负载或成功消息返回时回调请求。原因是当您在servlet api中阻塞请求进程时,您正在消耗servlet线程池中的资源和线程...您将能够通过使用continuation或3.0的异步servlet来扩展更清晰...

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...

此外,您还可以设计一个适当的机制来管理这些线程和诸如超时之类的事情以及针对异常情况的正确通知机制,并且它将在这样的一个servlet容器。

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至少:)

这篇关于尽快停止/取消断开连接的GET请求线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆