|
@@ -13,6 +13,8 @@ import org.springframework.stereotype.Service; |
|
@@ -13,6 +13,8 @@ import org.springframework.stereotype.Service; |
13
|
import org.springframework.util.LinkedMultiValueMap;
|
13
|
import org.springframework.util.LinkedMultiValueMap;
|
14
|
import org.springframework.util.MultiValueMap;
|
14
|
import org.springframework.util.MultiValueMap;
|
15
|
import org.springframework.util.StringUtils;
|
15
|
import org.springframework.util.StringUtils;
|
|
|
16
|
+import org.springframework.web.client.HttpClientErrorException;
|
|
|
17
|
+import org.springframework.web.client.RestClientException;
|
16
|
import org.springframework.web.client.RestTemplate;
|
18
|
import org.springframework.web.client.RestTemplate;
|
17
|
|
19
|
|
18
|
import javax.annotation.Resource;
|
20
|
import javax.annotation.Resource;
|
|
@@ -69,6 +71,10 @@ public class MessageBusProcessor { |
|
@@ -69,6 +71,10 @@ public class MessageBusProcessor { |
69
|
*/
|
71
|
*/
|
70
|
private static Boolean LOGIN_STATUS=false;
|
72
|
private static Boolean LOGIN_STATUS=false;
|
71
|
|
73
|
|
|
|
74
|
+ /**
|
|
|
75
|
+ * 失败重发请求次数
|
|
|
76
|
+ */
|
|
|
77
|
+ private static final int RETRY_TIMES= 3;
|
72
|
|
78
|
|
73
|
/**
|
79
|
/**
|
74
|
* HTTP请求框架
|
80
|
* HTTP请求框架
|
|
@@ -175,7 +181,18 @@ public class MessageBusProcessor { |
|
@@ -175,7 +181,18 @@ public class MessageBusProcessor { |
175
|
}
|
181
|
}
|
176
|
}
|
182
|
}
|
177
|
|
183
|
|
178
|
- @Scheduled(fixedRate = 10000)
|
184
|
+ /**
|
|
|
185
|
+ * 获取返回信息的状态码
|
|
|
186
|
+ * @param response response对象
|
|
|
187
|
+ * @return
|
|
|
188
|
+ */
|
|
|
189
|
+ private String getResponseDataCode(ResponseEntity<String> response){
|
|
|
190
|
+ JSONObject resJson = JSON.parseObject(response.getBody());
|
|
|
191
|
+ String code = resJson.getString("code");
|
|
|
192
|
+
|
|
|
193
|
+ return code;
|
|
|
194
|
+ }
|
|
|
195
|
+ @Scheduled(fixedDelay = 10000)
|
179
|
public void heartBit() {
|
196
|
public void heartBit() {
|
180
|
if (!StringUtils.isEmpty(TOKEN) && LOGIN_STATUS){
|
197
|
if (!StringUtils.isEmpty(TOKEN) && LOGIN_STATUS){
|
181
|
/*
|
198
|
/*
|
|
@@ -200,10 +217,12 @@ public class MessageBusProcessor { |
|
@@ -200,10 +217,12 @@ public class MessageBusProcessor { |
200
|
* 提交HTTP访问,获取返回信息
|
217
|
* 提交HTTP访问,获取返回信息
|
201
|
*/
|
218
|
*/
|
202
|
ResponseEntity<String> response = restTemplate.postForEntity(HEARTBEAT_URL, request, String.class);
|
219
|
ResponseEntity<String> response = restTemplate.postForEntity(HEARTBEAT_URL, request, String.class);
|
|
|
220
|
+
|
203
|
// 输出结果
|
221
|
// 输出结果
|
204
|
- System.out.println(response.getBody());
|
222
|
+ log.info(response.getBody());
|
|
|
223
|
+
|
205
|
if (response.getStatusCode().equals(HttpStatus.OK)) {
|
224
|
if (response.getStatusCode().equals(HttpStatus.OK)) {
|
206
|
- log.info("心跳成功");
|
225
|
+ log.info("心跳成功,token:[{}]",TOKEN);
|
207
|
} else {
|
226
|
} else {
|
208
|
log.error("心跳失败");
|
227
|
log.error("心跳失败");
|
209
|
}
|
228
|
}
|
|
@@ -217,17 +236,19 @@ public class MessageBusProcessor { |
|
@@ -217,17 +236,19 @@ public class MessageBusProcessor { |
217
|
*/
|
236
|
*/
|
218
|
public Boolean sendMsg(MSG msg) {
|
237
|
public Boolean sendMsg(MSG msg) {
|
219
|
if (LOGIN_STATUS) {
|
238
|
if (LOGIN_STATUS) {
|
220
|
- /*
|
|
|
221
|
- * 发起HTTP 登录请求
|
|
|
222
|
- * 登录接口的请求头为application/json
|
|
|
223
|
- */
|
|
|
224
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
225
|
- headers.setContentType(MediaType.APPLICATION_JSON);
|
239
|
+ try{
|
|
|
240
|
+ log.info("开始转发消息:{}",msg.toString());
|
|
|
241
|
+ /*
|
|
|
242
|
+ * 发起HTTP 登录请求
|
|
|
243
|
+ * 登录接口的请求头为application/json
|
|
|
244
|
+ */
|
|
|
245
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
246
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
226
|
|
247
|
|
227
|
- MSGS msgs = new MSGS();
|
|
|
228
|
- msg.getHEADER().setSNDR("HYYW");
|
248
|
+ MSGS msgs = new MSGS();
|
|
|
249
|
+ msg.getHEADER().setSNDR("HYYW");
|
229
|
|
250
|
|
230
|
- msgs.setMSG(msg);
|
251
|
+ msgs.setMSG(msg);
|
231
|
|
252
|
|
232
|
|
253
|
|
233
|
/*
|
254
|
/*
|
|
@@ -241,31 +262,33 @@ public class MessageBusProcessor { |
|
@@ -241,31 +262,33 @@ public class MessageBusProcessor { |
241
|
// dataModel.put("piece", "2");
|
262
|
// dataModel.put("piece", "2");
|
242
|
// msg.getMSG().setBODY(JSON.toJSONString(dataModel));
|
263
|
// msg.getMSG().setBODY(JSON.toJSONString(dataModel));
|
243
|
|
264
|
|
244
|
- /*
|
|
|
245
|
- * 设置获取到的token到头部信息Authorization节点中
|
|
|
246
|
- */
|
|
|
247
|
- headers.setBearerAuth(TOKEN);
|
|
|
248
|
-
|
|
|
249
|
- /*
|
|
|
250
|
- * 发起消息接口访问,发送消息
|
|
|
251
|
- */
|
|
|
252
|
-
|
|
|
253
|
- HttpEntity<MSGS> request = new HttpEntity<MSGS>(msgs, headers);
|
|
|
254
|
- ResponseEntity<String> response = restTemplate.postForEntity(SEND_MSG_URL, request, String.class);
|
|
|
255
|
-
|
|
|
256
|
- JSONObject resJson = JSON.parseObject(response.getBody());
|
|
|
257
|
- String code = resJson.getString("code");
|
265
|
+ /*
|
|
|
266
|
+ * 设置获取到的token到头部信息Authorization节点中
|
|
|
267
|
+ */
|
|
|
268
|
+ headers.setBearerAuth(TOKEN);
|
258
|
|
269
|
|
259
|
- System.out.println(response.getBody());
|
270
|
+ /*
|
|
|
271
|
+ * 发起消息接口访问,发送消息
|
|
|
272
|
+ */
|
260
|
|
273
|
|
261
|
- if (response.getStatusCode().equals(HttpStatus.OK) && "200".equals(code)) {
|
|
|
262
|
- log.info("消息发送成功");
|
|
|
263
|
- return true;
|
|
|
264
|
- } else {
|
|
|
265
|
- log.error("消息发送失败");
|
274
|
+ HttpEntity<MSGS> request = new HttpEntity<MSGS>(msgs, headers);
|
|
|
275
|
+ ResponseEntity<String> response = restTemplate.postForEntity(SEND_MSG_URL, request, String.class);
|
|
|
276
|
+ if (response.getStatusCode().equals(HttpStatus.OK)) {
|
|
|
277
|
+ JSONObject resJson = JSON.parseObject(response.getBody());
|
|
|
278
|
+ String code = resJson.getString("code");
|
|
|
279
|
+ if ("200".equals(code)) {
|
|
|
280
|
+ log.info("消息发送成功");
|
|
|
281
|
+ return true;
|
|
|
282
|
+ }
|
|
|
283
|
+ }
|
|
|
284
|
+ log.info("消息发送失败->{}",response.getBody());
|
|
|
285
|
+ }catch (Exception e){
|
|
|
286
|
+ log.info("消息发送失败->{},失败原因:{}",msg.toString(),e.toString());
|
|
|
287
|
+ log.error("消息发送失败->{},失败原因:{}",msg.toString(),e.toString());
|
266
|
return false;
|
288
|
return false;
|
267
|
}
|
289
|
}
|
268
|
-
|
290
|
+ }else {
|
|
|
291
|
+ log.info("未登陆,消息发送失败");
|
269
|
}
|
292
|
}
|
270
|
return false;
|
293
|
return false;
|
271
|
}
|
294
|
}
|
|
@@ -276,8 +299,8 @@ public class MessageBusProcessor { |
|
@@ -276,8 +299,8 @@ public class MessageBusProcessor { |
276
|
*
|
299
|
*
|
277
|
* @return
|
300
|
* @return
|
278
|
*/
|
301
|
*/
|
279
|
- @Async
|
|
|
280
|
- @Scheduled(fixedRate = 300)
|
302
|
+// @Async
|
|
|
303
|
+ @Scheduled(fixedRate = 1000)
|
281
|
public JSONArray getMsg() {
|
304
|
public JSONArray getMsg() {
|
282
|
if(!LOGIN_STATUS){
|
305
|
if(!LOGIN_STATUS){
|
283
|
login();
|
306
|
login();
|
|
@@ -287,86 +310,99 @@ public class MessageBusProcessor { |
|
@@ -287,86 +310,99 @@ public class MessageBusProcessor { |
287
|
* 发起HTTP 登录请求
|
310
|
* 发起HTTP 登录请求
|
288
|
* 登录接口的请求头为application/json
|
311
|
* 登录接口的请求头为application/json
|
289
|
*/
|
312
|
*/
|
290
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
291
|
- headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
292
|
- headers.setBearerAuth(TOKEN);
|
|
|
293
|
- /*
|
|
|
294
|
- * 请求参数拼装
|
|
|
295
|
- */
|
|
|
296
|
- MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
|
|
|
297
|
- params.add("username", "HYYW");
|
|
|
298
|
- HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(params, headers);
|
313
|
+ try{
|
|
|
314
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
315
|
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
316
|
+ headers.setBearerAuth(TOKEN);
|
|
|
317
|
+ /*
|
|
|
318
|
+ * 请求参数拼装
|
|
|
319
|
+ */
|
|
|
320
|
+ MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
|
|
|
321
|
+ params.add("username", "HYYW");
|
|
|
322
|
+ HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(params, headers);
|
299
|
|
323
|
|
300
|
- /*
|
|
|
301
|
- * 提交HTTP访问,获取返回信息
|
|
|
302
|
- */
|
|
|
303
|
- ResponseEntity<String> response = restTemplate.postForEntity(GET_MSG_URL, request, String.class);
|
|
|
304
|
- // 输出结果
|
|
|
305
|
- log.info("获取到消息返回---{}---",response.getBody());
|
|
|
306
|
- /*
|
|
|
307
|
- * 从返回信息中确定是否获取到消息
|
|
|
308
|
- */
|
|
|
309
|
- JSONObject resJson = JSON.parseObject(response.getBody());
|
|
|
310
|
- String code = resJson.getString("code");
|
|
|
311
|
- if (response.getStatusCode().equals(HttpStatus.OK) && "200".equals(code)) {
|
324
|
+ /*
|
|
|
325
|
+ * 提交HTTP访问,获取返回信息
|
|
|
326
|
+ */
|
|
|
327
|
+ ResponseEntity<String> response = restTemplate.postForEntity(GET_MSG_URL, request, String.class);
|
|
|
328
|
+ // 输出结果
|
312
|
|
329
|
|
313
|
- JSONArray data = resJson.getJSONArray("data");
|
|
|
314
|
- log.info("消息接收成功,接收消息为>>>{}<<<",data.toString());
|
|
|
315
|
|
330
|
|
316
|
- for (int i = 0; i<data.size() ; i++) {
|
331
|
+ if (response.getStatusCode().equals(HttpStatus.OK)) {
|
317
|
/*
|
332
|
/*
|
318
|
- 取得是大数据小组的实体,他们的msg.body的封装是以对象实体object封装的。不是json字符串。
|
333
|
+ * 从返回信息中确定是否获取到消息
|
319
|
*/
|
334
|
*/
|
320
|
- String msg = data.getObject(i,String.class);
|
|
|
321
|
- JSONObject rootJson = JSON.parseObject(msg);
|
|
|
322
|
- JSONObject msgJson = rootJson.getJSONObject("MSG");
|
|
|
323
|
- JSONObject body = msgJson.getJSONObject("BODY");
|
335
|
+ JSONObject resJson = JSON.parseObject(response.getBody());
|
|
|
336
|
+ String code = resJson.getString("code");
|
|
|
337
|
+ if ("200".equals(code)){
|
|
|
338
|
+ JSONArray data = resJson.getJSONArray("data");
|
|
|
339
|
+ log.info("消息接收成功,接收消息数量>>>{}<<<",data.size());
|
324
|
|
340
|
|
325
|
- HEADER msgHeader = msgJson.getObject("HEADER",HEADER.class);
|
341
|
+ for (int i = 0; i<data.size() ; i++) {
|
|
|
342
|
+ /*
|
|
|
343
|
+ 取得是大数据小组的实体,他们的msg.body的封装是以对象实体object封装的。不是json字符串。
|
|
|
344
|
+ */
|
|
|
345
|
+ String msg = data.getObject(i,String.class);
|
|
|
346
|
+ log.info("开始转发消息---{}---",msg);
|
|
|
347
|
+ JSONObject rootJson = JSON.parseObject(msg);
|
|
|
348
|
+ JSONObject msgJson = rootJson.getJSONObject("MSG");
|
|
|
349
|
+ JSONObject body = msgJson.getJSONObject("BODY");
|
326
|
|
350
|
|
327
|
- MSG transMsg= new MSG();
|
|
|
328
|
- String transBody = body.toJSONString();
|
|
|
329
|
- transMsg.setHEADER(msgHeader);
|
|
|
330
|
- transMsg.setBODY(transBody);
|
351
|
+ HEADER msgHeader = msgJson.getObject("HEADER",HEADER.class);
|
331
|
|
352
|
|
332
|
- transMsg.toString();
|
353
|
+ MSG transMsg= new MSG();
|
|
|
354
|
+ String transBody = body.toJSONString();
|
|
|
355
|
+ transMsg.setHEADER(msgHeader);
|
|
|
356
|
+ transMsg.setBODY(transBody);
|
333
|
|
357
|
|
334
|
/*
|
358
|
/*
|
335
|
自定义对返回数据的处理
|
359
|
自定义对返回数据的处理
|
336
|
*/
|
360
|
*/
|
337
|
- log.info("开始转发消息");
|
|
|
338
|
- Boolean sendResult = sendMsg(transMsg);
|
|
|
339
|
- /**
|
|
|
340
|
- * todo:消息失败处理
|
|
|
341
|
- */
|
|
|
342
|
- if(!sendResult){
|
|
|
343
|
- //todo:消息备份或者重发?
|
361
|
+ log.info("开始转发消息");
|
|
|
362
|
+ Boolean sendResult = sendMsg(transMsg);
|
|
|
363
|
+ /**
|
|
|
364
|
+ * todo:转发消息失败处理
|
|
|
365
|
+ */
|
|
|
366
|
+ if(!sendResult){
|
|
|
367
|
+ log.error("!!!!!!消息--->{}<---转发失败!!!!!!,尝试重发",transMsg.toString());
|
|
|
368
|
+ //todo:消息备份或者重发?
|
|
|
369
|
+ reSend(transMsg);
|
|
|
370
|
+ }
|
|
|
371
|
+ }
|
|
|
372
|
+ return data;
|
344
|
}
|
373
|
}
|
|
|
374
|
+ } else {
|
|
|
375
|
+ log.error("消息获取失败");
|
|
|
376
|
+ return new JSONArray();
|
345
|
}
|
377
|
}
|
|
|
378
|
+ }catch (Exception httpE){
|
|
|
379
|
+ log.info("消息获取失败,失败原因:{}",httpE.toString());
|
|
|
380
|
+ log.error("消息获取失败,失败原因:{}",httpE.toString());
|
346
|
|
381
|
|
347
|
-
|
|
|
348
|
-
|
|
|
349
|
-
|
|
|
350
|
- return data;
|
|
|
351
|
-
|
|
|
352
|
- } else {
|
|
|
353
|
- log.error("消息获取失败");
|
|
|
354
|
- return new JSONArray();
|
|
|
355
|
}
|
382
|
}
|
|
|
383
|
+ return new JSONArray();
|
356
|
}
|
384
|
}
|
357
|
|
385
|
|
358
|
/**
|
386
|
/**
|
359
|
* 读取备份消息并消息重发
|
387
|
* 读取备份消息并消息重发
|
360
|
* @return
|
388
|
* @return
|
361
|
*/
|
389
|
*/
|
362
|
- public Boolean reSend(){
|
|
|
363
|
- return false;
|
390
|
+ public void reSend(MSG msg){
|
|
|
391
|
+ log.error("***进入重发***");
|
|
|
392
|
+ for (int i = 0; i < RETRY_TIMES; i++) {
|
|
|
393
|
+ Boolean sendResult = sendMsg(msg);
|
|
|
394
|
+ if (sendResult){
|
|
|
395
|
+ log.error("***重发成功***");
|
|
|
396
|
+ break;
|
|
|
397
|
+ }
|
|
|
398
|
+ }
|
|
|
399
|
+ log.error("***已尝试重发三次,重发失败***");
|
364
|
}
|
400
|
}
|
365
|
|
401
|
|
366
|
}
|
402
|
}
|
367
|
|
403
|
|
368
|
/**
|
404
|
/**
|
369
|
- * 消息发送实体类
|
405
|
+ * 消息实体类
|
370
|
*/
|
406
|
*/
|
371
|
class MSGS implements Serializable {
|
407
|
class MSGS implements Serializable {
|
372
|
private MSG MSG;
|
408
|
private MSG MSG;
|
|
@@ -405,6 +441,11 @@ class MSG { |
|
@@ -405,6 +441,11 @@ class MSG { |
405
|
public void setBODY(String BODY) {
|
441
|
public void setBODY(String BODY) {
|
406
|
this.BODY = BODY;
|
442
|
this.BODY = BODY;
|
407
|
}
|
443
|
}
|
|
|
444
|
+
|
|
|
445
|
+ @Override
|
|
|
446
|
+ public String toString() {
|
|
|
447
|
+ return this.BODY;
|
|
|
448
|
+ }
|
408
|
}
|
449
|
}
|
409
|
|
450
|
|
410
|
/**
|
451
|
/**
|