christophe blin wrote:
> Hi,
> 
> I'd like to cancel a servlet request if it took more than 5 seconds.
> 
> ATM, I know this is not possible directly via a tomcat configuration (in
> fact I found no servlet container with this option).
> By googling and doing a lot of tries (mainly based on
> http://marc.info/?l=tomcat-user&m=109463824530983&w=2) , I find the
> following snippet to be working but I have some problems with it.

Are you really sure you want to kill a request after 5 seconds?

Do you mean 5 seconds after the connection is opened, or 5 seconds after
the response has begun?

The properties of the http connector element in server.xml allow you to
configure the former, but I'd strongly advise against doing so:

connectionTimeout="5"

See: http://tomcat.apache.org/tomcat-6.0-doc/config/http.html

I can't think of reason why you'd want to interrupt the *response* after
5 seconds, but if you do, then you probably need to run whichever job is
problematic inside a Thread that you interrupt after 5 seconds.

Can you elaborate on the reason behind this requirement?

p



> 1/ it use the deprecated Thread.stop method but I do not know if there is a
> way to use interrupt or any safer mecanism
> 
> 2/ I do not know if this is really the best way to achieve what I want so if
> you have some pointers, do not hesitate to give them to me.
> 
> thanks,
> 
> chris
> 
> [code]
> public class InterruptFilter implements Filter {
>     public void init(FilterConfig arg0) throws ServletException {
>     }
> 
>     public void destroy() {
>     }
> 
>     public void doFilter(ServletRequest request, ServletResponse response,
> FilterChain fc) throws IOException, ServletException {
>         InterruptTimer mTimer = new InterruptTimer(5000, 10,
> Thread.currentThread());
>         mTimer.start();
>         fc.doFilter(request, response);
>         mTimer.reset();
>     }
> 
>     public class InterruptTimer extends Thread {
>         long oEndTime = 0;
>         int oSleepTime = 10;
>         Thread oStartingThread = null;
> 
>         public InterruptTimer(int aLength, int aSleepTime, Thread
> aStartingThread) {
>             // Both times are in ms
>             oSleepTime = aSleepTime;
>             oEndTime = System.currentTimeMillis() + aLength;
>             oStartingThread = aStartingThread;
>         }
> 
>         public void run() {
>             // Loop until the end time is reached or reset() was called.
>             while (System.currentTimeMillis() < oEndTime) {
>                 try {
>                     Thread.sleep(oSleepTime);
>                 } catch (InterruptedException ex) {
>                 }
>             }
>             if (oEndTime > 0) {
>                 timeout();
>             }
>         }
>         public void reset() {
>             oEndTime = 0;
>         }
> 
>         public void timeout() {
>             System.out.println("**************** stop the request thread");
>             oStartingThread.stop();
>         }
>     }
> }
> 
> [/code]


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to