在spring 3.2 及以后版本中增加了對請求的異步處理,旨在提高請求的處理速度降低服務(wù)性能消耗。
在我們的請求中做了耗時處理,當(dāng)并發(fā)請求的情況下,為了避免web server的連接池被長期占用而引起性能問題,調(diào)用后生成一個非web的服務(wù)線程來處理,增加web服務(wù)器的吞吐量。
為此 Servlet 3.0 新增了請求的異步處理,Spring 也在此基礎(chǔ)上做了封裝處理。
本文還是以代碼例子的方式說明如何在 Spring Boot 中應(yīng)用異步請求。
首先說一下幾個要點:
1、@WebFilter 和 @WebServlet 注解中的 asyncSupported = true 屬性
異步處理的servlet若存在過濾器,則過濾器的注解@WebFilter應(yīng)設(shè)置asyncSupported=true,
否則會報錯 A filter or servlet of the current chain does not support asynchronous operations.
2、@EnableAsync 注解
Spring Boot 默認(rèn)添加了一些攔截 /* 的過濾器,因為 /* 會攔截所有請求,按理說我們也要設(shè)置 asyncSupported=true 屬性。因為這些過濾器都是 Spring Boot 初始化的,所以它提供了 @EnableAsync 注解來統(tǒng)一配置,該注解只針對 “非 @WebFilter 和 @WebServlet 注解的有效”,所以我們自己定義的 Filter 還是需要自己配置 asyncSupported=true 的。
3、AsyncContext 對象
獲取一個異步請求的上下文對象。
4、asyncContext.setTimeout(20 * 1000L);
我們不能讓異步請求無限的等待下去,通過 setTimeout 來設(shè)定最大超時時間。
下面通過兩種方式來測試異步任務(wù):
先在 SpringBootSampleApplication 上添加 @EnableAsync 注解。
再檢查所有自定義的Filter,如存在如下兩種情況需要配置 asyncSupported=true
1) 自定義Filter 攔截了 /*
2) 某Filter 攔截了 /shanhy/* ,我們需要執(zhí)行的異步請求的 Servlet 為 /shanhy/testcomet
方法一:原生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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
package org.springboot.sample.servlet; import java.io.IOException; import java.util.Queue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import javax.servlet.AsyncContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * HTTP長連接實現(xiàn) * * @author 單紅宇(365384722) * @myblog http://blog.csdn.net/catoop/ * @create 2016年3月29日 */ @WebServlet (urlPatterns = "/xs/cometservlet" , asyncSupported = true ) //異步處理的servlet若存在過濾器,則過濾器的注解@WebFilter應(yīng)設(shè)置asyncSupported=true, //否則會報錯A filter or servlet of the current chain does not support asynchronous operations. public class CometServlet extends HttpServlet { private static final long serialVersionUID = -8685285401859800066L; private final Queue<AsyncContext> asyncContexts = new LinkedBlockingQueue<>(); private final Thread generator = new Thread( "Async Event generator" ) { @Override public void run() { while (!generator.isInterrupted()) { // 線程有效 try { while (!asyncContexts.isEmpty()) { // 不為空 TimeUnit.SECONDS.sleep( 10 ); // 秒,模擬耗時操作 AsyncContext asyncContext = asyncContexts.poll(); HttpServletResponse res = (HttpServletResponse) asyncContext.getResponse(); res.getWriter().write( "{\"result\":\"OK - " +System.currentTimeMillis()+ "\"}" ); res.setStatus(HttpServletResponse.SC_OK); res.setContentType( "application/json" ); asyncContext.complete(); // 完成 } } catch (InterruptedException e) { Thread.currentThread().interrupt(); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }; @Override public void init() throws ServletException { super .init(); generator.start(); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println( ">>>>>>>>>>CometServlet Request<<<<<<<<<<<" ); doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { AsyncContext asyncContext = req.startAsync(); asyncContext.setTimeout( 20 * 1000L); asyncContexts.offer(asyncContext); } @Override public void destroy() { super .destroy(); generator.interrupt(); } } |
方法二:Controller 方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Controller public class PageController { @RequestMapping ( "/async/test" ) @ResponseBody public Callable<String> callable() { // 這么做的好處避免web server的連接池被長期占用而引起性能問題, // 調(diào)用后生成一個非web的服務(wù)線程來處理,增加web服務(wù)器的吞吐量。 return new Callable<String>() { @Override public String call() throws Exception { Thread.sleep( 3 * 1000L); return "小單 - " + System.currentTimeMillis(); } }; } } |
最后寫一個comet.jsp頁面測試:
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
|
<%@ page pageEncoding= "UTF-8" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > <html> <head> <title>長連接測試</title> <script type= "text/javascript" src= "${pageContext.request.contextPath }/webjarslocator/jquery/jquery.js" ></script> <script type= "text/javascript" > $(function(){ function longPolling(){ $.getJSON( '${pageContext.request.contextPath }/xs/cometservlet' , function(data){ console.log(data.result); $( '#n1' ).html(data.result); longPolling(); }); } longPolling(); function longPolling2(){ $.get( '${pageContext.request.contextPath }/async/test' , function(data){ console.log(data); $( '#n2' ).html(data); longPolling2(); }); } longPolling2(); }); </script> </head> <body> <h1>長連接測試</h1> <h2 id= "n1" ></h2> <h2 id= "n2" ></h2> </body> </html> |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/catoop/article/details/51034866