ResendProcessor.java
1.6 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
package com.tianbo.messagebus.service;
import com.tianbo.messagebus.controller.response.ResultJson;
import com.tianbo.messagebus.model.Cache;
import com.tianbo.messagebus.model.MSGS;
import com.tianbo.messagebus.myinterface.KafkaSendApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
/**
* 重发服务
* 从内存数组队列读取报文,并发送直到发送成功为止,
* 代表这个程序不能随便重启了,重启会丢失需要重复发的数据
*/
@Service
@Slf4j
public class ResendProcessor {
private static int TIMES = 20;
@Autowired
KafkaSendApi kafkaSendApi;
public boolean sendmsg(MSGS msgs){
ResultJson response = kafkaSendApi.send(msgs);
if ("200".equals(response.getCode())){
log.info("………………重发消息发送成功{}………………",response.toString());
return true;
}
log.info("400-重发消息发送失败->{}",response.toString());
return false;
}
public void resend(){
if (!Cache.SEND_CACHE.isEmpty()){
log.info("开始从内存读取报文,发送失败的报文,待重发数量->{}",Cache.SEND_CACHE.size());
Cache.SEND_CACHE.removeIf(this::send);
}
}
private boolean send(MSGS msgs){
for (int j = 0; j < TIMES; j++) {
if(sendmsg(msgs)) {
return true;
}
}
return false;
}
}