java后端处理高并发

一个登陆页面可能会被很多账户同时登陆或者注册,那么我们就好处理这些并发,否则降低程序的使用率,甚至程序奔溃,下面一段代码处理程序的高并发效果不错。

                    /**
            *@author xiaoxie
            *@date create 2019/9/17
            *@return
             * 处理高并发
            */
            public class RequestHolder {
            
            //    线程处理
                private static final ThreadLocal<SysUser> userHolder = new ThreadLocal<SysUser>();
            
            //    登录
                private static final ThreadLocal<HttpServletRequest> requestHolder = new ThreadLocal<HttpServletRequest>();
            
            //    设置登陆用户
                public static void add(SysUser sysUser){
                    userHolder.set(sysUser);
                }
            
                public static void add(HttpServletRequest request) {
                    requestHolder.set(request);
                }
            
            //    处理当前用户
                public static SysUser getCurrentUser() {
                    return userHolder.get();
                }
            
            //    处理当前登录
                public static HttpServletRequest getCurrentRequest() {
                    return requestHolder.get();
                }
            
            //    消亡
                public static void remove(){
                    userHolder.remove();
                    requestHolder.remove();
                }
            }

注意:SysUser是自己定义的model,根据自己的需要设置。

在登陆或者注册完成之后就要消亡,否则会产生数据的溢出。

            /**
        *@author xiaoxie
        *@date CREATE 2019/9/6  UPDATE 2019/9/12
        *@return 请求前后监听
        */
        @Slf4j
        public class HttpInterceptor extends HandlerInterceptorAdapter {
        
            private static final String START_TIME = "requestStartTime";
        
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                String url = request.getRequestURI().toString();
                Map parameterMap = request.getParameterMap();
                log.info("request start. url:{}, params:{}", url, JsonMapper.obj2String(parameterMap));
                long start = System.currentTimeMillis();
                request.setAttribute(START_TIME, start);
                return true;
            }
        
            @Override
            public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //        String url = request.getRequestURI().toString();
        //        long start = (Long) request.getAttribute(START_TIME);
        //        long end = System.currentTimeMillis();
        //        log.info("request finished. url:{}, cost:{}", url, end - start);
                removeThreadLocalInfo();
            }
        
            @Override
            public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
                String url = request.getRequestURI().toString();
                long start = (Long) request.getAttribute(START_TIME);
                long end = System.currentTimeMillis();
                log.info("request completed. url:{}, cost:{}", url, end - start);
        
                removeThreadLocalInfo();
            }
        
            public void removeThreadLocalInfo() {
                RequestHolder.remove();
            }

通过过滤器进行实现

        /**
        *@author xiaoxie
        *@date  create 2019/9/17
        *@return
         *
         * 登陆过滤
        */
        @Slf4j
        public class LoginFilter implements Filter {
            @Override
            public void init(FilterConfig filterConfig) throws ServletException {
        
            }
        
            @Override
            public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
                HttpServletRequest req = (HttpServletRequest) servletRequest;
                HttpServletResponse resp = (HttpServletResponse) servletResponse;
        
                SysUser sysUser = (SysUser) req.getSession().getAttribute("user");
        
                if (sysUser == null){
                    String path = "/signin.jsp";
                    resp.sendRedirect(path);
                    return;
                }
        
        //        处理高并发
                RequestHolder.add(sysUser);
                RequestHolder.add(req);
                filterChain.doFilter(servletRequest,servletResponse);
                return;
            }
        
            @Override
            public void destroy() {
        
            }
        }

简单的高并发就处理好了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值