Tomcat请求超时
在我的 Web 应用程序中,有些请求持续时间超过 20 秒。但在某些情况下,代码可能会导致无限循环或类似的情况,从而降低服务器速度。
我想在服务器端设置 60 秒的请求超时。这是在tomcat中实现的吗?
In my web application there are some requests which last longer than 20 seconds. But in some situations the code can lead to infinite loop or something similar which slows down the server.
I want to put a request timeout for 60 sec on the server side. Is this implemented in tomcat?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 Tomcat 7,您可以添加 StuckThreadDetectionValve ,它将使您能够识别“卡住”的线程。您可以在要进行检测的应用程序的 Context 元素中设置阀门:
这将为任何耗时超过 60 秒的线程在 tomcat 日志中写入一个 WARN 条目,这将使您能够识别应用程序和禁止它们,因为它们有缺陷。
基于 源代码 你也许可以编写自己的阀门来尝试停止线程,但这会对线程池产生连锁反应,并且有 没有可靠的方法在没有线程合作的情况下停止Java中的线程线...
With Tomcat 7, you can add the StuckThreadDetectionValve which will enable you to identify threads that are "stuck". You can set-up the valve in the Context element of the applications where you want to do detecting:
This would write a WARN entry into the tomcat log for any thread that takes longer than 60 seconds, which would enable you to identify the applications and ban them because they are faulty.
Based on the source code you may be able to write your own valve that attempts to stop the thread, however this would have knock on effects on the thread pool and there is no reliable way of stopping a thread in Java without the cooperation of that thread...
如果您试图防止请求运行太长时间,那么在 Tomcat 中设置超时将无济于事。正如 Chris 所说,您可以为 Tomcat 设置全局超时值。但是,来自 Apache Tomcat 连接器 - 通用操作方法
超时,请参阅回复超时部分:
因此 Tomcat 会检测到 servlet 在超时时间内没有响应,并将向用户发送回响应,但不会停止线程运行。我不认为你能实现你想做的事。
If you are trying to prevent a request from running too long, then setting a timeout in Tomcat will not help you. As Chris says, you can set the global timeout value for Tomcat. But, from The Apache Tomcat Connector - Generic HowTo
Timeouts, see the Reply Timeout section:
So Tomcat will detect that the servlet has not responded within the timeout and will send back a response to the user, but will not stop the thread running. I don't think you can achieve what you want to do.
您可以在
server.xml
中设置默认超时You can set the default time out in the
server.xml
本文讨论在服务器级别设置超时。
http://www.coderanch.com/t/364207 /Servlets/java/Servlet-Timeout-two-ways
是什么导致应用程序进入无限循环?如果您要打开与其他资源的连接,您可能希望对这些连接设置超时,并在发生超时时发送适当的响应。
This article talks about setting the timeouts on the server level.
http://www.coderanch.com/t/364207/Servlets/java/Servlet-Timeout-two-ways
What is causing the application to go into infinite loop? If you are opening connections to other resources, you might want to put timeouts on those connections and sending appropriate response when those time out occurs.
对于像我一样不喜欢上面发布的任何解决方案的人,您可以简单地自己实现一个计时器,并通过抛出运行时异常来停止请求执行。如下所示:
或者最好使用 java guava timeLimiter 此处
For anyone who doesn't like none of the solutions posted above like me then you can simply implement a timer yourself and stop the request execution by throwing a runtime exception. Something like below:
or preferably use the java guava timeLimiter here
在 Eclipse 中添加 tomcat 在
Eclipse 中,作为 tomcat 服务器,双击“Tomcat v7.0 Server at Localhost”,将超时设置 45 中所示的属性更改为您喜欢的秒数
Add tomcat in Eclipse
In Eclipse, as tomcat server, double click "Tomcat v7.0 Server at Localhost", Change the properties as shown in time out settings 45 to whatever sec you like