J2ee高并发情况下监听器
引言:在高并发下限制最大并发次数,在web.xml中用过滤器设置參数(最大并发数),并设置其它相关參数。具体见代码。
第一步:配置web.xml配置,不懂的地方解释一下:參数50通过參数名maxConcurrent用在filter的实现类中获取,filter-class就是写的实现类,
url-pattern就是限制并发时间的url。结束!
<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>
第二步:写实现类实现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; //总计数 private static AtomicInteger count = new AtomicInteger(0); /** * 获取当前并发数 * @return */ public static int get(){ return count.get(); } /** * 添加并发数量 * @return */ public static int increase(){ return count.incrementAndGet(); } /** * 降低并发数量 * @return */ public static int decrement(){ return count.decrementAndGet(); } /** * 初始化 */ public void init(FilterConfig filterConfig) throws ServletException { //获取配置的最大并发数量 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{ //添加并发数量 int num = increase(); if(maxConcurrent > 0){ if(maxConcurrent >= num){ chain.doFilter(request, response); log.info("第一次并发数量:"+count.get()); }else{ HttpServletResponse res = (HttpServletResponse) response; res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,"达到最大并发数限制"); log.info("达到最大并发数"); log.info("最大并发数量:"+count.get()); } }else{ chain.doFilter(request, response); log.info("第二次并发数量:"+count.get()); } }finally { decrement(); log.info("减小的并发量:"+count.get()); } } /** * 退出销毁 */ public void destroy() { this.filterConfig = null; log.info("销毁......"); } }
代码到此完。
吐槽一下自己在项目中遇到的坑:
1.response.sendError( int, string);在本文代码中为res.sendError当中若直接如本文代码那样会返回一个503server带出来的页面,此页面粗暴及其难看。
在此为了友好通知用户,需做例如以下步骤。在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)状态码下的此页面而不会抛server的页面。
当中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 - 服务临时不可用</title> <%@include file="/WEB-INF/views/include/head.jsp" %> </head> <body> <div class="container-fluid"> <div class="page-header"><h1>服务临时不可用请稍后再试.</h1></div> <div class="errorMessage"> 错误信息:<%=ex==null?"未知错误.":StringUtils.toHtml(ex.getMessage())%> <br/> <br/> server临时不可用请稍后再试,谢谢。<br/> <br/> <a href="javascript:" onclick="history.go(-1);" class="btn">返回上一页</a> <a href="javascript:" onclick="$('.errorMessage').toggle();" class="btn">查看具体信息</a> </div> <div class="errorMessage hide"> <%=StringUtils.toHtml(sb.toString())%> <br/> <a href="javascript:" onclick="history.go(-1);" class="btn">返回上一页</a> <a href="javascript:" onclick="$('.errorMessage').toggle();" class="btn">隐藏具体信息</a> <br/> <br/> </div> <script>try{top.$.jBox.closeTip();}catch(e){}</script> </div> </body> </html> <% } out = pageContext.pushBody(); %>
此页面就比server抛出的页面友好甚多。
本文借鉴自:http://blog.csdn.net/zyb134506/article/details/41692893 在此谢谢此篇博客。
posted on 2017-08-19 18:19 cynchanpin 阅读(377) 评论(0) 编辑 收藏 举报