GpsBufferRefreshThread.java 3.34 KB
package com.bsth.gpsdata.thread;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsth.gpsdata.buffer.GpsRealDataBuffer;
import com.bsth.gpsdata.entity.GpsRealData;
import com.bsth.gpsdata.util.ConfigUtil;
import com.google.common.collect.ImmutableMap;

/**
 * 
 * @ClassName: RefreshBufferThread 
 * @Description: TODO(刷新GPS实时缓存数据 线程) 
 * @author PanZhao
 * @date 2016年5月11日 下午8:56:44 
 *
 */
@Component
public class GpsBufferRefreshThread extends Thread{
	
	private static Logger logger = LoggerFactory.getLogger(GpsBufferRefreshThread.class);

	//接口地址
	private static String url;
	
	static{
		url = ConfigUtil.getProp("http.gps.real.url");
	}
	
	@Autowired
	GpsRealDataBuffer gpsBuffer;
	
	@Override
	public void run() {
		try{
			long t = System.currentTimeMillis();
			
			List<GpsRealData> newList = getterRealGpsData();
			
			if(gpsBuffer.isNullEmpty())
				gpsBuffer.initBuffer(newList);
			else{
				ImmutableMap<String, GpsRealData> deviceMap = gpsBuffer.getDeviceGpsMap();
				
				GpsRealData oldGps;
				
				//新的GPS信号
				List<GpsRealData> upGpsList = new ArrayList<>();
				//需要更新映射的线路
				Set<Integer> upLines = new HashSet<>();
				
				for(GpsRealData newData : newList){
					
					oldGps = deviceMap.get(newData.getDeviceId());
					
					//新的GPS点
					if(null == oldGps 
							|| oldGps.getTimestamp() < newData.getTimestamp()){
						upGpsList.add(newData);
						upLines.add(newData.getLineId());
					}
				}
				
				//更新缓存
				gpsBuffer.updateBuffer(upLines, upGpsList);
			}
			
			logger.info("本次刷新GPS实时数据耗时:" + (System.currentTimeMillis() - t) + "毫秒");
		}catch(Exception e){
			logger.error("", e);
		}
	}
	
	/**
	 * 
	 * @Title: getterRealGpsData 
	 * @Description: TODO(请求实时GPS数据) 
	 * @return List<GpsRealData>
	 * @throws
	 */
	public List<GpsRealData> getterRealGpsData() throws Exception{
		List<GpsRealData> list = new ArrayList<>();
		
		HttpClient client = new HttpClient();
		GetMethod method = new GetMethod(url);
		//要求服务器主动断开连接
		method.setRequestHeader("Connection", "close");
		
		int status = client.executeMethod(method);
		
		if(status == 200){
			BufferedReader br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
			
			StringBuffer stringBuffer = new StringBuffer();
			String str= "";
			while((str = br.readLine()) != null){   
	            stringBuffer .append(str );   
	        }
			
			JSONObject jsonObj = JSON.parseObject(stringBuffer.toString());
			
			if(jsonObj != null)
				list = JSON.parseArray(jsonObj.getString("data"), GpsRealData.class);
			
			//释放连接
			method.releaseConnection();
		}
		else
			logger.error("error status code: " + status);
		
		return list;
	}
}