Tomcat请求超时 [英] Tomcat request timeout
问题描述
在我的网络应用程序中,有些请求的持续时间超过20秒。但在某些情况下,代码可能导致无限循环或类似的东西,从而减慢服务器的速度。
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.
我想在服务器端将请求超时60秒。这是在tomcat中实现的吗?
I want to put a request timeout for 60 sec on the server side. Is this implemented in tomcat ?
谢谢你,
Horatiu
Thank you, Horatiu
推荐答案
使用Tomcat 7,您可以添加 StuckThreadDetectionValve 这将使您能够识别卡住的线程。您可以在要检测的应用程序的Context元素中设置阀门:
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:
<Context ...>
...
<Valve
className="org.apache.catalina.valves.StuckThreadDetectionValve"
threshold="60" />
...
</Context>
这会在tomcat日志中为任何花费超过60秒的线程写入一个WARN条目,将使您能够识别应用程序并禁止它们因为它们有问题。
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.
基于源代码您可以编写自己的阀门,试图阻止线程,但这会对线程池产生影响,并且有没有可靠的方法在没有该线程的合作的情况下停止Java中的线程......
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请求超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!