Browse Source

发送mq消息 如果失败重试1次

dev_shibei_match
jianjun 5 years ago
parent
commit
e6c87975c1
  1. 160
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/SendMqMsgUtils.java

160
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/SendMqMsgUtils.java

@ -28,85 +28,91 @@ import java.util.Map;
@Slf4j
@Component
public class SendMqMsgUtils {
private static MqConfig mqConfig;
private static MqConfig mqConfig;
/**
* desc:发送mq消息
*
* @return
*/
public static Result<String> sendMsg(MqBaseMsgDTO msg) {
if (mqConfig == null) {
mqConfig = SpringContextUtils.getBean(MqConfig.class);
}
log.info("sendMsg param:{}", JSON.toJSONString(msg));
try {
// TODO
ValidatorUtils.validateEntity(msg, DefaultGroup.class);
} catch (ValidateException e) {
return new Result<String>().error(e.getMsg());
}
msg.setAppId(mqConfig.getAppId());
msg.setRequestUrl(mqConfig.getHost().concat(MqMethodPathEnum.SEND_MSG.getCode()));
msg.setToken(mqConfig.getToken());
try {
Result<String> result = HttpClientManager.getInstance().sendPostByHttps(msg.getRequestUrl(), JSON.toJSONString(msg));
log.info("sendMsg result:{}", JSON.toJSONString(result));
if (result.success()) {
MqReturnBaseResult resultResult = JSON.parseObject(result.getData(), MqReturnBaseResult.class);
if (resultResult.getErrCode().equals(NumConstant.ZERO)) {
JSONObject jsonObject = JSON.parseObject(resultResult.getData());
return new Result<String>().ok(jsonObject.getString("msgId"));
} else {
log.error("sendMsg fail,resultData:{}", JSON.toJSONString(resultResult));
return new Result<String>().error(EpmetErrorCode.SERVER_ERROR.getCode(), resultResult.getErrMsg());
}
}
Result<String> resultResult = new Result<>();
resultResult.error(result.getCode(), result.getMsg());
resultResult.setInternalMsg(result.getInternalMsg());
return resultResult;
} catch (Exception e) {
log.error("sendMsg exception", e);
return new Result<String>().error(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg());
}
}
/**
* desc:发送mq消息(如果失败重试1次)
*
* @return
*/
public static Result<String> sendMsg(MqBaseMsgDTO msg) {
if (mqConfig == null) {
mqConfig = SpringContextUtils.getBean(MqConfig.class);
}
log.info("sendMsg param:{}", JSON.toJSONString(msg));
try {
// TODO
ValidatorUtils.validateEntity(msg, DefaultGroup.class);
} catch (ValidateException e) {
return new Result<String>().error(e.getMsg());
}
msg.setAppId(mqConfig.getAppId());
msg.setRequestUrl(mqConfig.getHost().concat(MqMethodPathEnum.SEND_MSG.getCode()));
msg.setToken(mqConfig.getToken());
try {
int retryTime = 0;
Result<String> result = null;
do {
retryTime++;
result = HttpClientManager.getInstance().sendPostByHttps(msg.getRequestUrl(), JSON.toJSONString(msg));
log.info("sendMsg retryTime:{},result:{}", retryTime, JSON.toJSONString(result));
} while ((!result.success()) && retryTime < 2);
if (result.success()) {
MqReturnBaseResult resultResult = JSON.parseObject(result.getData(), MqReturnBaseResult.class);
if (resultResult.getErrCode().equals(NumConstant.ZERO)) {
JSONObject jsonObject = JSON.parseObject(resultResult.getData());
return new Result<String>().ok(jsonObject.getString("msgId"));
} else {
log.error("sendMsg fail,resultData:{}", JSON.toJSONString(resultResult));
return new Result<String>().error(EpmetErrorCode.SERVER_ERROR.getCode(), resultResult.getErrMsg());
}
}
Result<String> resultResult = new Result<>();
resultResult.error(result.getCode(), result.getMsg());
resultResult.setInternalMsg(result.getInternalMsg());
return resultResult;
} catch (Exception e) {
log.error("sendMsg exception", e);
return new Result<String>().error(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg());
}
}
/**
* desc:发送mq消息
*
* @return
*/
public static Result<List<EventClassDto>> getEventClassList() {
if (mqConfig == null) {
mqConfig = SpringContextUtils.getBean(MqConfig.class);
}
Map<String, String> param = new HashMap<>();
param.put("appId", mqConfig.getAppId());
param.put("token", mqConfig.getToken());
String requestUrl = mqConfig.getHost().concat(MqMethodPathEnum.GET_EVENT_LIST.getCode());
try {
Result<String> result = HttpClientManager.getInstance().sendPostByHttps(requestUrl, JSON.toJSONString(param));
log.info("getEventClassList result:{}", JSON.toJSONString(result));
if (result.success()) {
MqReturnBaseResult resultResult = JSON.parseObject(result.getData(), MqReturnBaseResult.class);
if (resultResult.getErrCode().equals(NumConstant.ZERO)) {
List<EventClassDto> eventClassDto = JSON.parseArray(resultResult.getData(), EventClassDto.class);
return new Result<List<EventClassDto>>().ok(eventClassDto);
} else {
log.error("sendMsg fail,resultData:{}", JSON.toJSONString(resultResult));
return new Result<List<EventClassDto>>().error(EpmetErrorCode.SERVER_ERROR.getCode(), resultResult.getErrMsg());
}
}
Result<List<EventClassDto>> resultResult = new Result<>();
resultResult.error(result.getCode(), result.getMsg());
resultResult.setInternalMsg(result.getInternalMsg());
return resultResult;
} catch (Exception e) {
log.error("sendMsg exception", e);
return new Result<List<EventClassDto>>().error(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg());
}
}
/**
* desc:发送mq消息
*
* @return
*/
public static Result<List<EventClassDto>> getEventClassList() {
if (mqConfig == null) {
mqConfig = SpringContextUtils.getBean(MqConfig.class);
}
Map<String, String> param = new HashMap<>();
param.put("appId", mqConfig.getAppId());
param.put("token", mqConfig.getToken());
String requestUrl = mqConfig.getHost().concat(MqMethodPathEnum.GET_EVENT_LIST.getCode());
try {
Result<String> result = HttpClientManager.getInstance().sendPostByHttps(requestUrl, JSON.toJSONString(param));
log.info("getEventClassList result:{}", JSON.toJSONString(result));
if (result.success()) {
MqReturnBaseResult resultResult = JSON.parseObject(result.getData(), MqReturnBaseResult.class);
if (resultResult.getErrCode().equals(NumConstant.ZERO)) {
List<EventClassDto> eventClassDto = JSON.parseArray(resultResult.getData(), EventClassDto.class);
return new Result<List<EventClassDto>>().ok(eventClassDto);
} else {
log.error("sendMsg fail,resultData:{}", JSON.toJSONString(resultResult));
return new Result<List<EventClassDto>>().error(EpmetErrorCode.SERVER_ERROR.getCode(), resultResult.getErrMsg());
}
}
Result<List<EventClassDto>> resultResult = new Result<>();
resultResult.error(result.getCode(), result.getMsg());
resultResult.setInternalMsg(result.getInternalMsg());
return resultResult;
} catch (Exception e) {
log.error("sendMsg exception", e);
return new Result<List<EventClassDto>>().error(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg());
}
}
}

Loading…
Cancel
Save