前面介绍过spring的taskExecutor,今天介绍一个jdk里处理多线程的方法 一、spring的配置文件(注入bean) <bean id="cmsClickButtonMng" class="com.xxx.manager.main.impl.CmsClickButtonMngImpl"/> 二、线程类CustomerButton.java import java.util.concurrent.BlockingQueue; import nl.bitwalker.useragentutils.UserAgent; import org.apache.commons.lang.StringUtils; import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; import com.xxxx.cms.entity.main.CmsClickButton; import com.xxxx.cms.manager.main.CmsClickButtonMng; import com.xxxx.common.util.UserAgentUtils; @SuppressWarnings("rawtypes") public class ConsumerButton implements Runnable{ public static boolean running = false; protected WebApplicationContext ctx; private CmsClickButtonMng cmsClickButtonMng;//要处理的类 protected BlockingQueue queue = null; protected static int i = 0; public ConsumerButton(BlockingQueue queue) { this.queue = queue; } public void run() { try { System.out.println("queue大小为:"+ queue.size()); while(!queue.isEmpty()){ CmsClickButton cb = (CmsClickButton) queue.take(); if(cb != null){ record(cb); } } ConsumerButton.running = false; } catch (InterruptedException e) { e.printStackTrace(); } } /**业务*/ public void record(CmsClickButton cb){ try { if(cb != null){ if(StringUtils.isNotBlank(cb.getAgent())){ String agent = cb.getAgent(); String customerModel = UserAgentUtils.getCustomerModel(agent);//手机型号 cb.setCustomerModel(customerModel); UserAgent userAgent = UserAgent.parseUserAgentString(agent); if(userAgent != null){ String browserName = String.valueOf(userAgent.getBrowser().getName());//浏览器类型 String operatingSystem = String.valueOf(userAgent.getOperatingSystem().getName());//操作系统类型 String browserVersion =String.valueOf(userAgent.getBrowserVersion());//浏览器版本 boolean isMobileDevice = userAgent.getOperatingSystem().isMobileDevice();//是否是移动设备 cb.setBrowserName(browserName); cb.setBrowserVersion(browserVersion); cb.setIsMobileDevice(isMobileDevice); cb.setOperatingSystem(operatingSystem); } } WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); cmsClickButtonMng = (CmsClickButtonMng) wac.getBean("cmsClickButtonMng"); cmsClickButtonMng.saveCb(cb); ConsumerButton.i++; System.out.println("finish ..."+ConsumerButton.i); } } catch (Exception e) { e.printStackTrace(); } } } 三、Producer.java import java.util.concurrent.BlockingQueue; import com.gmiao.cms.entity.main.CmsClickButton; import com.gmiao.cms.entity.main.CmsTrafficPage; @SuppressWarnings("rawtypes") public class Producer implements Runnable { protected BlockingQueue queue = null; protected CmsTrafficPage tp = null; //产品一 protected CmsClickButton cb = null; //产品二 public Producer(BlockingQueue queue,CmsTrafficPage tp) { this.queue = queue; this.tp = tp; } public Producer(BlockingQueue queue,CmsClickButton cb) { this.queue = queue; this.cb = cb; } @SuppressWarnings("unchecked") public void run() { try { if(tp != null){ queue.put(tp); }else if(cb != null){ queue.put(cb); } } catch (InterruptedException e) { e.printStackTrace(); } } } 四、springmvc /** * 参数指队列的最大容量 */ public static BlockingQueue queue = new ArrayBlockingQueue(10000); @RequestMapping(value="/save.jspf") public void save(String pid,String openId,String pageName,HttpServletRequest request,HttpServletResponse response){ try { //如果项目id或 页面名称为空,则不作记录 if(StringUtils.isBlank(pid) || StringUtils.isBlank(pageName)){ ajaxErrorToJson(response, null, "项目id或页面名称不能为空!"); return ; } CmsTrafficPage tp = new CmsTrafficPage(); tp.setPid(pid); tp.setDate(new Date()); tp.setStayTime(0l); tp.setIp(RequestUtils.getIpAddr(request));//用户ip地址 tp.setPageName(pageName); tp.setPageUrl(request.getHeader("Referer"));//发起请求的页面链接 tp.setSessionId(request.getSession().getId());//用户sessionId String agent = request.getHeader("user-agent");//客户端信息 if(StringUtils.isNotBlank(agent)){ tp.setAgent(agent); } BlockingQueue queue = TrafficPageAct.queue;//所在的action或controller Producer producer = new Producer(queue,tp); new Thread(producer).start(); if(!Consumer.running){ Consumer consumer = new Consumer(queue); new Thread(consumer).start(); Consumer.running = true; } } catch (Exception e) { log.error("记录页面的访问出错了!",e); ajaxErrorToJson(response, null, "记录页面访问出错了!"); return ; } } 我这里只是项目代码中使用BlockQueue,要了解或学习可以查看下面一位网页的文章http://wsmajunfeng.iteye.com/blog/1629354或查看jdk文档
时间: 2024-10-13 14:02:43