javatomcatrequesttimeout

Tomcat request timeout


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?


Solution

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