J2ee 高并發(fā)情況下監(jiān)聽器實例詳解
J2ee 高并發(fā)情況下監(jiān)聽器實例詳解
引言:在高并發(fā)下限制最大并發(fā)次數(shù),在web.xml中用過濾器設(shè)置參數(shù)(最大并發(fā)數(shù)),并設(shè)置其他相關(guān)參數(shù)。詳細見代碼。
第一步:配置web.xml配置,不懂的地方解釋一下:參數(shù)50通過參數(shù)名maxConcurrent用在filter的實現(xiàn)類中獲取,filter-class就是寫的實現(xiàn)類,
url-pattern就是限制并發(fā)時間的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>
第二步:寫實現(xiàn)類實現(xiàn)filter,該接口有三個方法,詳見代碼。
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;
//總計數(shù)
private static AtomicInteger count = new AtomicInteger(0);
/**
* 獲取當前并發(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,"達到最大并發(fā)數(shù)限制");
log.info("達到最大并發(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("銷毀......");
}
}
代碼到此完。
吐槽一下自己在項目中遇到的坑:
1.response.sendError( int, string);在本文代碼中為res.sendError其中若直接如本文代碼那樣會返回一個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頁面需自己完成在此僅僅貼出來一個示例做參考,代碼如下:
<%
response.setStatus(503);
// 獲取異常類
Throwable ex = Exceptions.getThrowable(request);
if (ex != null){
LoggerFactory.getLogger("500.jsp").error(ex.getMessage(), ex);
}
// 編譯錯誤信息
StringBuilder sb = new StringBuilder("錯誤信息:\n");
if (ex != null) {
sb.append(Exceptions.getStackTraceAsString(ex));
} else {
sb.append("未知錯誤.\n\n");
}
// 如果是異步請求或是手機端,則直接返回信息
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ù)暫時不可用</title>
<%@include file="/WEB-INF/views/include/head.jsp" %>
</head>
<body>
<div class="container-fluid">
<div class="page-header"><h1>服務(wù)暫時不可用請稍后再試.</h1></div>
<div class="errorMessage">
錯誤信息:<%=ex==null?"未知錯誤.":StringUtils.toHtml(ex.getMessage())%> <br/> <br/>
服務(wù)器暫時不可用請稍后再試,謝謝!<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">查看詳細信息</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">隱藏詳細信息</a>
<br/> <br/>
</div>
<script>try{top.$.jBox.closeTip();}catch(e){}</script>
</div>
</body>
</html>
<%
} out = pageContext.pushBody();
%>
此頁面就比服務(wù)器拋出的頁面友好甚多。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
IDEA配置Maven并版本統(tǒng)一管理的實現(xiàn)
本文主要介紹了IDEA配置Maven并版本統(tǒng)一管理的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09

