English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
J2ee Listener Instance in High Concurrent Situation Detailed Explanation
Introduction: Limit the maximum concurrent number under high concurrency, set parameters (maximum concurrent number) in web.xml using a filter, and set other related parameters. Details can be seen in the code.
Step 1: Configure the web.xml configuration, explain the parts that are not understood: parameters50 is used through the parameter name maxConcurrent in the implementation class of the filter, filter-The 'class' is the implementation class written,
url-The 'pattern' is the URL that limits the concurrent time, end!
<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>
Step 2: Write the implementation class to implement the filter interface, which has three methods, see the code for details.
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; //Total count private static AtomicInteger count = new AtomicInteger(0); /** * Get the current concurrent number * @return */ public static int get(){ return count.get(); } /** * Increase the concurrent number * @return */ public static int increase(){ return count.incrementAndGet(); } /** * Decrease the concurrent number * @return */ public static int decrement(){ return count.decrementAndGet(); } /** * Initialization */ public void init(FilterConfig filterConfig) throws ServletException { //Get the maximum concurrent number of configuration String maxStr = filterConfig.getInitParameter("maxConcurrent"); int num = -1; if(maxStr != null && !"".equals(maxStr)){ num = Integer.parseInt(maxStr); } if(num >= 1){ this.maxConcurrent = num; } this.maxConcurrent = -1; } } /** * Filter main method */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try{ //Increase the concurrent number int num = increase(); if(maxConcurrent > 0){ if(maxConcurrent >= num){ chain.doFilter(request, response); log.info("The first concurrent number:")+count.get()); } HttpServletResponse res = (HttpServletResponse) response; res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,"Maximum concurrency limit reached"); log.info("Maximum concurrency reached"); log.info("Maximum concurrency count:")+count.get()); } } chain.doFilter(request, response); log.info("Second concurrency count:")+count.get()); } } decrement(); log.info("Decreased concurrency level:")+count.get()); } } /** * Exit destruction */ public void destroy() { this.filterConfig = null; log.info("Destroy......"); } }
The code is complete here.
Complain about the pitfalls encountered in the project:
1.response.sendError(int, string);In the code of this article, it is res.sendError, and if it is returned directly as in the code of this article, it will return a503The page brought out by the server, this page is rough and ugly,
In order to notify users in a friendly manner, the following steps need to be taken, and the following configuration code should be added to web.xml:
<error-page> <error-code>503</error-code> <location>/WEB-INF/views/error/503.jsp</location> </error-page>
If the above information is configured in web.xml, it will be filtered first503at status code (HttpServletResponse.SC_SERVICE_UNAVAILABLE) of this page without throwing the server's page.
Among them503.jsp page needs to be completed by yourself. Here is just a sample for reference, the code is as follows:
<% response.setStatus(503); // Get exception class Throwable ex = Exceptions.getThrowable(request); if (ex != null){ LoggerFactory.getLogger("500.jsp").error(ex.getMessage(), ex); } // Compilation error information StringBuilder sb = new StringBuilder("Error information: \n"); if (ex != null) { sb.append(Exceptions.getStackTraceAsString(ex)); } sb.append("Unknown error.\n\n"); } // If it is an asynchronous request or a mobile device, return the information directly}} if (Servlets.isAjaxRequest(request)) { out.print(sb); } // Output exception information page 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 - The service is temporarily unavailable</title> <%@include file="/WEB-INF/views/include/head.jsp" %> </head> <body> <div class="container-fluid"> <div class="page-header"><h1>The service is temporarily unavailable, please try again later.</h1></div> <div class="errorMessage"> Error Information:<%=ex==null&63;"Unknown Error.":StringUtils.toHtml(ex.getMessage())%> <br/> <br/> The server is temporarily unavailable, please try again later. Thank you! <br/> <br/> <a href="javascript:" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="history.go(-1);" class="btn">Go Back</a> <a href="javascript:" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="$('.errorMessage').toggle();" class="btn">View Details</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">Go Back</a> <a href="javascript:" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="$('.errorMessage').toggle();" class="btn">Hide Details</a> <br/> <br/> </div> <script>try{top.$.jBox.closeTip();}catch(e){}</script>/script> </div> </body> </html> <% out = pageContext.pushBody(); %>
This page is much friendlier than the page thrown out by the server.
Thank you for reading, and I hope it can help everyone. Thank you for your support of this site!
Declaration: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright. It has not been manually edited and does not assume any relevant legal responsibility. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting. Provide relevant evidence, and once verified, this site will immediately delete the infringing content.)