J2ee 高并发情况下监听器实例详解
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,该接口有三个方法,详见代码。
importjava.io.IOException; importjava.util.concurrent.atomic.AtomicInteger; importjavax.servlet.Filter; importjavax.servlet.FilterChain; importjavax.servlet.FilterConfig; importjavax.servlet.ServletException; importjavax.servlet.ServletRequest; importjavax.servlet.ServletResponse; importjavax.servlet.http.HttpServletResponse; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; /** *ServletFilterimplementationclassConcurrentCountFilter */ publicclassConcurrentCountFilterimplementsFilter{ privatestaticLoggerlog=LoggerFactory.getLogger(ConcurrentCountFilter.class); privateFilterConfigfilterConfig; privateintmaxConcurrent=-1; //总计数 privatestaticAtomicIntegercount=newAtomicInteger(0); /** *获取当前并发数 *@return */ publicstaticintget(){ returncount.get(); } /** *增加并发数量 *@return */ publicstaticintincrease(){ returncount.incrementAndGet(); } /** *减少并发数量 *@return */ publicstaticintdecrement(){ returncount.decrementAndGet(); } /** *初始化 */ publicvoidinit(FilterConfigfilterConfig)throwsServletException{ //获取配置的最大并发数量 StringmaxStr=filterConfig.getInitParameter("maxConcurrent"); intnum=-1; if(maxStr!=null&&!"".equals(maxStr)){ num=Integer.parseInt(maxStr); } if(num>=1){ this.maxConcurrent=num; }else{ this.maxConcurrent=-1; } } /** *过滤主方法 */ publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{ try{ //增加并发数量 intnum=increase(); if(maxConcurrent>0){ if(maxConcurrent>=num){ chain.doFilter(request,response); log.info("第一次并发数量:"+count.get()); }else{ HttpServletResponseres=(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()); } } /** *退出销毁 */ publicvoiddestroy(){ this.filterConfig=null; log.info("销毁......"); } }
代码到此完。
吐槽一下自己在项目中遇到的坑:
1.response.sendError(int,string);在本文代码中为res.sendError其中若直接如本文代码那样会返回一个503服务器带出来的页面,此页面粗暴及其难看,
在此为了友好通知用户,需做如下步骤,在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)状态码下的此页面而不会抛服务器的页面。
其中503.jsp页面需自己完成在此仅仅贴出来一个示例做参考,代码如下:
<% response.setStatus(503); //获取异常类 Throwableex=Exceptions.getThrowable(request); if(ex!=null){ LoggerFactory.getLogger("500.jsp").error(ex.getMessage(),ex); } //编译错误信息 StringBuildersb=newStringBuilder("错误信息:\n"); if(ex!=null){ sb.append(Exceptions.getStackTraceAsString(ex)); }else{ sb.append("未知错误.\n\n"); } //如果是异步请求或是手机端,则直接返回信息 if(Servlets.isAjaxRequest(request)){ out.print(sb); } //输出异常信息页面 else{ %> <%@pageimport="org.slf4j.Logger,org.slf4j.LoggerFactory"%> <%@pageimport="com.xahl_oa.internal.common.web.Servlets"%> <%@pageimport="com.xahl_oa.internal.common.utils.Exceptions"%> <%@pageimport="com.xahl_oa.internal.common.utils.StringUtils"%> <%@pagecontentType="text/html;charset=UTF-8"isErrorPage="true"%> <%@includefile="/WEB-INF/views/include/taglib.jsp"%> <!DOCTYPEhtml> <html> <head> <title>503-服务暂时不可用</title> <%@includefile="/WEB-INF/views/include/head.jsp"%> </head> <body> <divclass="container-fluid"> <divclass="page-header"><h1>服务暂时不可用请稍后再试.</h1></div> <divclass="errorMessage"> 错误信息:<%=ex==null?"未知错误.":StringUtils.toHtml(ex.getMessage())%><br/><br/> 服务器暂时不可用请稍后再试,谢谢!<br/><br/> <ahref="javascript:"rel="externalnofollow"rel="externalnofollow"rel="externalnofollow"rel="externalnofollow"onclick="history.go(-1);"class="btn">返回上一页</a> <ahref="javascript:"rel="externalnofollow"rel="externalnofollow"rel="externalnofollow"rel="externalnofollow"onclick="$('.errorMessage').toggle();"class="btn">查看详细信息</a> </div> <divclass="errorMessagehide"> <%=StringUtils.toHtml(sb.toString())%><br/> <ahref="javascript:"rel="externalnofollow"rel="externalnofollow"rel="externalnofollow"rel="externalnofollow"onclick="history.go(-1);"class="btn">返回上一页</a> <ahref="javascript:"rel="externalnofollow"rel="externalnofollow"rel="externalnofollow"rel="externalnofollow"onclick="$('.errorMessage').toggle();"class="btn">隐藏详细信息</a> <br/><br/> </div> <script>try{top.$.jBox.closeTip();}catch(e){}</script> </div> </body> </html> <% }out=pageContext.pushBody(); %>
此页面就比服务器抛出的页面友好甚多。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!