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;
}
}