SafeDrivDataLoadThread.java 3.07 KB
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:9988//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());
        }
    }
}