All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.
@WebListener
public class MyListener implements AsyncListener {
...
public void onStartAsync(AsyncEvent event){
System.out.println("Async Listener on start");
}
...
}
@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
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.
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.