SafeDrivDataLoadThread.java
2.56 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
package com.bsth.data.safe_driv;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
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{
Logger logger = LoggerFactory.getLogger(this.getClass());
static String url;
static CloseableHttpClient httpClient = null;
static HttpGet get;
static RequestConfig requestConfig;
static CloseableHttpResponse response;
static HttpEntity entity;
static BufferedReader br;
static {
url = "http://180.166.5.82:9007/bsth-safedriving/Crlcxb/realtimeInterface.do";
httpClient = HttpClients.createDefault();
get = new HttpGet(url);
requestConfig = RequestConfig.custom()
.setConnectTimeout(5500).setConnectionRequestTimeout(5000)
.setSocketTimeout(5500).build();
get.setConfig(requestConfig);
}
@Override
public void run() {
List<SafeDriv> list;
try {
response = httpClient.execute(get);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode != 200){
get.abort();
logger.error("http client status code: " + statusCode);
return;
}
entity = response.getEntity();
if (null != entity) {
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);
for(SafeDriv sd : list){
SafeDrivCenter.put(sd);
}
}
if (null != response)
response.close();
} catch (Exception e) {
logger.error("安全驾驶接口报错了" , e.getMessage());
}
}
}