SafeDrivDataLoadThread.java
3.1 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
package com.bsth.data.safe_driv;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
/**
* 安全驾驶数据加载线程
* Created by panzhao on 2017/4/6.
*/
@Component
public class SafeDrivDataLoadThread extends Thread{
private final static String url = "http://180.166.5.82:9007/bsth-safedriving/Crlcxb/realtimeInterface.do";
Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public void run() {
List<SafeDriv> list = null;
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
HttpGet get = new HttpGet(url);
response = httpClient.execute(get);
HttpEntity entity = response.getEntity();
if (null != entity) {
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuffer stringBuffer = new StringBuffer();
String str = "";
while ((str = br.readLine()) != null)
stringBuffer.append(str);
list = JSON.parseArray(stringBuffer.toString(), SafeDriv.class);
/**
* 模拟数据
SafeDriv sd1 = new SafeDriv();
sd1.setYczltype("1");
sd1.setClzbh("W2B-001");
sd1.setStartime("2017-04-07 08:00:00.0");
SafeDriv sd2 = new SafeDriv();
sd2.setYczltype("2");
sd2.setClzbh("W2B-002");
sd2.setStartime("2017-04-07 08:02:00.0");
SafeDriv sd3 = new SafeDriv();
sd3.setYczltype("3");
sd3.setClzbh("W2B-003");
sd3.setStartime("2017-04-07 08:03:00.0");
SafeDriv sd4 = new SafeDriv();
sd4.setYczltype("4");
sd4.setClzbh("W2B-004");
sd4.setStartime("2017-04-07 08:04:00.0");
SafeDriv sd5 = new SafeDriv();
sd5.setYczltype("5");
sd5.setClzbh("W2B-005");
sd5.setStartime("2017-04-07 08:05:00.0");
list.add(sd1);
list.add(sd2);
list.add(sd3);
list.add(sd4);
list.add(sd5);
*/
for(SafeDriv sd : list){
SafeDrivCenter.put(sd);
}
}
httpClient.close();
response.close();
} catch (Exception e) {
logger.error("安全驾驶接口报错了" , e.getMessage());
}
}
}