Servlet 3特性:异步Servlet

理解异步Servlet以前,让咱们试着理解为何须要它。假设咱们有一个Servlet须要不少的时间来处理,相似下面的内容: html

LongRunningServlet.java 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 apache

获得响应为“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请求或响应。 session

这可能致使线程饥饿——由于咱们的Servlet线程被阻塞,直到全部的处理完成。若是服务器的请求获得了不少过程,它将达到最大Servlet线程限制和进一步的请求会拒绝链接错误。 app

Servlet 3.0以前,这些长期运行的线程容器特定的解决方案,咱们能够产生一个单独的工做线程完成耗时的任务,而后返回响应客户。Servlet线程返回Servlet池后启动工做线程。Tomcat 的 Comet、WebLogic FutureResponseServlet 和 WebSphere Asynchronous Request Dispatcher都是实现异步处理的很好示例。 框架

容器特定解决方案的问题在于,在不改变应用程序代码时不能移动到其余Servlet容器。这就是为何在Servlet3.0提供标准的方式异步处理Servlet的同时增长异步Servlet支持。 异步

实现异步Servlet async

让咱们看看步骤来实现异步Servlet,而后咱们将提供异步支持Servlet上面的例子: ide

  1. 首先Servlet,咱们提供异步支持 Annotation @WebServlet  的属性asyncSupported 值为true。
  2. 因为实际实现委托给另外一个线程,咱们应该有一个线程池实现。咱们能够一个经过Executors framework 建立线程池和使用servlet context listener来初始化线程池。
  3. 经过ServletRequest.startAsync方法获取AsyncContext的实例。AsyncContext提供方法让ServletRequest和ServletResponse对象引用。它还提供了使用调度方法将请求转发到另外一个 dispatch() 方法。
  4. 编写一个可运行的实现,咱们将进行重处理,而后使用AsyncContext对象发送请求到另外一个资源或使用ServletResponse编写响应对象。一旦处理完成,咱们经过AsyncContext.complete()方法通知容器异步处理完成。
  5. 添加AsyncListener实现AsyncContext对象实现回调方法,咱们可使用它来提供错误响应客户端装进箱的错误或超时,而异步线程处理。在这里咱们也能够作一些清理工做。

一旦咱们将完成咱们的项目对于异步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线程执行完,很快就和全部主要的处理工做是发生在其余线程。

相关文章
相关标签/搜索