You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by jie tang <cr...@gmail.com> on 2013/05/24 10:05:27 UTC

What's the meaning of timeout of an asynchronous operation

Hi,

I don't understand the meaning of timeout of an asynchronous operation.
Servlet 3.0 says "The time out applies to the AsyncContext once the
container-initiated dispatch during which one of the
ServletRequest.startAsync methods was called has returned to the container."
But when is the completion of the asynchronous operation? The invocation of
AsyncContext.complete or AsyncContext.dispatch? Or something else?

Thanks.
Jie

Re: What's the meaning of timeout of an asynchronous operation

Posted by jie tang <cr...@gmail.com>.
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 <ma...@apache.org>

> 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: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: What's the meaning of timeout of an asynchronous operation

Posted by Mark Thomas <ma...@apache.org>.
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: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: What's the meaning of timeout of an asynchronous operation

Posted by jie tang <cr...@gmail.com>.
But it means that even if I write some content to response, the onTimeout
method is still called


2013/5/24 Mark Thomas <ma...@apache.org>

> On 24/05/2013 10:15, jie tang wrote:
> > I tried the following code:
> >         final AsyncContext async = req.startAsync();
> >         async.setTimeout(30000);
> >
> >         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");
> >
> >             }
> >
> >             @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 {
> >                     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();
> >                 }
> >             }
> >
> >         });
> >
> > The output:
> >     Thread[http-bio-80-exec-4,5,main]run
> >     Thread[http-bio-80-exec-5,5,main]onTimeout
> >     Thread[http-bio-80-exec-5,5,main]onError null
> >     Thread[http-bio-80-exec-5,5,main]onComplete
> > Although the Runnable writes some content. The onTimeout method is still
> > invoked.
>
> As expected. 30s after you write the content, the timeout is triggered.
>
> Mark
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: What's the meaning of timeout of an asynchronous operation

Posted by Mark Thomas <ma...@apache.org>.
On 24/05/2013 10:15, jie tang wrote:
> I tried the following code:
>         final AsyncContext async = req.startAsync();
>         async.setTimeout(30000);
> 
>         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");
> 
>             }
> 
>             @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 {
>                     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();
>                 }
>             }
> 
>         });
> 
> The output:
>     Thread[http-bio-80-exec-4,5,main]run
>     Thread[http-bio-80-exec-5,5,main]onTimeout
>     Thread[http-bio-80-exec-5,5,main]onError null
>     Thread[http-bio-80-exec-5,5,main]onComplete
> Although the Runnable writes some content. The onTimeout method is still
> invoked.

As expected. 30s after you write the content, the timeout is triggered.

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: What's the meaning of timeout of an asynchronous operation

Posted by jie tang <cr...@gmail.com>.
I tried the following code:
        final AsyncContext async = req.startAsync();
        async.setTimeout(30000);

        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");

            }

            @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 {
                    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();
                }
            }

        });

The output:
    Thread[http-bio-80-exec-4,5,main]run
    Thread[http-bio-80-exec-5,5,main]onTimeout
    Thread[http-bio-80-exec-5,5,main]onError null
    Thread[http-bio-80-exec-5,5,main]onComplete
Although the Runnable writes some content. The onTimeout method is still
invoked.


2013/5/24 jie tang <cr...@gmail.com>

> Thank you very much
>
>
> 2013/5/24 Mark Thomas <ma...@apache.org>
>
>> On 24/05/2013 09:23, jie tang wrote:
>> > So if I use AsyncContext.start to run a Runnable. When that Runnable
>> does
>> > some work but not write to response, will AsyncListener.onTimeout be
>> > invoked?
>>
>> Yes, unless you set the timeout to zero or less (no timeout).
>>
>> The default value is 30 seconds.
>>
>> Mark
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>

Re: What's the meaning of timeout of an asynchronous operation

Posted by jie tang <cr...@gmail.com>.
Thank you very much


2013/5/24 Mark Thomas <ma...@apache.org>

> On 24/05/2013 09:23, jie tang wrote:
> > So if I use AsyncContext.start to run a Runnable. When that Runnable does
> > some work but not write to response, will AsyncListener.onTimeout be
> > invoked?
>
> Yes, unless you set the timeout to zero or less (no timeout).
>
> The default value is 30 seconds.
>
> Mark
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: What's the meaning of timeout of an asynchronous operation

Posted by Mark Thomas <ma...@apache.org>.
On 24/05/2013 09:23, jie tang wrote:
> So if I use AsyncContext.start to run a Runnable. When that Runnable does
> some work but not write to response, will AsyncListener.onTimeout be
> invoked?

Yes, unless you set the timeout to zero or less (no timeout).

The default value is 30 seconds.

Mark


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: What's the meaning of timeout of an asynchronous operation

Posted by jie tang <cr...@gmail.com>.
So if I use AsyncContext.start to run a Runnable. When that Runnable does
some work but not write to response, will AsyncListener.onTimeout be
invoked?


2013/5/24 Mark Thomas <ma...@apache.org>

> On 24/05/2013 09:16, jie tang wrote:
> > Thanks.
> >
> > So the only way to avoid the invocation of AsyncListener.onTimeout is
> that
> > we invoke AsyncContext.complete or AsyncContext.dispatch?
>
> Or write some content to the response.
>
> Mark
>
> >
> >
> > 2013/5/24 Mark Thomas <ma...@apache.org>
> >
> >> On 24/05/2013 09:05, jie tang wrote:
> >>> Hi,
> >>>
> >>> I don't understand the meaning of timeout of an asynchronous operation.
> >>> Servlet 3.0 says "The time out applies to the AsyncContext once the
> >>> container-initiated dispatch during which one of the
> >>> ServletRequest.startAsync methods was called has returned to the
> >> container."
> >>> But when is the completion of the asynchronous operation? The
> invocation
> >> of
> >>> AsyncContext.complete or AsyncContext.dispatch? Or something else?
> >>
> >> Either of those will end aysnc processing.
> >>
> >> Mark
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> >> For additional commands, e-mail: users-help@tomcat.apache.org
> >>
> >>
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: What's the meaning of timeout of an asynchronous operation

Posted by Mark Thomas <ma...@apache.org>.
On 24/05/2013 09:16, jie tang wrote:
> Thanks.
> 
> So the only way to avoid the invocation of AsyncListener.onTimeout is that
> we invoke AsyncContext.complete or AsyncContext.dispatch?

Or write some content to the response.

Mark

> 
> 
> 2013/5/24 Mark Thomas <ma...@apache.org>
> 
>> On 24/05/2013 09:05, jie tang wrote:
>>> Hi,
>>>
>>> I don't understand the meaning of timeout of an asynchronous operation.
>>> Servlet 3.0 says "The time out applies to the AsyncContext once the
>>> container-initiated dispatch during which one of the
>>> ServletRequest.startAsync methods was called has returned to the
>> container."
>>> But when is the completion of the asynchronous operation? The invocation
>> of
>>> AsyncContext.complete or AsyncContext.dispatch? Or something else?
>>
>> Either of those will end aysnc processing.
>>
>> Mark
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: What's the meaning of timeout of an asynchronous operation

Posted by jie tang <cr...@gmail.com>.
Thanks.

So the only way to avoid the invocation of AsyncListener.onTimeout is that
we invoke AsyncContext.complete or AsyncContext.dispatch?


2013/5/24 Mark Thomas <ma...@apache.org>

> On 24/05/2013 09:05, jie tang wrote:
> > Hi,
> >
> > I don't understand the meaning of timeout of an asynchronous operation.
> > Servlet 3.0 says "The time out applies to the AsyncContext once the
> > container-initiated dispatch during which one of the
> > ServletRequest.startAsync methods was called has returned to the
> container."
> > But when is the completion of the asynchronous operation? The invocation
> of
> > AsyncContext.complete or AsyncContext.dispatch? Or something else?
>
> Either of those will end aysnc processing.
>
> Mark
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: What's the meaning of timeout of an asynchronous operation

Posted by Mark Thomas <ma...@apache.org>.
On 24/05/2013 09:05, jie tang wrote:
> Hi,
> 
> I don't understand the meaning of timeout of an asynchronous operation.
> Servlet 3.0 says "The time out applies to the AsyncContext once the
> container-initiated dispatch during which one of the
> ServletRequest.startAsync methods was called has returned to the container."
> But when is the completion of the asynchronous operation? The invocation of
> AsyncContext.complete or AsyncContext.dispatch? Or something else?

Either of those will end aysnc processing.

Mark


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org