理解异步Servlet之前,让我们试着理解为什么需要它。假设我们有一个Servlet需要很多的时间来处理,类似下面的内容:
LongRunningServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
packagecom.journaldev.servlet;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
@WebServlet("/LongRunningServlet")
publicclassLongRunningServletextendsHttpServlet {
privatestaticfinallongserialVersionUID = 1L;
protectedvoiddoGet(HttpServletRequest request,
HttpServletResponse response)throwsServletException, IOException {
longstartTime = System.currentTimeMillis();
System.out.println("LongRunningServlet Start::Name="
+ Thread.currentThread().getName() +"::ID="
+ Thread.currentThread().getId());
String time = request.getParameter("time");
intsecs = Integer.valueOf(time);
// max 10 seconds
if(secs >10000)
secs =10000;
longProcessing(secs);
PrintWriter out = response.getWriter();
longendTime = System.currentTimeMillis();
out.write("Processing done for "+ secs +" milliseconds!!");
System.out.println("LongRunningServlet Start::Name="
+ Thread.currentThread().getName() +"::ID="
+ Thread.currentThread().getId() +"::Time Taken="
+ (endTime - startTime) +" ms.");
}
privatevoidlongProcessing(intsecs) {
// wait for given time before finishing
try{
Thread.sleep(secs);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
|
如果我们的URL是:http://localhost:8080/AsyncServletExample/LongRunningServlet?time=8000
得到响应为“Processing done for 8000 milliseconds! !“。现在,如果你会查看服务器日志,会得到以下记录:
1
2
|
LongRunningServlet Start::Name=http-bio-8080-exec-34::ID=103
LongRunningServlet Start::Name=http-bio-8080-exec-34::ID=103::Time Taken=8002 ms.
|
所以Servlet线程实际运行超过 8秒,尽管大多数时间用来处理其它Servlet请求或响应。
这可能导致线程饥饿——因为我们的Servlet线程被阻塞,直到所有的处理完成。如果服务器的请求得到了很多过程,它将达到最大Servlet线程限制和进一步的请求会拒绝连接错误。
Servlet 3.0之前,这些长期运行的线程容器特定的解决方案,我们可以产生一个单独的工作线程完成耗时的任务,然后返回响应客户。Servlet线程返回Servlet池后启动工作线程。Tomcat 的 Comet、WebLogic FutureResponseServlet 和 WebSphere Asynchronous Request Dispatcher都是实现异步处理的很好示例。
容器特定解决方案的问题在于,在不改变应用程序代码时不能移动到其他Servlet容器。这就是为什么在Servlet3.0提供标准的方式异步处理Servlet的同时增加异步Servlet支持。
实现异步Servlet
让我们看看步骤来实现异步Servlet,然后我们将提供异步支持Servlet上面的例子:
一旦我们将完成我们的项目对于异步Servlet示例,项目结构看起来会像下面的图片:
在监听中初始化线程池
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
packagecom.journaldev.servlet.async;
importjava.util.concurrent.ArrayBlockingQueue;
importjava.util.concurrent.ThreadPoolExecutor;
importjava.util.concurrent.TimeUnit;
importjavax.servlet.ServletContextEvent;
importjavax.servlet.ServletContextListener;
importjavax.servlet.annotation.WebListener;
@WebListener
publicclassAppContextListenerimplementsServletContextListener {
publicvoidcontextInitialized(ServletContextEvent servletContextEvent) {
// create the thread pool
ThreadPoolExecutor executor =newThreadPoolExecutor(100,200, 50000L,
TimeUnit.MILLISECONDS,newArrayBlockingQueue<Runnable>(100));
servletContextEvent.getServletContext().setAttribute("executor",
executor);
}
publicvoidcontextDestroyed(ServletContextEvent servletContextEvent) {
ThreadPoolExecutor executor = (ThreadPoolExecutor) servletContextEvent
.getServletContext().getAttribute("executor");
executor.shutdown();
}
}
|
实现很直接,如果你不熟悉ThreadPoolExecutor 框架请读线程池的ThreadPoolExecutor 。关于listeners 的更多细节,请阅读教程Servlet Listener。
工作线程实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
packagecom.journaldev.servlet.async;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.AsyncContext;
publicclassAsyncRequestProcessorimplementsRunnable {
privateAsyncContext asyncContext;
privateintsecs;
publicAsyncRequestProcessor() {
}
publicAsyncRequestProcessor(AsyncContext asyncCtx,intsecs) {
this.asyncContext = asyncCtx;
this.secs = secs;
}
@Override
publicvoidrun() {
System.out.println("Async Supported? "
+ asyncContext.getRequest().isAsyncSupported());
longProcessing(secs);
try{
PrintWriter out = asyncContext.getResponse().getWriter();
out.write("Processing done for "+ secs +" milliseconds!!");
}catch(IOException e) {
e.printStackTrace();
}
//complete the processing
asyncContext.complete();
}
privatevoidlongProcessing(intsecs) {
// wait for given time before finishing
try{
Thread.sleep(secs);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
|
注意:在请求和响应时使用AsyncContext对象,然后在完成时调用 asyncContext.complete() 方法。
AsyncListener 实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
packagecom.journaldev.servlet.async;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.AsyncEvent;
importjavax.servlet.AsyncListener;
importjavax.servlet.ServletResponse;
importjavax.servlet.annotation.WebListener;
@WebListener
publicclassAppAsyncListenerimplementsAsyncListener {
@Override
publicvoidonComplete(AsyncEvent asyncEvent)throwsIOException {
System.out.println("AppAsyncListener onComplete");
// we can do resource cleanup activity here
}
@Override
publicvoidonError(AsyncEvent asyncEvent)throwsIOException {
System.out.println("AppAsyncListener onError");
//we can return error response to client
}
@Override
publicvoidonStartAsync(AsyncEvent asyncEvent)throwsIOException {
System.out.println("AppAsyncListener onStartAsync");
//we can log the event here
}
@Override
publicvoidonTimeout(AsyncEvent asyncEvent)throwsIOException {
System.out.println("AppAsyncListener onTimeout");
//we can send appropriate response to client
ServletResponse response = asyncEvent.getAsyncContext().getResponse();
PrintWriter out = response.getWriter();
out.write("TimeOut Error in Processing");
}
}
|
通知的实现在 Timeout()方法,通过它发送超时响应给客户端。
Async Servlet 实现
这是我们的异步Servlet实现,注意使用AsyncContext和ThreadPoolExecutor进行处理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
packagecom.journaldev.servlet.async;
importjava.io.IOException;
importjava.util.concurrent.ThreadPoolExecutor;
importjavax.servlet.AsyncContext;
importjavax.servlet.ServletException;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns ="/AsyncLongRunningServlet", asyncSupported =true)
publicclassAsyncLongRunningServletextendsHttpServlet {
privatestaticfinallongserialVersionUID = 1L;
protectedvoiddoGet(HttpServletRequest request,
HttpServletResponse response)throwsServletException, IOException {
longstartTime = System.currentTimeMillis();
System.out.println("AsyncLongRunningServlet Start::Name="
+ Thread.currentThread().getName() +"::ID="
+ Thread.currentThread().getId());
request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED",true);
String time = request.getParameter("time");
intsecs = Integer.valueOf(time);
// max 10 seconds
if(secs >10000)
secs =10000;
AsyncContext asyncCtx = request.startAsync();
asyncCtx.addListener(newAppAsyncListener());
asyncCtx.setTimeout(9000);
ThreadPoolExecutor executor = (ThreadPoolExecutor) request
.getServletContext().getAttribute("executor");
executor.execute(newAsyncRequestProcessor(asyncCtx, secs));
longendTime = System.currentTimeMillis();
System.out.println("AsyncLongRunningServlet End::Name="
+ Thread.currentThread().getName() +"::ID="
+ Thread.currentThread().getId() +"::Time Taken="
+ (endTime - startTime) +" ms.");
}
}
|
Run Async Servlet
现在,当我们将上面运行servlet URL:
http://localhost:8080/AsyncServletExample/AsyncLongRunningServlet?time=8000
得到响应和日志:
1
2
3
4
|
AsyncLongRunningServlet Start::Name=http-bio-8080-exec-50::ID=124
AsyncLongRunningServlet End::Name=http-bio-8080-exec-50::ID=124::Time Taken=1 ms.
Async Supported?true
AppAsyncListener onComplete
|
如果运行时设置time=9999,在客户端超时以后会得到响应超时错误处理和日志:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
AsyncLongRunningServlet Start::Name=http-bio-8080-exec-44::ID=117
AsyncLongRunningServlet End::Name=http-bio-8080-exec-44::ID=117::Time Taken=1 ms.
Async Supported?true
AppAsyncListener onTimeout
AppAsyncListener onError
AppAsyncListener onComplete
Exceptioninthread"pool-5-thread-6"java.lang.IllegalStateException: The request associated with the AsyncContext has already completed processing.
at org.apache.catalina.core.AsyncContextImpl.check(AsyncContextImpl.java:439)
at org.apache.catalina.core.AsyncContextImpl.getResponse(AsyncContextImpl.java:197)
at com.journaldev.servlet.async.AsyncRequestProcessor.run(AsyncRequestProcessor.java:27)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:680)
|
注意:Servlet线程执行完,很快就和所有主要的处理工作是发生在其他线程。