GpsBufferRefreshThread.java
3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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;
}
}