BorrowCenter.java 2 KB
package com.bsth.vehicle;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bsth.vehicle.directive.service.DirectiveService;

/**
 * 
 * @ClassName: BorrowCenter 
 * @Description: TODO(借还车中心^_^) 
 * @author PanZhao
 * @date 2016年7月12日 下午11:49:36 
 *
 */
@Component
public class BorrowCenter {

	ScheduledExecutorService scheduler;
	
	@Autowired
	DirectiveService directiveService;
	
	Logger logger = LoggerFactory.getLogger(BorrowCenter.class);
	
	/**
	 * 构造函数
	 */
	public BorrowCenter() {
		scheduler = Executors.newScheduledThreadPool(15);
	}
	
	/**
	 * 
	 * @Title: put 
	 * @param @param nbbm 车辆
	 * @param @param lineCode 线路代码
	 * @param @param type    0 借出,1归还
	 * @param @param time    时间
	 * @throws
	 */
	public void put(String nbbm, Integer lineCode, int type, Long time, int upDown){
		Long t = System.currentTimeMillis();
		if(t >= time){
			//立即执行
			new Thread(new ChangeThread(nbbm, lineCode, type, upDown)).start();
		}
		else{
			//定时线程
			scheduler.schedule(new ChangeThread(nbbm, lineCode, type, upDown), (time - t) / 1000, TimeUnit.SECONDS);
		}
	}
	
	public class ChangeThread implements Runnable{
		String nbbm;
		Integer lineCode;
		int type;
		int upDown;
		
		public ChangeThread(String nbbm, Integer lineCode, int type, int upDown) {
			this.nbbm = nbbm;
			this.lineCode = lineCode;
			this.type = type;
			this.upDown = upDown;
		}

		@Override
		public void run() {
			logger.info("nbbm " + (type==0?"借出":"归还") + "线路代码 " + lineCode);
			directiveService.lineChange(nbbm, lineCode);
			if(upDown != -1){
				//切换走向
				directiveService.upDownChange(nbbm, upDown);
			}
		}
	}
}