源头
package jane. servlet;
import java. io. IOException;
import javax. servlet. AsyncContext;
import javax. servlet. ServletException;
import javax. servlet. ServletResponse;
import javax. servlet. annotation. WebServlet;
import javax. servlet. http. HttpServlet;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
@WebServlet ( value = "/Async" , asyncSupported = true )
public class HelloAsyncServlet extends HttpServlet
{
@Override
protected void doGet ( HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
System. out. println ( "主线程开始" + Thread. currentThread ( ) ) ;
AsyncContext startAsync = req. startAsync ( ) ;
startAsync. start ( new Runnable ( )
{
@Override
public void run ( )
{
try
{
System. out. println ( "子线程开始" + Thread. currentThread ( ) ) ;
Thread. sleep ( 2000 ) ;
startAsync. complete ( ) ;
AsyncContext asyncContext = req. getAsyncContext ( ) ;
ServletResponse response = asyncContext. getResponse ( ) ;
response. getWriter ( ) . write ( "Hello World Asunc" ) ;
System. out. println ( "子线程结束" + Thread. currentThread ( ) ) ;
} catch ( Exception e)
{
System. out. println ( "异常" ) ;
e. printStackTrace ( ) ;
}
}
} ) ;
System. out. println ( "主线程结束" + Thread. currentThread ( ) ) ;
}
}
错误
java. lang. IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode ( i. e. isAsyncStarted ( ) returns false )
at org. apache. catalina. connector. Request. getAsyncContext ( Request. java: 1783 )
at org. apache. catalina. connector. RequestFacade. getAsyncContext ( RequestFacade. java: 1068 )
at com. atguigu. servlet. HelloAsyncServlet$1. run ( HelloAsyncServlet. java: 32 )
at org. apache. catalina. core. AsyncContextImpl$RunnableWrapper. run ( AsyncContextImpl. java: 546 )
at java. util. concurrent. ThreadPoolExecutor. runWorker ( ThreadPoolExecutor. java: 1142 )
at java. util. concurrent. ThreadPoolExecutor$Worker. run ( ThreadPoolExecutor. java: 617 )
at org. apache. tomcat. util. threads. TaskThread$WrappingRunnable. run ( TaskThread. java: 61 )
at java. lang. Thread. run ( Thread. java: 745 )
解决
只是startAsync. complete ( ) ; 这个方法调用的时机错误了,
这个方法表示异步处理完毕
应该在子线程完全处理完成业务逻辑的最后调用, 就是连响应都响应完了才调用
package jane. servlet;
import java. io. IOException;
import javax. servlet. AsyncContext;
import javax. servlet. ServletException;
import javax. servlet. ServletResponse;
import javax. servlet. annotation. WebServlet;
import javax. servlet. http. HttpServlet;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
@WebServlet ( value = "/Async" , asyncSupported = true )
public class HelloAsyncServlet extends HttpServlet
{
@Override
protected void doGet ( HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
System. out. println ( "主线程开始" + Thread. currentThread ( ) ) ;
AsyncContext startAsync = req. startAsync ( ) ;
startAsync. start ( new Runnable ( )
{
@Override
public void run ( )
{
try
{
System. out. println ( "子线程开始" + Thread. currentThread ( ) ) ;
Thread. sleep ( 2000 ) ;
AsyncContext asyncContext = req. getAsyncContext ( ) ;
ServletResponse response = asyncContext. getResponse ( ) ;
response. getWriter ( ) . write ( "Hello World Asunc" ) ;
startAsync. complete ( ) ;
System. out. println ( "子线程结束" + Thread. currentThread ( ) ) ;
} catch ( Exception e)
{
System. out. println ( "异常" ) ;
e. printStackTrace ( ) ;
}
}
} ) ;
System. out. println ( "主线程结束" + Thread. currentThread ( ) ) ;
}
}