并发处理

public static void main(String[] args){
		if (args.length != 2) {
			System.out.println("Usage: java AutoLocationMain spring.xml autoLocation.conf");
			return;
		}
		String springXmlfile = args[0];
		String confFile = args[1]; 
		
		System.out.println(springXmlfile + "   " + confFile);
		
		LocationMain locationMain=new LocationMain(springXmlfile,confFile);
		
		locationQueue=new ArrayBlockingQueue(conf.locationQueueLength);
		
		locationMain.production();
		locationMain.consume();
		
	}
	
	public void production(){
		Timer timer = new Timer();
		timer.schedule(new Producer(locationQueue, conf), 0 , conf.checkInterval*1000);
	}
	
	//消费
	public void consume(){
		LocationMsg obj;
		ExecutorService executorService = Executors.newFixedThreadPool(conf.locationThreadPollNum);
		
		while(true){
			try {
				obj= (LocationMsg)locationQueue.poll(30, TimeUnit.SECONDS);
				if (obj!=null){ 
					Consumer task=new Consumer(obj, locationQueue, conf);
					executorService.submit(task);
				}
				
			} catch (InterruptedException e) { 
				e.printStackTrace();
			}
		}
		
		
	}


//生产者
public class Producer extends TimerTask {
	private static final Logger logger = LoggerFactory.getLogger(Producer.class);
	
	private final BlockingQueue queue;
	private static HashMap<String, String> clockuserMap=new HashMap<String, String>();
	private static int day=0;
	private static int timeIntSystem = 0;
	private static LocationConfig conf=null; 
	
	public Producer(BlockingQueue q, LocationConfig conf){
		this.queue = q;
		this.conf = conf;
	}

	@Override
	public void run() {
		int currTimet = (int)(System.currentTimeMillis()/1000);  
		int nowDay = TimeUtil.dayOfTimet(currTimet); 
		String systime = TimeUtil.timet2String(currTimet, "HH:mm:ss");
		timeIntSystem = changeTimes(systime);
		if (day!=nowDay){
			clockuserMap.clear();
			day=nowDay;
		}
		
		List listOne = new ArrayList();
		List listRepeat = new ArrayList();
		listOne = oneLocation();
		listRepeat = repeatLocation();
		
		logger.debug("  -----------------------------   listOne size : " + listOne.size() + " listRepeat size  " + listRepeat.size());
		logger.debug("  -----------------------------   queue size : " + queue.size());
		
		if(listOne.size() > 0){
			try {
				for (int i = 0; i < listOne.size(); i++) {
					Object obj = listOne.get(i);
					queue.put(obj);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		if(listRepeat.size() > 0){
			try {
				for (int i = 0; i < listRepeat.size(); i++) {
					Object obj = listRepeat.get(i);
					queue.put(obj);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}


//消费者
public class Consumer implements Callable {

	private final Logger logger=LoggerFactory.getLogger(Consumer.class);
	
	private final BlockingQueue queue;
	private static LocationConfig conf=null; 
	
	LocationMsg obj ;
	
	public Consumer(LocationMsg obj, BlockingQueue queue, LocationConfig conf){
		this.obj = obj;
		this.queue = queue;
		this.conf = conf;
	}
	
	//消费,定位
	public Object call(){
		String returnId = location(obj.getMobile(), obj.getCompanyId());
		if(!returnId.equals("1") && !returnId.equals("0")){
			String returnId2 = location(obj.getMobile(), obj.getCompanyId());
			if(!returnId.equals("1")){
				locationError(obj.getMobile(), obj.getCompanyId(), returnId2);
			}
		}else if(returnId.equals("0")){
			try {
				Thread.sleep(conf.resentIntervalTime*1000);
				String returnId2 = location(obj.getMobile(), obj.getCompanyId());
				if(!returnId.equals("1")){
					locationError(obj.getMobile(), obj.getCompanyId(), returnId2);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		/*if(returnId.equals("9990245") || returnId.equals("11196") || returnId.equals("1119207") || returnId.equals("11192") || returnId.equals("1110102")){
			if(obj.getResult() < 2){
				LocationMsg lm = new LocationMsg();
				lm.setMobile(obj.getMobile());
				lm.setCompanyId(obj.getCompanyId());
				lm.setResult(obj.getResult() + 1);
				try {
					queue.put(lm);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else {
				locationError(obj.getMobile(), obj.getCompanyId(), returnId);
			}
			
		}else if(returnId.equals("0")){
			try {
				Thread.sleep(conf.resentIntervalTime*1000);
				String returnId2 = location(obj.getMobile(), obj.getCompanyId());
				if(returnId.equals("9990245") || returnId.equals("11196") || returnId.equals("1119207") || returnId.equals("11192") || returnId.equals("1110102") || returnId2.equals("0")){
					locationError(obj.getMobile(), obj.getCompanyId(), returnId2);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}*/
		
		return null;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值