I changed a little in my code:
final AtomicLong writeResponseTime= new AtomicLong(0);
final long timeout = 30000;
final AsyncContext async = req.startAsync();
async.setTimeout(timeout);
async.addListener(new AsyncListener(){
@Override
public void onComplete(AsyncEvent event) throws IOException {
System.out.println(Thread.currentThread()+"onComplete");
}
@Override
public void onTimeout(AsyncEvent event) throws IOException {
System.out.println(Thread.currentThread()+"onTimeout ");
long current = System.currentTimeMillis();
System.out.println("\t the set timeout = "+ timeout/1000+"
s");
System.out.println("\t the diff between writeResponse and
onTimeout = "+(current-writeResponseTime.get())/1000 +" s");
}
@Override
public void onError(AsyncEvent event) throws IOException {
System.out.println(Thread.currentThread()+"onError
"+event.getThrowable());
}
@Override
public void onStartAsync(AsyncEvent event) throws IOException {
System.out.println(Thread.currentThread()+"onStartAsync");
}
});
async.start(new Runnable(){
@Override
public void run() {
try {
Thread.sleep(timeout/2);
writeResponseTime.set(System.currentTimeMillis());
async.getResponse().getWriter().write("22222");
async.getResponse().getWriter().flush();
System.out.println(Thread.currentThread()+"run ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
The output:
Thread[http-bio-80-exec-4,5,main]run
Thread[http-bio-80-exec-5,5,main]onTimeout
the set timeout = 30 s
the diff between writeResponse and onTimeout = 15 s
Thread[http-bio-80-exec-5,5,main]onError null
Thread[http-bio-80-exec-5,5,main]onComplete
So it seems that the write of the data doesn't reset the timeout.
2013/5/24 Mark Thomas <[email protected]>
> On 24/05/2013 10:27, jie tang wrote:
> > But it means that even if I write some content to response, the onTimeout
> > method is still called
>
> Correct. The timeout starts when the AsyncContext is started and is
> reset every time data is written to the response. Exactly the same way
> socket timeouts work.
>
> If you don't want a timeout, use setTimeout(0)
>
> Mark
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>