...
|
...
|
@@ -3,7 +3,14 @@ package com.tianbo.messagebus.service; |
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.tianbo.messagebus.controller.response.ResultJson;
|
|
|
import com.tianbo.messagebus.model.HEADER;
|
|
|
import com.tianbo.messagebus.model.MSG;
|
|
|
import com.tianbo.messagebus.model.MSGS;
|
|
|
import com.tianbo.messagebus.myinterface.KafkaReciveApi;
|
|
|
import com.tianbo.messagebus.myinterface.KafkaSendApi;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.*;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
...
|
...
|
@@ -13,15 +20,14 @@ import org.springframework.stereotype.Service; |
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.client.HttpClientErrorException;
|
|
|
import org.springframework.web.client.RestClientException;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.Serializable;
|
|
|
import java.util.List;
|
|
|
|
|
|
@Service
|
|
|
@EnableAsync
|
|
|
//@EnableAsync
|
|
|
@Slf4j
|
|
|
public class MessageBusProcessor {
|
|
|
|
...
|
...
|
@@ -74,7 +80,7 @@ public class MessageBusProcessor { |
|
|
/**
|
|
|
* 失败重发请求次数
|
|
|
*/
|
|
|
private static final int RETRY_TIMES= 3;
|
|
|
private static final int RETRY_TIMES= 100;
|
|
|
|
|
|
/**
|
|
|
* HTTP请求框架
|
...
|
...
|
@@ -82,6 +88,12 @@ public class MessageBusProcessor { |
|
|
@Resource
|
|
|
private RestTemplate restTemplate;
|
|
|
|
|
|
@Autowired
|
|
|
KafkaReciveApi kafkaReciveApi;
|
|
|
|
|
|
@Autowired
|
|
|
KafkaSendApi kafkaSendApi;
|
|
|
|
|
|
/**
|
|
|
* 发起登录,存储token
|
|
|
*
|
...
|
...
|
@@ -192,7 +204,7 @@ public class MessageBusProcessor { |
|
|
|
|
|
return code;
|
|
|
}
|
|
|
@Scheduled(fixedDelay = 10000)
|
|
|
// @Scheduled(fixedDelay = 10000)
|
|
|
public void heartBit() {
|
|
|
if (!StringUtils.isEmpty(TOKEN) && LOGIN_STATUS){
|
|
|
/*
|
...
|
...
|
@@ -237,7 +249,7 @@ public class MessageBusProcessor { |
|
|
public Boolean sendMsg(MSG msg) {
|
|
|
if (LOGIN_STATUS) {
|
|
|
try{
|
|
|
log.info("开始转发消息:{}",msg.toString());
|
|
|
log.info("………………开始发送消息:{}………………",msg.toString());
|
|
|
/*
|
|
|
* 发起HTTP 登录请求
|
|
|
* 登录接口的请求头为application/json
|
...
|
...
|
@@ -277,7 +289,7 @@ public class MessageBusProcessor { |
|
|
JSONObject resJson = JSON.parseObject(response.getBody());
|
|
|
String code = resJson.getString("code");
|
|
|
if ("200".equals(code)) {
|
|
|
log.info("消息发送成功");
|
|
|
log.info("………………消息发送成功………………");
|
|
|
return true;
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -300,7 +312,7 @@ public class MessageBusProcessor { |
|
|
* @return
|
|
|
*/
|
|
|
// @Async
|
|
|
@Scheduled(fixedRate = 1000)
|
|
|
// @Scheduled(fixedRate = 1000)
|
|
|
public JSONArray getMsg() {
|
|
|
if(!LOGIN_STATUS){
|
|
|
login();
|
...
|
...
|
@@ -326,7 +338,7 @@ public class MessageBusProcessor { |
|
|
*/
|
|
|
ResponseEntity<String> response = restTemplate.postForEntity(GET_MSG_URL, request, String.class);
|
|
|
// 输出结果
|
|
|
|
|
|
log.info("接口访问结果:{}",response);
|
|
|
|
|
|
if (response.getStatusCode().equals(HttpStatus.OK)) {
|
|
|
/*
|
...
|
...
|
@@ -343,7 +355,7 @@ public class MessageBusProcessor { |
|
|
取得是大数据小组的实体,他们的msg.body的封装是以对象实体object封装的。不是json字符串。
|
|
|
*/
|
|
|
String msg = data.getObject(i,String.class);
|
|
|
log.info("开始转发消息---{}---",msg);
|
|
|
log.info("循环处理消息[{}]---{}---",i,msg);
|
|
|
JSONObject rootJson = JSON.parseObject(msg);
|
|
|
JSONObject msgJson = rootJson.getJSONObject("MSG");
|
|
|
JSONObject body = msgJson.getJSONObject("BODY");
|
...
|
...
|
@@ -384,178 +396,111 @@ public class MessageBusProcessor { |
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 读取备份消息并消息重发
|
|
|
* @return
|
|
|
* feigin从服务直接获取消息
|
|
|
*/
|
|
|
public void reSend(MSG msg){
|
|
|
log.error("***进入重发***");
|
|
|
for (int i = 0; i < RETRY_TIMES; i++) {
|
|
|
Boolean sendResult = sendMsg(msg);
|
|
|
if (sendResult){
|
|
|
log.error("***重发成功***");
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
log.error("***已尝试重发三次,重发失败***");
|
|
|
}
|
|
|
@Scheduled(fixedRate = 1000)
|
|
|
public void getDataFromFeigin(){
|
|
|
|
|
|
log.info("1-开始执行获取任务");
|
|
|
ResultJson<List<String>> listResultJson = kafkaReciveApi.recive("HYYW");
|
|
|
log.info("2-获取结果为:{},数量为:{}",listResultJson.toString(),listResultJson.getData().size());
|
|
|
if ("200".equals(listResultJson.getCode()) && listResultJson.getData()!=null && listResultJson.getData().size()>0){
|
|
|
log.info("3-开始处理获取数据");
|
|
|
List<String> dataList = listResultJson.getData();
|
|
|
for (int i = 0; i <dataList.size() ; i++) {
|
|
|
String msg = dataList.get(i);
|
|
|
log.info("4-循环处理消息[{}]--->{}<---",i,msg);
|
|
|
JSONObject rootJson = JSON.parseObject(msg);
|
|
|
JSONObject msgJson = rootJson.getJSONObject("MSG");
|
|
|
JSONObject body = msgJson.getJSONObject("BODY");
|
|
|
|
|
|
}
|
|
|
HEADER msgHeader = msgJson.getObject("HEADER",HEADER.class);
|
|
|
|
|
|
/**
|
|
|
* 消息实体类
|
|
|
*/
|
|
|
class MSGS implements Serializable {
|
|
|
private MSG MSG;
|
|
|
MSG transMsg= new MSG();
|
|
|
String transBody = body.toJSONString();
|
|
|
transMsg.setHEADER(msgHeader);
|
|
|
transMsg.setBODY(transBody);
|
|
|
|
|
|
public MSG getMSG() {
|
|
|
return MSG;
|
|
|
log.info("5-开始转发消息");
|
|
|
boolean sendResult = sendMsgByFeign(transMsg);
|
|
|
if(!sendResult){
|
|
|
log.error("!!!!!!消息--->{}<---转发失败!!!!!!,尝试重发",transMsg.toString());
|
|
|
//todo:消息备份或者重发?
|
|
|
reTrySend(transMsg);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void setMSG(MSG MSG) {
|
|
|
this.MSG = MSG;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class MSG {
|
|
|
/**
|
|
|
* 具体消息头部信息
|
|
|
* feign从服务直接发送消息
|
|
|
*/
|
|
|
private HEADER HEADER;
|
|
|
/**
|
|
|
* 具体消息支持JSON字符串或者XML
|
|
|
*/
|
|
|
private String BODY;
|
|
|
|
|
|
public HEADER getHEADER() {
|
|
|
return HEADER;
|
|
|
}
|
|
|
public boolean sendMsgByFeign(MSG msg){
|
|
|
MSGS msgs = new MSGS();
|
|
|
msg.getHEADER().setSNDR("HYYW");
|
|
|
|
|
|
public void setHEADER(HEADER HEADER) {
|
|
|
this.HEADER = HEADER;
|
|
|
}
|
|
|
msgs.setMSG(msg);
|
|
|
|
|
|
public String getBODY() {
|
|
|
return BODY;
|
|
|
}
|
|
|
ResultJson response = kafkaSendApi.send(msgs);
|
|
|
|
|
|
public void setBODY(String BODY) {
|
|
|
this.BODY = BODY;
|
|
|
if ("200".equals(response.getCode())){
|
|
|
log.info("………………6-消息发送成功{}………………",response.toString());
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String toString() {
|
|
|
return this.BODY;
|
|
|
log.info("400-消息发送失败->{}",response.toString());
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 消息发送头部信息
|
|
|
*/
|
|
|
class HEADER {
|
|
|
/**
|
|
|
* 消息唯一标识,可以是ID
|
|
|
*/
|
|
|
private long SEQNO;
|
|
|
/**
|
|
|
* 发送的消息大类,在总线系统配置好后,分配给消息生产者
|
|
|
* feign重发消息
|
|
|
*/
|
|
|
private String TYPE;
|
|
|
/**
|
|
|
* 发送的消息子类,在总线系统配置好后,分配给消息生产者
|
|
|
*/
|
|
|
private String STYPE;
|
|
|
/**
|
|
|
* 消息增删改类型(IE=insert event,UE=update event,DE=delete event)
|
|
|
*/
|
|
|
private String OPTYPE;
|
|
|
/**
|
|
|
* 发送时间(如:20210320101421/yyyyMMddHHmmss)
|
|
|
*/
|
|
|
private String DDTM;
|
|
|
/**
|
|
|
* 你的账号名称
|
|
|
*/
|
|
|
private String SNDR;
|
|
|
/**
|
|
|
* 消息接收者
|
|
|
*/
|
|
|
private String RCVR;
|
|
|
|
|
|
public long getSEQNO() {
|
|
|
return SEQNO;
|
|
|
public void reTrySend(MSG msg){
|
|
|
log.error("***进入重发***");
|
|
|
for (int i = 0; i < RETRY_TIMES; i++) {
|
|
|
Boolean sendResult = sendMsgByFeign(msg);
|
|
|
if (sendResult){
|
|
|
log.error("***重发成功***");
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
public void setSEQNO(long SEQNO) {
|
|
|
this.SEQNO = SEQNO;
|
|
|
if (i>85){
|
|
|
log.error("***重发{}次未成功,执行重新登录尝试***",i);
|
|
|
login();
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
public String getTYPE() {
|
|
|
return TYPE;
|
|
|
log.error("***已尝试重发>>>{}<<<次,重发失败***",i);
|
|
|
}
|
|
|
|
|
|
public void setTYPE(String TYPE) {
|
|
|
this.TYPE = TYPE;
|
|
|
}
|
|
|
|
|
|
public String getSTYPE() {
|
|
|
return STYPE;
|
|
|
/**
|
|
|
* 读取备份消息并消息重发
|
|
|
* @return
|
|
|
*/
|
|
|
public void reSend(MSG msg){
|
|
|
log.error("***进入重发***");
|
|
|
for (int i = 0; i < RETRY_TIMES; i++) {
|
|
|
Boolean sendResult = sendMsg(msg);
|
|
|
if (sendResult){
|
|
|
log.error("***重发成功***");
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
public void setSTYPE(String STYPE) {
|
|
|
this.STYPE = STYPE;
|
|
|
if (i>85){
|
|
|
log.error("***重发{}次未成功,执行重新登录尝试***",i);
|
|
|
login();
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
public String getOPTYPE() {
|
|
|
return OPTYPE;
|
|
|
log.error("***已尝试重发>>>{}<<<次,重发失败***",i);
|
|
|
}
|
|
|
|
|
|
public void setOPTYPE(String OPTYPE) {
|
|
|
this.OPTYPE = OPTYPE;
|
|
|
}
|
|
|
|
|
|
public String getDDTM() {
|
|
|
return DDTM;
|
|
|
}
|
|
|
|
|
|
public void setDDTM(String DDTM) {
|
|
|
this.DDTM = DDTM;
|
|
|
}
|
|
|
|
|
|
public String getSNDR() {
|
|
|
return SNDR;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public void setSNDR(String SNDR) {
|
|
|
this.SNDR = SNDR;
|
|
|
}
|
|
|
|
|
|
public String getRCVR() {
|
|
|
return RCVR;
|
|
|
}
|
|
|
|
|
|
public void setRCVR(String RCVR) {
|
|
|
this.RCVR = RCVR;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 收发送接口返回结果实体类
|
|
|
*/
|
|
|
|
|
|
class ResultJson<T> implements Serializable {
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
/**
|
|
|
* 状态码 正确为200,其他为异常
|
|
|
*/
|
|
|
private String code;
|
|
|
/**
|
|
|
* 结果描述
|
|
|
*/
|
|
|
private String msg;
|
|
|
|
|
|
/**
|
|
|
* 结果数据
|
|
|
*/
|
|
|
private T data;
|
|
|
} |
...
|
...
|
|