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.

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]
-- 
View this message in context: 
http://www.nabble.com/How-to-have-a-request-timeout---tf4855066.html#a13892897
Sent from the Tomcat - User mailing list archive at Nabble.com.


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