DirectivesPstThread.java
11.3 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package com.bsth.data.directive;
import com.alibaba.fastjson.JSON;
import com.bsth.data.schedule.DayOfSchedule;
import com.bsth.entity.directive.D60;
import com.bsth.entity.directive.D64;
import com.bsth.entity.directive.Directive;
import com.bsth.repository.directive.D60Repository;
import com.bsth.repository.directive.D64Repository;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* 指令持久化线程
* Created by panzhao on 2017/3/6.
*/
@Component
public class DirectivesPstThread extends Thread {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
D60Repository d60Repository;
@Autowired
D64Repository d64Repository;
@Autowired
DayOfSchedule dayOfSchedule;
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
private TransactionTemplate transactionTemplate;
private static DateTimeFormatter fmtyyyyMMdd = DateTimeFormat.forPattern("yyyy-MM-dd");
@Override
public void run() {
logger.warn("指令持久化开始");
try{
ConcurrentLinkedQueue<Directive> list = DayOfDirectives.pstDirectives;
List<D60> d60s = new ArrayList<>();
List<D64> d64s = new ArrayList<>();
//按 60 和 64 分组
Directive directive;
D60 d60;
for (int i = 0; i < 2000; i++) {
directive = list.poll();
if(null == directive)
break;
//日期
directive.setRq(fmtyyyyMMdd.print(directive.getTimestamp()));
if (directive instanceof D60) {
d60 = (D60) directive;
if(isDelete(d60))
continue;
d60s.add(d60);
}
else if(directive instanceof D64)
d64s.add((D64) directive);
}
//入库60
save60(d60s);
//入库64
save64(d64s);
// 60 指令更新(车载响应)
ConcurrentLinkedQueue<D60> updateD60s = DayOfDirectives.pstD60s;
d60s = new ArrayList<>();
for (int i = 0; i < 2000; i++) {
d60 = updateD60s.poll();
if(null == d60)
break;
d60s.add(d60);
}
if(d60s.size() > 0)
update60(d60s);
}catch (Exception e){
logger.error("指令入库出现异常", e);
}
logger.warn("指令持久化结束");
}
private void save64(final List<D64> d64s) {
if(null == d64s || d64s.size() == 0)
return;
logger.warn("64指令保存开始");
String sql = "insert into bsth_v_directive_64(device_id,error_text,http_code,oper_code,rq,sender,timestamp,city_code,line_id,txt_content,resp_ack) " +
" values(?,?,?,?,?,?,?,?,?,?,?)";
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
try {
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
D64 d64 = d64s.get(i);
ps.setString(1 , d64.getDeviceId());
ps.setString(2, isNvl(d64.getErrorText()));
ps.setInt(3, d64.getHttpCode());
ps.setShort(4, isNvl(d64.getOperCode()));
ps.setString(5, d64.getRq());
ps.setString(6, isNvl(d64.getSender()));
ps.setLong(7, d64.getTimestamp());
ps.setShort(8, isNvl(d64.getData().getCityCode()));
ps.setString(9, isNvl(d64.getData().getLineId()));
ps.setString(10, isNvl(d64.getData().getTxtContent()));
ps.setShort(11, isNvl(d64.getRespAck()));
}
@Override
public int getBatchSize() {
return d64s.size();
}
});
} catch (Exception e) {
logger.error(String.format("错误数据:%s", JSON.toJSONString(d64s)), e);
status.setRollbackOnly();
}
}
});
logger.warn("64指令保存结束");
}
private void update60(final List<D60> d60s) {
if(null == d60s || d60s.size() == 0)
return;
logger.warn("60指令更新开始");
String sql = "update bsth_v_directive_60 set reply46=?,reply46time=?,reply47=?,reply47time=? where device_id=? and timestamp=? and msg_id=?";
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
try{
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
D60 d60 = d60s.get(i);
ps.setShort(1, isNvl(d60.getReply46()));
if(null == d60.getReply46Time())
ps.setNull(2, Types.BIGINT);
else
ps.setLong(2, d60.getReply46Time());
ps.setShort(3, isNvl(d60.getReply47()));
if(null == d60.getReply47Time())
ps.setNull(4, Types.BIGINT);
else
ps.setLong(4, d60.getReply47Time());
ps.setString(5, d60.getDeviceId());
ps.setLong(6, d60.getTimestamp());
ps.setInt(7, d60.getMsgId());
}
@Override
public int getBatchSize() {
return d60s.size();
}
});
}catch (Exception e){
logger.error(String.format("错误数据:%s", JSON.toJSONString(d60s)), e);
status.setRollbackOnly();
}
}
});
logger.warn("60指令更新结束");
}
private void save60(final List<D60> d60s) {
if(null == d60s || d60s.size() == 0)
return;
logger.warn("60指令保存开始");
String sql = "insert into bsth_v_directive_60(device_id,error_text,http_code,oper_code,rq,sender,timestamp" +
",alarm_time,company_code,dispatch_instruct,instruct_type,msg_id,service_state,txt_content,is_dispatch" +
",line_code,reply46,reply46time,reply47,reply47time,sch) " +
" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
try{
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
D60 d60 = d60s.get(i);
ps.setString(1, d60.getDeviceId());
ps.setString(2, isNvl(d60.getErrorText()));
ps.setInt(3, d60.getHttpCode());
ps.setShort(4, d60.getOperCode());
ps.setString(5, d60.getRq());
ps.setString(6, d60.getSender());
ps.setLong(7, d60.getTimestamp());
ps.setLong(8, isNvl(d60.getData().getAlarmTime()));
ps.setShort(9, isNvl(d60.getData().getCompanyCode()));
ps.setShort(10, isNvl(d60.getData().getDispatchInstruct()));
ps.setInt(11, d60.getData().getInstructType());
ps.setInt(12, d60.getData().getMsgId());
ps.setLong(13, d60.getData().getServiceState());
ps.setString(14, d60.getData().getTxtContent());
ps.setBoolean(15, d60.isDispatch());
ps.setString(16, isNvl(d60.getLineCode()));
ps.setShort(17, isNvl(d60.getReply46()));
if(null == d60.getReply46Time())
ps.setNull(18, Types.BIGINT);
else
ps.setLong(18, d60.getReply46Time());
ps.setShort(19, isNvl(d60.getReply47()));
if(null == d60.getReply47Time())
ps.setNull(20, Types.BIGINT);
else
ps.setLong(20, d60.getReply47Time());
if(d60.getSch()==null)
ps.setNull(21, Types.BIGINT);
else
ps.setLong(21, d60.getSch().getId());
}
@Override
public int getBatchSize() {
return d60s.size();
}
});
}catch (Exception e){
logger.error(String.format("错误数据:%s", JSON.toJSONString(d60s)), e);
status.setRollbackOnly();
}
}
});
logger.warn("60指令保存结束");
}
private String isNvl(String v) {
return v==null?"":v;
}
private short isNvl(Short v) {
return v==null?0:v;
}
private long isNvl(Long v) {
return v==null?0:v;
}
private boolean isDelete(D60 d60){
try{
//如果关联的班次已经不存在了,放弃入库,很低概率出现
if(d60.isDispatch() && d60.getSch().isDeleted()){
logger.warn("save 指令,发现 deleted=true 的班次,id=" + d60.getSch().getId());
return true;
}
}catch (Exception e){
logger.error("", e);
}
return false;
}
}