Page 1 of 1

About Question enthuware.jwpv6.2.594 :

Posted: Sat Apr 30, 2016 7:37 pm
by himaiMinh
Hi, I tried to demo the scenario where another asynch process starts , but the onStartAsync method is not called.

Code: Select all

@WebListener
public class MyListener implements AsyncListener {
    
     ... 
    public void onStartAsync(AsyncEvent event){
        
        System.out.println("Async Listener on start");
    }
     ...
}

Code: Select all

@WebServlet(urlPatterns="/foo/*",
            name = "NullServlet",
            asyncSupported = true)
public class NullServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp){
        MyListener myAsyncListener = new MyListener();
        req.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
        final AsyncContext ac = req.startAsync();
        ac.addListener(myAsyncListener);
        
        ac.start(new Runnable(){
            public void run(){
                System.out.println(" I am async thread.");
             }
        });
        //try to start a new async process, 
        //but the listener's onStartAsync method is not called.
        ac.start(new Runnable(){
            public void run(){
                System.out.println(" I am async thread 1 .");
              
            }
        });
        
       
    }
}
I am async thread.
I am async thread 1 .
Async Listener on timeout
Async Listner on complete

Re: About Question enthuware.jwpv6.2.594 :

Posted: Sat Apr 30, 2016 8:52 pm
by admin
The JavaDoc API description for addListener method says:
"Registers the given AsyncListener with the most recent asynchronous cycle that was started by a call to one of the ServletRequest#startAsync methods.
The given AsyncListener will receive an AsyncEvent when the asynchronous cycle completes successfully, times out, or results in an error."
It doesn't talk about what happens if ac.start() is called again.

I have updated the explanation to make it clear.

thank you for your feedback!
Paul.

Re: About Question enthuware.jwpv6.2.594 :

Posted: Sun May 01, 2016 9:56 am
by himaiMinh
Hi, admin, thanks for your reply and update.
But in this code:

Code: Select all

AsyncContext ac=request.startAsync(); // line 1
//onStartAsync is not called here by the container
ac.addListener(myAsyncListener);  //line 2
//onStartAsync is not called here as the async process has already started.
onStartAsync method should be called after line 2 (after the AsyncContext object has been created in line 1).
But in this moment, the async process has already started and calling onStartAsync is never invoked by the container.

The container does not call the onStartAsync after line 1 and before line 2.

Re: About Question enthuware.jwpv6.2.594 :

Posted: Sun May 01, 2016 10:19 am
by admin
Yes, you are right. I am just saying that the API description doesn't make it clear.
-Paul.