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() {
}
}
简单的高并发就处理好了。