Tomcat请求超时

发布于 2024-11-30 18:23:30 字数 116 浏览 2 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

魔法唧唧 2024-12-07 18:23:30

使用 Tomcat 7,您可以添加 StuckThreadDetectionValve ,它将使您能够识别“卡住”的线程。您可以在要进行检测的应用程序的 Context 元素中设置阀门:

<Context ...>
  ...
  <Valve 
    className="org.apache.catalina.valves.StuckThreadDetectionValve"
    threshold="60" />
  ...
</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:

<Context ...>
  ...
  <Valve 
    className="org.apache.catalina.valves.StuckThreadDetectionValve"
    threshold="60" />
  ...
</Context>

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

岁月无声 2024-12-07 18:23:30

如果您试图防止请求运行太长时间,那么在 Tomcat 中设置超时将无济于事。正如 Chris 所说,您可以为 Tomcat 设置全局超时值。但是,来自 Apache Tomcat 连接器 - 通用操作方法
超时
,请参阅回复超时部分:

JK 还可以对请求回复使用超时。这个超时不
测量响应的完整处理时间。相反,它控制,
允许连续响应数据包之间的时间间隔是多少。

在大多数情况下,这才是人们真正想要的。例如考虑
长时间运行的下载。您将无法设置有效的
全局回复超时,因为下载可能会持续很多分钟。
大多数应用程序在开始之前的处理时间有限
返回响应。对于这些应用程序,您可以设置
显式回复超时。与回复不协调的申请
超时是批处理类型的应用程序、数据仓库和报告
预计处理时间较长的应用程序。

如果 JK 由于回复超时而中止等待响应,
无法停止后端的处理。虽然你有空
在您的网络服务器中处理资源时,请求将继续
在后端运行 - 一旦执行就没有任何方法发送回结果
回复超时被触发。

因此 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:

JK can also use a timeout on request replies. This timeout does not
measure the full processing time of the response. Instead it controls,
how much time between consecutive response packets is allowed.

In most cases, this is what one actually wants. Consider for example
long running downloads. You would not be able to set an effective
global reply timeout, because downloads could last for many minutes.
Most applications though have limited processing time before starting
to return the response. For those applications you could set an
explicit reply timeout. Applications that do not harmonise with reply
timeouts are batch type applications, data warehouse and reporting
applications which are expected to observe long processing times.

If JK aborts waiting for a response, because a reply timeout fired,
there is no way to stop processing on the backend. Although you free
processing resources in your web server, the request will continue to
run on the backend - without any way to send back a result once the
reply timeout fired.

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.

笑红尘 2024-12-07 18:23:30

您可以在server.xml中设置默认超时

<Connector URIEncoding          ="UTF-8"
           acceptCount          ="100"
           connectionTimeout    ="20000"
           disableUploadTimeout ="true"
           enableLookups        ="false"
           maxHttpHeaderSize    ="8192"
           maxSpareThreads      ="75"
           maxThreads           ="150"
           minSpareThreads      ="25"
           port                 ="7777"
           redirectPort         ="8443"/>

You can set the default time out in the server.xml

<Connector URIEncoding          ="UTF-8"
           acceptCount          ="100"
           connectionTimeout    ="20000"
           disableUploadTimeout ="true"
           enableLookups        ="false"
           maxHttpHeaderSize    ="8192"
           maxSpareThreads      ="75"
           maxThreads           ="150"
           minSpareThreads      ="25"
           port                 ="7777"
           redirectPort         ="8443"/>
雾里花 2024-12-07 18:23:30

本文讨论在服务器级别设置超时。
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.

风情万种。 2024-12-07 18:23:30

对于像我一样不喜欢上面发布的任何解决方案的人,您可以简单地自己实现一个计时器,并通过抛出运行时异常来停止请求执行。如下所示:

                  try 
                 {
                     timer.schedule(new TimerTask() {
                       @Override
                       public void run() {
                         timer.cancel();
                       }
                     }, /* specify time of the requst */ 1000);
                 }
                 catch(Exception e)
                 {
                   throw new RuntimeException("the request is taking longer than usual");
                 }

   

或者最好使用 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:

                  try 
                 {
                     timer.schedule(new TimerTask() {
                       @Override
                       public void run() {
                         timer.cancel();
                       }
                     }, /* specify time of the requst */ 1000);
                 }
                 catch(Exception e)
                 {
                   throw new RuntimeException("the request is taking longer than usual");
                 }

   

or preferably use the java guava timeLimiter here

記柔刀 2024-12-07 18:23:30

在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文