在Servlet 3.0之前,Servlet采用Thread-Per-Request的方式處理請求,即每一次Http請求都由某一個線程從頭到尾負(fù)責(zé)處理。如果一個請求需要進(jìn)行IO操作,比如訪問數(shù)據(jù)庫、調(diào)用第三方服務(wù)接口等,那么其所對應(yīng)的線程將同步地等待IO操作完成, 而IO操作是非常慢的,所以此時的線程并不能及時地釋放回線程池以供后續(xù)使用,在并發(fā)量越來越大的情況下,這將帶來嚴(yán)重的性能問題。即便是像Spring、Struts這樣的高層框架也脫離不了這樣的桎梏,因為他們都是建立在Servlet之上的。為了解決這樣的問題,Servlet 3.0引入了異步處理,然后在Servlet 3.1中又引入了非阻塞IO來進(jìn)一步增強(qiáng)異步處理的性能。
本文源代碼:https://github.com/davenkin/servlet-3-async-learning
項目下載地址:servlet-3-async-learning.rar
在Servlet 3.0中,我們可以從HttpServletRequest對象中獲得一個AsyncContext對象,該對象構(gòu)成了異步處理的上下文,Request和Response對象都可從中獲取。AsyncContext可以從當(dāng)前線程傳給另外的線程,并在新的線程中完成對請求的處理并返回結(jié)果給客戶端,初始線程便可以還回給容器線程池以處理更多的請求。如此,通過將請求從一個線程傳給另一個線程處理的過程便構(gòu)成了Servlet 3.0中的異步處理。
舉個例子,對于一個需要完成長時處理的Servlet來說,其實現(xiàn)通常為:
1
2
3
4
5
6
7
8
9
|
@WebServlet ( "/syncHello" ) public class SyncHelloServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { new LongRunningProcess().run(); response.getWriter().write( "Hello World!" ); } } |
為了模擬長時處理過程,我們創(chuàng)建了一個LongRunningProcess類,其run()方法將隨機(jī)地等待2秒之內(nèi)的一個時間:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class LongRunningProcess { public void run() { try { int millis = ThreadLocalRandom.current().nextInt( 2000 ); String currentThread = Thread.currentThread().getName(); System.out.println(currentThread + " sleep for " + millis + " milliseconds." ); Thread.sleep(millis); } catch (InterruptedException e) { e.printStackTrace(); } } } |
此時的SyncHelloServlet將順序地先執(zhí)行LongRunningProcess的run()方法,然后將將HelloWorld返回給客戶端,這是一個典型的同步過程。
在Servlet 3.0中,我們可以這么寫來達(dá)到異步處理:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@WebServlet (value = "/simpleAsync" , asyncSupported = true ) public class SimpleAsyncHelloServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { AsyncContext asyncContext = request.startAsync(); asyncContext.start(() -> { new LongRunningProcess().run(); try { asyncContext.getResponse().getWriter().write( "Hello World!" ); } catch (IOException e) { e.printStackTrace(); } asyncContext.complete(); }); } |
此時,我們先通過request.startAsync()獲取到該請求對應(yīng)的AsyncContext,然后調(diào)用AsyncContext的start()方法進(jìn)行異步處理,處理完畢后需要調(diào)用complete()方法告知Servlet容器。start()方法會向Servlet容器另外申請一個新的線程(可以是從Servlet容器中已有的主線程池獲取,也可以另外維護(hù)一個線程池,不同容器實現(xiàn)可能不一樣),然后在這個新的線程中繼續(xù)處理請求,而原先的線程將被回收到主線程池中。事實上,這種方式對性能的改進(jìn)不大,因為如果新的線程和初始線程共享同一個線程池的話,相當(dāng)于閑置下了一個線程,但同時又占用了另一個線程。
當(dāng)然,除了調(diào)用AsyncContext的start()方法,我們還可以通過手動創(chuàng)建線程的方式來實現(xiàn)異步處理:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@WebServlet (value = "/newThreadAsync" , asyncSupported = true ) public class NewThreadAsyncHelloServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { AsyncContext asyncContext = request.startAsync(); Runnable runnable = () -> { new LongRunningProcess().run(); try { asyncContext.getResponse().getWriter().write( "Hello World!" ); } catch (IOException e) { e.printStackTrace(); } asyncContext.complete(); }; new Thread(runnable).start(); } } |
自己手動創(chuàng)建新線程一般是不被鼓勵的,并且此時線程不能重用。因此,一種更好的辦法是我們自己維護(hù)一個線程池。這個線程池不同于Servlet容器的主線程池,如下圖:
在上圖中,用戶發(fā)起的請求首先交由Servlet容器主線程池中的線程處理,在該線程中,我們獲取到AsyncContext,然后將其交給異步處理線程池??梢酝ㄟ^Java提供的Executor框架來創(chuàng)建線程池:
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
|
@WebServlet (value = "/threadPoolAsync" , asyncSupported = true ) public class ThreadPoolAsyncHelloServlet extends HttpServlet { private static ThreadPoolExecutor executor = new ThreadPoolExecutor( 100 , 200 , 50000L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>( 100 )); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { AsyncContext asyncContext = request.startAsync(); executor.execute(() -> { new LongRunningProcess().run(); try { asyncContext.getResponse().getWriter().write( "Hello World!" ); } catch (IOException e) { e.printStackTrace(); } asyncContext.complete(); }); } } |
Servlet 3.0對請求的處理雖然是異步的,但是對InputStream和OutputStream的IO操作卻依然是阻塞的,對于數(shù)據(jù)量大的請求體或者返回體,阻塞IO也將導(dǎo)致不必要的等待。因此在Servlet 3.1中引入了非阻塞IO(參考下圖紅框內(nèi)容),通過在HttpServletRequest和HttpServletResponse中分別添加ReadListener和WriterListener方式,只有在IO數(shù)據(jù)滿足一定條件時(比如數(shù)據(jù)準(zhǔn)備好時),才進(jìn)行后續(xù)的操作。
對應(yīng)的代碼示:
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
|
@WebServlet (value = "/nonBlockingThreadPoolAsync" , asyncSupported = true ) public class NonBlockingAsyncHelloServlet extends HttpServlet { private static ThreadPoolExecutor executor = new ThreadPoolExecutor( 100 , 200 , 50000L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>( 100 )); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { AsyncContext asyncContext = request.startAsync(); ServletInputStream inputStream = request.getInputStream(); inputStream.setReadListener( new ReadListener() { @Override public void onDataAvailable() throws IOException { } @Override public void onAllDataRead() throws IOException { executor.execute(() -> { new LongRunningProcess().run(); try { asyncContext.getResponse().getWriter().write( "Hello World!" ); } catch (IOException e) { e.printStackTrace(); } asyncContext.complete(); }); } @Override public void onError(Throwable t) { asyncContext.complete(); } }); } } |
在上例中,我們?yōu)镾ervletInputStream添加了一個ReadListener,并在ReadListener的onAllDataRead()方法中完成了長時處理過程。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.jianshu.com/p/05a57d00d5cb