您好,登錄后才能下訂單哦!
J2ee 如何實(shí)現(xiàn)監(jiān)聽高并發(fā)?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
J2ee 高并發(fā)情況下監(jiān)聽器實(shí)例詳解
引言:在高并發(fā)下限制最大并發(fā)次數(shù),在web.xml中用過濾器設(shè)置參數(shù)(最大并發(fā)數(shù)),并設(shè)置其他相關(guān)參數(shù)。詳細(xì)見代碼。
第一步:配置web.xml配置,不懂的地方解釋一下:參數(shù)50通過參數(shù)名maxConcurrent用在filter的實(shí)現(xiàn)類中獲取,filter-class就是寫的實(shí)現(xiàn)類,
url-pattern就是限制并發(fā)時(shí)間的url,結(jié)束!
<filter> <filter-name>ConcurrentCountFilter</filter-name> <filter-class>com.procure.pass.ConcurrentCountFilter</filter-class> <init-param> <param-name>maxConcurrent</param-name> <param-value>50</param-value> </init-param> </filter> <filter-mapping> <filter-name>ConcurrentCountFilter</filter-name> <url-pattern>/a/pass/export</url-pattern> </filter-mapping>
第二步:寫實(shí)現(xiàn)類實(shí)現(xiàn)filter,該接口有三個(gè)方法,詳見代碼。
import java.io.IOException; import java.util.concurrent.atomic.AtomicInteger; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Servlet Filter implementation class ConcurrentCountFilter */ public class ConcurrentCountFilter implements Filter { private static Logger log = LoggerFactory.getLogger(ConcurrentCountFilter.class); private FilterConfig filterConfig; private int maxConcurrent = -1; //總計(jì)數(shù) private static AtomicInteger count = new AtomicInteger(0); /** * 獲取當(dāng)前并發(fā)數(shù) * @return */ public static int get(){ return count.get(); } /** * 增加并發(fā)數(shù)量 * @return */ public static int increase(){ return count.incrementAndGet(); } /** * 減少并發(fā)數(shù)量 * @return */ public static int decrement(){ return count.decrementAndGet(); } /** * 初始化 */ public void init(FilterConfig filterConfig) throws ServletException { //獲取配置的最大并發(fā)數(shù)量 String maxStr = filterConfig.getInitParameter("maxConcurrent"); int num = -1; if(maxStr != null && !"".equals(maxStr)){ num = Integer.parseInt(maxStr); } if(num >= 1){ this.maxConcurrent = num; }else{ this.maxConcurrent = -1; } } /** * 過濾主方法 */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try{ //增加并發(fā)數(shù)量 int num = increase(); if(maxConcurrent > 0){ if(maxConcurrent >= num){ chain.doFilter(request, response); log.info("第一次并發(fā)數(shù)量:"+count.get()); }else{ HttpServletResponse res = (HttpServletResponse) response; res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,"達(dá)到最大并發(fā)數(shù)限制"); log.info("達(dá)到最大并發(fā)數(shù)"); log.info("最大并發(fā)數(shù)量:"+count.get()); } }else{ chain.doFilter(request, response); log.info("第二次并發(fā)數(shù)量:"+count.get()); } }finally { decrement(); log.info("減小的并發(fā)量:"+count.get()); } } /** * 退出銷毀 */ public void destroy() { this.filterConfig = null; log.info("銷毀......"); } }
代碼到此完。
吐槽一下自己在項(xiàng)目中遇到的坑:
1.response.sendError( int, string);在本文代碼中為res.sendError其中若直接如本文代碼那樣會返回一個(gè)503服務(wù)器帶出來的頁面,此頁面粗暴及其難看,
在此為了友好通知用戶,需做如下步驟,在web.xml中做如下配置代碼:
<error-page> <error-code>503</error-code> <location>/WEB-INF/views/error/503.jsp</location> </error-page>
如果在web.xml中配置了上面信息,首先會過濾503(HttpServletResponse.SC_SERVICE_UNAVAILABLE)狀態(tài)碼下的此頁面而不會拋服務(wù)器的頁面。
其中503.jsp頁面需自己完成在此僅僅貼出來一個(gè)示例做參考,代碼如下:
<% response.setStatus(503); // 獲取異常類 Throwable ex = Exceptions.getThrowable(request); if (ex != null){ LoggerFactory.getLogger("500.jsp").error(ex.getMessage(), ex); } // 編譯錯(cuò)誤信息 StringBuilder sb = new StringBuilder("錯(cuò)誤信息:\n"); if (ex != null) { sb.append(Exceptions.getStackTraceAsString(ex)); } else { sb.append("未知錯(cuò)誤.\n\n"); } // 如果是異步請求或是手機(jī)端,則直接返回信息 if (Servlets.isAjaxRequest(request)) { out.print(sb); } // 輸出異常信息頁面 else { %> <%@page import="org.slf4j.Logger,org.slf4j.LoggerFactory"%> <%@page import="com.xahl_oa.internal.common.web.Servlets"%> <%@page import="com.xahl_oa.internal.common.utils.Exceptions"%> <%@page import="com.xahl_oa.internal.common.utils.StringUtils"%> <%@page contentType="text/html;charset=UTF-8" isErrorPage="true"%> <%@include file="/WEB-INF/views/include/taglib.jsp"%> <!DOCTYPE html> <html> <head> <title>503 - 服務(wù)暫時(shí)不可用</title> <%@include file="/WEB-INF/views/include/head.jsp" %> </head> <body> <div class="container-fluid"> <div class="page-header"><h2>服務(wù)暫時(shí)不可用請稍后再試.</h2></div> <div class="errorMessage"> 錯(cuò)誤信息:<%=ex==null?"未知錯(cuò)誤.":StringUtils.toHtml(ex.getMessage())%> <br/> <br/> 服務(wù)器暫時(shí)不可用請稍后再試,謝謝!<br/> <br/> <a href="javascript:" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="history.go(-1);" class="btn">返回上一頁</a> <a href="javascript:" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="$('.errorMessage').toggle();" class="btn">查看詳細(xì)信息</a> </div> <div class="errorMessage hide"> <%=StringUtils.toHtml(sb.toString())%> <br/> <a href="javascript:" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="history.go(-1);" class="btn">返回上一頁</a> <a href="javascript:" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="$('.errorMessage').toggle();" class="btn">隱藏詳細(xì)信息</a> <br/> <br/> </div> <script>try{top.$.jBox.closeTip();}catch(e){}</script> </div> </body> </html> <% } out = pageContext.pushBody(); %>
看完上述內(nèi)容,你們掌握J(rèn)2ee 如何實(shí)現(xiàn)監(jiān)聽高并發(fā)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。