Hi All,

I need some help with the async context timeout in servlet 3.0 running on
tomcat 7.0.40.

I have set the asynccontext timeout value, but the timeout behaviour is
highly inconsistent. Sometimes the timeouts work, but most of the times
they don't. I am attaching the source code and the output in plain text
format.

I have tried this on Tomcat 7.0.20, 7.0.37, 7.0.40. Nothing seems to
help. Am i doing something wrong?

Please help. Apologize if I am wasting your time! But I need to fix it.

Thanks for your help!

-Rahul

-- 
*Meet AdIQuity at:*
*Ad:Tech, Singapore *on 13th-14th June 2013

*The information contained in this communication is intended solely for the 
use of the individual or entity to whom it is addressed and others 
authorized to receive it. This communication may contain confidential or 
legally privileged information. If you are not the intended recipient, any 
disclosure, copying, distribution or action taken relying on the contents 
is prohibited and may be unlawful. If you have received this communication 
in error, or if you or your employer does not consent to email messages of 
this kind, please notify to sender immediately by responding to this email 
and then delete it from your system. No liability is accepted for any harm 
that may be caused to your systems or data by this message.*
@WebServlet(name = "TestServlet", urlPatterns = {"/test"},asyncSupported = true)
public class TestServlet extends HttpServlet{

    private static final long serialVersionUID = 1L;

    private static PriorityBlockingQueue<Runnable> pq = new 
PriorityBlockingQueue<Runnable>(1000);

    private static ThreadPoolExecutor threadPoolExecutor = new 
ThreadPoolExecutor(1,1,10, TimeUnit.SECONDS,pq);

    public void service(final ServletRequest servletRequest, final 
ServletResponse response)
        throws ServletException, IOException {
        TestListener listener = new TestListener();
        final AsyncContext asyncContext = 
servletRequest.startAsync(servletRequest, response);
        asyncContext.addListener(listener);
        asyncContext.setTimeout(100);
        Handler handler = new Handler(asyncContext);
        threadPoolExecutor.execute(handler);
    }
}

### Handler class###


public class Handler implements Runnable {

    private AsyncContext asyncContext;

    public Handler(AsyncContext asyncContext){
        this.asyncContext = asyncContext;
    }
    public void run(){
        try {
            long currtime = System.currentTimeMillis();
            Thread.sleep(500);
            System.out.println("slept for " + (System.currentTimeMillis() - 
currtime));
        } catch (InterruptedException e) {
            System.out.println("Error in thread ");
        }

        try{
            if(asyncContext != null){
                System.out.println("Completing async context " + " timeout is " 
+ asyncContext.getTimeout());
                asyncContext.complete();
            }
        }catch (Exception e){
            System.out.println("Exception in completing async context ");
        }
    }
}

#### Listener class ####

public class TestListener implements AsyncListener {
    public void onComplete(AsyncEvent event) throws IOException {
        System.out.println("Event completed");
    }

    public void onError(AsyncEvent event) throws IOException {
        event.getAsyncContext().complete();
    }

    public void onStartAsync(AsyncEvent event) throws IOException {
        // TODO Auto-generated method stub
    }



    public void onTimeout(AsyncEvent event){
        System.out.println("Timeout ");
        event.getAsyncContext().complete();
    }
}


Execution ####

[ops@root combinedlogs]$ time curl "http://localhost:9001/mockresponse/test";

real    0m0.506s
user    0m0.001s
sys 0m0.003s
[ops@root combinedlogs]$ time curl "http://localhost:9001/mockresponse/test";

real    0m0.159s
user    0m0.001s
sys 0m0.003s


####Catalina logs

slept for 500
Completing async context  timeout is 100
Event completed

Timeout 
Event completed
slept for 500
Exception in completing async context
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to