ArrivalThread.java
3.5 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
package com.bsth.vehicle.gpsdata;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.bsth.util.DateUtils;
import com.bsth.util.db.DBUtils_MS;
import com.bsth.vehicle.directive.service.DirectiveService;
import com.bsth.vehicle.gpsdata.arrival.MatchService;
import com.bsth.vehicle.gpsdata.buffer.ArrivalDataBuffer;
import com.bsth.vehicle.gpsdata.entity.ArrivalInfo;
import com.bsth.websocket.handler.RealControlSocketHandler;
/**
*
* @ClassName: GpsArrivalStationThread
* @Description: TODO(定时从数据库加载到离站数据)
* @author PanZhao
* @date 2016年6月27日 上午10:58:13
*
*/
@Component
public class ArrivalThread extends Thread{
Logger logger = LoggerFactory.getLogger(this.getClass());
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
@Autowired
DirectiveService directiveService;
@Autowired
RealControlSocketHandler socketHandler;
Long diff = 1000 * 60 * 20L;
@Override
public void run() {
List<ArrivalInfo> list = null;
try {
list = loadData();
} catch (ParseException e) {
e.printStackTrace();
}
//缓存
ArrivalDataBuffer.putAll(list);
//有新到离站数据的线路
Set<String> newSet = new HashSet<>();
for(ArrivalInfo arr : list)
newSet.add(arr.getLineCode());
//启动对应的匹配线程
for(String lineCode : newSet){
MatchService.addService(lineCode);
}
}
/**
* @throws ParseException
*
* @Title: loadData
* @Description: TODO(从数据库加载到离站信息)
* @return List<ArrivalInfo> 返回类型
* @throws
*/
private List<ArrivalInfo> loadData() throws ParseException{
Calendar cal = Calendar.getInstance();
//周数,表分区字段
int weeks_year = cal.get(Calendar.WEEK_OF_YEAR);
//按时间标记增量加载
if(null == ArrivalDataBuffer.markTime){
//第一次从当天0点开始
ArrivalDataBuffer.markTime = DateUtils.getTimesmorning() * 1000L;
}
Long t = System.currentTimeMillis();
String sql = "select * from bsth_c_arrival_info where weeks_year=? and create_timestamp > ? and create_timestamp <=? order by create_date";
List<ArrivalInfo> list = new ArrayList<>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtils_MS.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, weeks_year/*30*/);
ps.setLong(2, ArrivalDataBuffer.markTime/*1461361377000L*/);
ps.setLong(3, t/*30*/);
rs = ps.executeQuery();
while(rs.next()){
list.add(new ArrivalInfo(rs.getString("device_id"), rs.getLong("ts"), rs.getString("line_id")
, rs.getInt("up_down"), rs.getString("stop_no"), rs.getInt("in_out"), rs.getLong("create_date")
, rs.getInt("weeks_year")));
}
logger.info("获取 到离站 数量:" + list.size() + ", st: " + ArrivalDataBuffer.markTime + " , et: " + t);
//时间戳标记
ArrivalDataBuffer.markTime = t;
} catch (Exception e) {
logger.error("", e);
}finally {
DBUtils_MS.close(rs, ps, conn);
}
return list;
}
}