功能:完善多店铺登录与首次部署SQL
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package com.xiaowang.messagehub.config;
|
||||
|
||||
import org.springframework.amqp.core.*;
|
||||
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
|
||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||
import org.springframework.amqp.core.AcknowledgeMode;
|
||||
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -66,4 +70,15 @@ public class RabbitMQConfig {
|
||||
public Binding emailBinding() {
|
||||
return BindingBuilder.bind(emailQueue()).to(messageExchange()).with(EMAIL_ROUTING_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleRabbitListenerContainerFactory manualAckRabbitListenerContainerFactory(
|
||||
SimpleRabbitListenerContainerFactoryConfigurer configurer,
|
||||
ConnectionFactory connectionFactory
|
||||
) {
|
||||
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
|
||||
configurer.configure(factory, connectionFactory);
|
||||
factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
|
||||
return factory;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.rabbitmq.client.Channel;
|
||||
import com.xiaowang.messagehub.dto.MessageDTO;
|
||||
import com.xiaowang.messagehub.service.EmailSendService;
|
||||
import com.xiaowang.messagehub.service.impl.MessageHandleService;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
@@ -29,9 +30,11 @@ public class EmailQueueListener {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@RabbitListener(queues = "emailQueue")
|
||||
@RabbitListener(queues = "emailQueue", containerFactory = "manualAckRabbitListenerContainerFactory")
|
||||
public void handleEmailMessage(Message message, Channel channel) {
|
||||
String messageId = message.getMessageProperties().getMessageId();
|
||||
LocalDateTime sendTime = LocalDateTime.now();
|
||||
long deliveryTag = message.getMessageProperties().getDeliveryTag();
|
||||
|
||||
try {
|
||||
String messageBody = new String(message.getBody(), StandardCharsets.UTF_8);
|
||||
@@ -42,9 +45,11 @@ public class EmailQueueListener {
|
||||
log.info("========================================");
|
||||
|
||||
MessageDTO messageDTO = objectMapper.readValue(messageBody, MessageDTO.class);
|
||||
boolean valid = messageHandleService.validateAndSaveLog(messageDTO, messageId, LocalDateTime.now());
|
||||
if (!valid) {
|
||||
log.warn("邮件消息校验未通过,消息ID: {}", messageId);
|
||||
MessageHandleService.MessageLogDecision decision =
|
||||
messageHandleService.validateAndPrepareLog(messageDTO, messageId, sendTime);
|
||||
if (!decision.shouldProcess()) {
|
||||
log.warn("邮件消息无需继续处理,消息ID: {}, reason: {}", messageId, decision.reason());
|
||||
acknowledge(channel, deliveryTag);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -65,11 +70,24 @@ public class EmailQueueListener {
|
||||
log.info("收件人: {}", messageDTO.getReceiver());
|
||||
log.info("主题: {}", subject);
|
||||
log.info("========================================");
|
||||
acknowledge(channel, deliveryTag);
|
||||
} catch (JsonProcessingException exception) {
|
||||
log.error("邮件消息解析失败,消息ID: {}", messageId, exception);
|
||||
messageHandleService.markMessageRejected(messageId, "EMAIL", sendTime, "消息体解析失败: " + exception.getOriginalMessage());
|
||||
reject(channel, deliveryTag);
|
||||
} catch (Exception exception) {
|
||||
log.error("处理邮件消息时发生异常,消息ID: {}", messageId, exception);
|
||||
messageHandleService.updatePushLogStatus(messageId, 2, exception.getMessage());
|
||||
} finally {
|
||||
acknowledge(message, channel);
|
||||
MessageHandleService.MessageRetryDecision retryDecision =
|
||||
messageHandleService.recordSendFailure(messageId, "EMAIL", sendTime, exception);
|
||||
if (retryDecision.shouldRetry()) {
|
||||
log.warn("邮件消息准备重试,消息ID: {}, retryCount: {}, reason: {}",
|
||||
messageId, retryDecision.retryCount(), retryDecision.failReason());
|
||||
requeue(channel, deliveryTag);
|
||||
return;
|
||||
}
|
||||
|
||||
messageHandleService.markMessageRejected(messageId, "EMAIL", sendTime, retryDecision.failReason());
|
||||
reject(channel, deliveryTag);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,12 +102,27 @@ public class EmailQueueListener {
|
||||
return messageHandleService.buildFinalContent(messageDTO.getTemplateCode(), messageDTO.getTemplateParams());
|
||||
}
|
||||
|
||||
private void acknowledge(Message message, Channel channel) {
|
||||
private void acknowledge(Channel channel, long deliveryTag) {
|
||||
try {
|
||||
long deliveryTag = message.getMessageProperties().getDeliveryTag();
|
||||
channel.basicAck(deliveryTag, false);
|
||||
} catch (Exception ackException) {
|
||||
log.error("邮件消息确认失败", ackException);
|
||||
}
|
||||
}
|
||||
|
||||
private void requeue(Channel channel, long deliveryTag) {
|
||||
try {
|
||||
channel.basicNack(deliveryTag, false, true);
|
||||
} catch (Exception nackException) {
|
||||
log.error("邮件消息重新入队失败", nackException);
|
||||
}
|
||||
}
|
||||
|
||||
private void reject(Channel channel, long deliveryTag) {
|
||||
try {
|
||||
channel.basicReject(deliveryTag, false);
|
||||
} catch (Exception rejectException) {
|
||||
log.error("邮件消息拒绝失败", rejectException);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.rabbitmq.client.Channel;
|
||||
import com.xiaowang.messagehub.dto.SmsMessageDTO;
|
||||
import com.xiaowang.messagehub.service.SmsSendService;
|
||||
import com.xiaowang.messagehub.service.impl.MessageHandleService;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.amqp.core.Message;
|
||||
@@ -31,10 +32,11 @@ public class SmsQueueListener {
|
||||
this.messageHandleService = messageHandleService;
|
||||
}
|
||||
|
||||
@RabbitListener(queues = "smsQueue")
|
||||
@RabbitListener(queues = "smsQueue", containerFactory = "manualAckRabbitListenerContainerFactory")
|
||||
public void handleSmsMessage(Message message, Channel channel) {
|
||||
String messageId = message.getMessageProperties().getMessageId();
|
||||
LocalDateTime sendTime = LocalDateTime.now();
|
||||
long deliveryTag = message.getMessageProperties().getDeliveryTag();
|
||||
|
||||
try {
|
||||
String messageBody = new String(message.getBody(), StandardCharsets.UTF_8);
|
||||
@@ -45,9 +47,11 @@ public class SmsQueueListener {
|
||||
logger.info("========================================");
|
||||
|
||||
SmsMessageDTO smsMessageDTO = objectMapper.readValue(messageBody, SmsMessageDTO.class);
|
||||
boolean valid = messageHandleService.validateAndSaveLog(smsMessageDTO, messageId, sendTime);
|
||||
if (!valid) {
|
||||
logger.warn("短信消息校验未通过,messageId: {}", messageId);
|
||||
MessageHandleService.MessageLogDecision decision =
|
||||
messageHandleService.validateAndPrepareLog(smsMessageDTO, messageId, sendTime);
|
||||
if (!decision.shouldProcess()) {
|
||||
logger.warn("短信消息无需继续处理,messageId: {}, reason: {}", messageId, decision.reason());
|
||||
acknowledge(channel, deliveryTag);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -64,20 +68,48 @@ public class SmsQueueListener {
|
||||
logger.info("手机号: {}", smsMessageDTO.getReceiver());
|
||||
logger.info("发送方式: {}", smsMessageDTO.getSendMethod());
|
||||
logger.info("========================================");
|
||||
acknowledge(channel, deliveryTag);
|
||||
} catch (JsonProcessingException exception) {
|
||||
logger.error("短信消息解析失败,消息ID: {}", messageId, exception);
|
||||
messageHandleService.markMessageRejected(messageId, "SMS", sendTime, "消息体解析失败: " + exception.getOriginalMessage());
|
||||
reject(channel, deliveryTag);
|
||||
} catch (Exception exception) {
|
||||
logger.error("处理短信消息时发生异常,消息ID: {}", messageId, exception);
|
||||
messageHandleService.updatePushLogStatus(messageId, 2, exception.getMessage());
|
||||
} finally {
|
||||
acknowledge(message, channel);
|
||||
MessageHandleService.MessageRetryDecision retryDecision =
|
||||
messageHandleService.recordSendFailure(messageId, "SMS", sendTime, exception);
|
||||
if (retryDecision.shouldRetry()) {
|
||||
logger.warn("短信消息准备重试,messageId: {}, retryCount: {}, reason: {}",
|
||||
messageId, retryDecision.retryCount(), retryDecision.failReason());
|
||||
requeue(channel, deliveryTag);
|
||||
return;
|
||||
}
|
||||
|
||||
messageHandleService.markMessageRejected(messageId, "SMS", sendTime, retryDecision.failReason());
|
||||
reject(channel, deliveryTag);
|
||||
}
|
||||
}
|
||||
|
||||
private void acknowledge(Message message, Channel channel) {
|
||||
private void acknowledge(Channel channel, long deliveryTag) {
|
||||
try {
|
||||
long deliveryTag = message.getMessageProperties().getDeliveryTag();
|
||||
channel.basicAck(deliveryTag, false);
|
||||
} catch (Exception ackException) {
|
||||
logger.error("短信消息确认失败", ackException);
|
||||
}
|
||||
}
|
||||
|
||||
private void requeue(Channel channel, long deliveryTag) {
|
||||
try {
|
||||
channel.basicNack(deliveryTag, false, true);
|
||||
} catch (Exception nackException) {
|
||||
logger.error("短信消息重新入队失败", nackException);
|
||||
}
|
||||
}
|
||||
|
||||
private void reject(Channel channel, long deliveryTag) {
|
||||
try {
|
||||
channel.basicReject(deliveryTag, false);
|
||||
} catch (Exception rejectException) {
|
||||
logger.error("短信消息拒绝失败", rejectException);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ public interface PushLogMapper extends BaseMapper<PushLogDO> {
|
||||
* @param uniqueId 唯一标识符
|
||||
* @return 推送日志对象
|
||||
*/
|
||||
@Select("SELECT * FROM push_log WHERE unique_id = #{uniqueId}")
|
||||
@Select("SELECT * FROM push_log WHERE unique_id = #{uniqueId} ORDER BY create_time DESC LIMIT 1")
|
||||
PushLogDO selectByUniqueId(@Param("uniqueId") String uniqueId);
|
||||
|
||||
/**
|
||||
|
||||
+13
-6
@@ -11,6 +11,7 @@ import org.springframework.amqp.core.MessageProperties;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -38,7 +39,7 @@ public class MessageProducerService {
|
||||
try {
|
||||
// 生成唯一消息ID
|
||||
String messageId = UUID.randomUUID().toString();
|
||||
messageDTO.setUniqueId(messageId);
|
||||
ensureUniqueId(messageDTO, messageId);
|
||||
|
||||
// 将消息对象转换为JSON字符串
|
||||
String messageBody = objectMapper.writeValueAsString(messageDTO);
|
||||
@@ -74,7 +75,7 @@ public class MessageProducerService {
|
||||
public void sendEmailMessage(MessageDTO messageDTO) {
|
||||
try {
|
||||
String messageId = UUID.randomUUID().toString();
|
||||
messageDTO.setUniqueId(messageId);
|
||||
ensureUniqueId(messageDTO, messageId);
|
||||
|
||||
String messageBody = objectMapper.writeValueAsString(messageDTO);
|
||||
|
||||
@@ -105,7 +106,7 @@ public class MessageProducerService {
|
||||
public void sendWeChatMessage(MessageDTO messageDTO) {
|
||||
try {
|
||||
String messageId = UUID.randomUUID().toString();
|
||||
messageDTO.setUniqueId(messageId);
|
||||
ensureUniqueId(messageDTO, messageId);
|
||||
|
||||
String messageBody = objectMapper.writeValueAsString(messageDTO);
|
||||
|
||||
@@ -136,7 +137,7 @@ public class MessageProducerService {
|
||||
public void sendPushMessage(MessageDTO messageDTO) {
|
||||
try {
|
||||
String messageId = UUID.randomUUID().toString();
|
||||
messageDTO.setUniqueId(messageId);
|
||||
ensureUniqueId(messageDTO, messageId);
|
||||
|
||||
String messageBody = objectMapper.writeValueAsString(messageDTO);
|
||||
|
||||
@@ -168,7 +169,7 @@ public class MessageProducerService {
|
||||
public void sendMessage(MessageDTO messageDTO) {
|
||||
try {
|
||||
String messageId = UUID.randomUUID().toString();
|
||||
messageDTO.setUniqueId(messageId);
|
||||
ensureUniqueId(messageDTO, messageId);
|
||||
|
||||
String messageBody = objectMapper.writeValueAsString(messageDTO);
|
||||
|
||||
@@ -207,4 +208,10 @@ public class MessageProducerService {
|
||||
throw new RuntimeException("发送消息失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureUniqueId(MessageDTO messageDTO, String fallbackMessageId) {
|
||||
if (messageDTO != null && !StringUtils.hasText(messageDTO.getUniqueId())) {
|
||||
messageDTO.setUniqueId(fallbackMessageId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+212
-8
@@ -7,6 +7,7 @@ import com.xiaowang.messagehub.entity.PushTemplateDO;
|
||||
import com.xiaowang.messagehub.mapper.PushLogMapper;
|
||||
import com.xiaowang.messagehub.mapper.PushTemplateMapper;
|
||||
import com.xiaowang.messagehub.service.source.MessageSourceSystemService;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -26,6 +27,7 @@ import java.util.stream.Collectors;
|
||||
public class MessageHandleService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MessageHandleService.class);
|
||||
private static final int MAX_RETRY_COUNT = 3;
|
||||
|
||||
private final Validator validator;
|
||||
private final PushLogMapper pushLogMapper;
|
||||
@@ -42,24 +44,46 @@ public class MessageHandleService {
|
||||
this.sourceSystemService = sourceSystemService;
|
||||
}
|
||||
|
||||
public boolean validateAndSaveLog(MessageDTO messageDTO, String messageId, LocalDateTime sendTime) {
|
||||
PushLogDO pushLog = buildPushLog(messageDTO, messageId, sendTime);
|
||||
public MessageLogDecision validateAndPrepareLog(MessageDTO messageDTO, String messageId, LocalDateTime sendTime) {
|
||||
String failReason = validateMessage(messageDTO);
|
||||
|
||||
if (failReason != null) {
|
||||
pushLog.setStatus(2);
|
||||
pushLog.setFailReason(failReason);
|
||||
pushLogMapper.insert(pushLog);
|
||||
saveOrUpdateRejectedLog(buildPushLog(messageDTO, messageId, sendTime), failReason);
|
||||
logger.warn("消息校验失败,messageId: {}, reason: {}", messageId, failReason);
|
||||
return false;
|
||||
return MessageLogDecision.reject(failReason);
|
||||
}
|
||||
|
||||
PushLogDO successLog = findSuccessfulLogByUniqueId(messageDTO, messageId);
|
||||
if (successLog != null) {
|
||||
logger.info("检测到重复业务消息,跳过重复发送,messageId: {}, uniqueId: {}, existingLogId: {}",
|
||||
messageId, messageDTO.getUniqueId(), successLog.getId());
|
||||
return MessageLogDecision.skip("同一业务消息已成功处理");
|
||||
}
|
||||
|
||||
PushLogDO existingLog = StringUtils.hasText(messageId) ? pushLogMapper.selectById(messageId) : null;
|
||||
if (existingLog != null) {
|
||||
if (Integer.valueOf(1).equals(existingLog.getStatus())) {
|
||||
logger.info("消息已成功处理过,直接确认,messageId: {}", messageId);
|
||||
return MessageLogDecision.skip("消息已成功处理");
|
||||
}
|
||||
if (hasReachedMaxRetry(existingLog)) {
|
||||
logger.warn("消息已达到最大重试次数,直接丢弃,messageId: {}, retryCount: {}",
|
||||
messageId, normalizeRetryCount(existingLog.getRetryCount()));
|
||||
return MessageLogDecision.reject("消息已达到最大重试次数");
|
||||
}
|
||||
refreshPendingLog(existingLog, messageDTO, sendTime);
|
||||
logger.info("检测到重复投递消息,继续按重试流程处理,messageId: {}, retryCount: {}",
|
||||
messageId, normalizeRetryCount(existingLog.getRetryCount()));
|
||||
return MessageLogDecision.process();
|
||||
}
|
||||
|
||||
PushLogDO pushLog = buildPushLog(messageDTO, messageId, sendTime);
|
||||
pushLog.setStatus(0);
|
||||
pushLog.setFailReason(null);
|
||||
pushLogMapper.insert(pushLog);
|
||||
insertPendingLog(pushLog);
|
||||
logger.info("消息校验通过,messageId: {}, sourceSystem: {}, channelType: {}",
|
||||
messageId, messageDTO.getSourceSystem(), messageDTO.getChannelType());
|
||||
return true;
|
||||
return MessageLogDecision.process();
|
||||
}
|
||||
|
||||
public PushTemplateDO getTemplateByCode(String templateCode) {
|
||||
@@ -86,6 +110,9 @@ public class MessageHandleService {
|
||||
}
|
||||
|
||||
public void markSendSuccess(String messageId, String finalContent) {
|
||||
if (!StringUtils.hasText(messageId)) {
|
||||
return;
|
||||
}
|
||||
PushLogDO pushLog = new PushLogDO();
|
||||
pushLog.setId(messageId);
|
||||
pushLog.setStatus(1);
|
||||
@@ -95,6 +122,9 @@ public class MessageHandleService {
|
||||
}
|
||||
|
||||
public void updatePushLogStatus(String messageId, Integer status, String failReason) {
|
||||
if (!StringUtils.hasText(messageId)) {
|
||||
return;
|
||||
}
|
||||
PushLogDO pushLog = new PushLogDO();
|
||||
pushLog.setId(messageId);
|
||||
pushLog.setStatus(status);
|
||||
@@ -102,6 +132,51 @@ public class MessageHandleService {
|
||||
pushLogMapper.updateById(pushLog);
|
||||
}
|
||||
|
||||
public MessageRetryDecision recordSendFailure(String messageId,
|
||||
String channelType,
|
||||
LocalDateTime sendTime,
|
||||
Exception exception) {
|
||||
String failReason = buildFailReason(exception);
|
||||
if (!StringUtils.hasText(messageId)) {
|
||||
logger.warn("消息缺少 messageId,无法记录重试状态,reason: {}", failReason);
|
||||
return MessageRetryDecision.reject(failReason, MAX_RETRY_COUNT);
|
||||
}
|
||||
|
||||
PushLogDO existingLog = pushLogMapper.selectById(messageId);
|
||||
if (existingLog == null) {
|
||||
PushLogDO fallbackLog = new PushLogDO();
|
||||
fallbackLog.setId(messageId);
|
||||
fallbackLog.setChannelType(channelType);
|
||||
fallbackLog.setSendTime(sendTime);
|
||||
fallbackLog.setRetryCount(0);
|
||||
fallbackLog.setStatus(0);
|
||||
fallbackLog.setFailReason(null);
|
||||
insertPendingLog(fallbackLog);
|
||||
existingLog = pushLogMapper.selectById(messageId);
|
||||
}
|
||||
|
||||
int nextRetryCount = normalizeRetryCount(existingLog == null ? null : existingLog.getRetryCount()) + 1;
|
||||
boolean shouldRetry = nextRetryCount < MAX_RETRY_COUNT;
|
||||
pushLogMapper.updateRetryStatus(messageId, shouldRetry ? 0 : 2, failReason);
|
||||
return shouldRetry
|
||||
? MessageRetryDecision.retry(failReason, nextRetryCount)
|
||||
: MessageRetryDecision.reject(failReason, nextRetryCount);
|
||||
}
|
||||
|
||||
public void markMessageRejected(String messageId,
|
||||
String channelType,
|
||||
LocalDateTime sendTime,
|
||||
String failReason) {
|
||||
PushLogDO pushLog = new PushLogDO();
|
||||
pushLog.setId(messageId);
|
||||
pushLog.setChannelType(channelType);
|
||||
pushLog.setSendTime(sendTime);
|
||||
pushLog.setStatus(2);
|
||||
pushLog.setRetryCount(0);
|
||||
pushLog.setFailReason(failReason);
|
||||
saveOrUpdateRejectedLog(pushLog, failReason);
|
||||
}
|
||||
|
||||
public String replaceTemplatePlaceholders(String contentPattern, Map<String, Object> templateParams) {
|
||||
if (!StringUtils.hasText(contentPattern)) {
|
||||
return contentPattern;
|
||||
@@ -134,6 +209,109 @@ public class MessageHandleService {
|
||||
return pushLog;
|
||||
}
|
||||
|
||||
private PushLogDO findSuccessfulLogByUniqueId(MessageDTO messageDTO, String currentMessageId) {
|
||||
if (messageDTO == null || !StringUtils.hasText(messageDTO.getUniqueId())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
PushLogDO existingLog = pushLogMapper.selectByUniqueId(messageDTO.getUniqueId());
|
||||
if (existingLog == null) {
|
||||
return null;
|
||||
}
|
||||
if (StringUtils.hasText(currentMessageId) && currentMessageId.equals(existingLog.getId())) {
|
||||
return null;
|
||||
}
|
||||
return Integer.valueOf(1).equals(existingLog.getStatus()) ? existingLog : null;
|
||||
}
|
||||
|
||||
private void insertPendingLog(PushLogDO pushLog) {
|
||||
if (pushLog == null || !StringUtils.hasText(pushLog.getId())) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
pushLogMapper.insert(pushLog);
|
||||
} catch (DuplicateKeyException duplicateKeyException) {
|
||||
logger.info("push_log 已存在,按幂等处理,messageId: {}", pushLog.getId());
|
||||
}
|
||||
}
|
||||
|
||||
private void saveOrUpdateRejectedLog(PushLogDO pushLog, String failReason) {
|
||||
if (pushLog == null || !StringUtils.hasText(pushLog.getId())) {
|
||||
return;
|
||||
}
|
||||
|
||||
pushLog.setStatus(2);
|
||||
pushLog.setFailReason(failReason);
|
||||
PushLogDO existingLog = pushLogMapper.selectById(pushLog.getId());
|
||||
if (existingLog == null) {
|
||||
insertPendingLog(pushLog);
|
||||
PushLogDO rejectedLog = new PushLogDO();
|
||||
rejectedLog.setId(pushLog.getId());
|
||||
rejectedLog.setStatus(2);
|
||||
rejectedLog.setFailReason(failReason);
|
||||
pushLogMapper.updateById(rejectedLog);
|
||||
return;
|
||||
}
|
||||
|
||||
existingLog.setStatus(2);
|
||||
existingLog.setFailReason(failReason);
|
||||
existingLog.setSendTime(pushLog.getSendTime());
|
||||
if (StringUtils.hasText(pushLog.getSourceSystem())) {
|
||||
existingLog.setSourceSystem(pushLog.getSourceSystem());
|
||||
}
|
||||
if (StringUtils.hasText(pushLog.getUniqueId())) {
|
||||
existingLog.setUniqueId(pushLog.getUniqueId());
|
||||
}
|
||||
if (StringUtils.hasText(pushLog.getReceiver())) {
|
||||
existingLog.setReceiver(pushLog.getReceiver());
|
||||
}
|
||||
if (StringUtils.hasText(pushLog.getChannelType())) {
|
||||
existingLog.setChannelType(pushLog.getChannelType());
|
||||
}
|
||||
if (StringUtils.hasText(pushLog.getMsgType())) {
|
||||
existingLog.setMsgType(pushLog.getMsgType());
|
||||
}
|
||||
if (StringUtils.hasText(pushLog.getTemplateCode())) {
|
||||
existingLog.setTemplateCode(pushLog.getTemplateCode());
|
||||
}
|
||||
if (StringUtils.hasText(pushLog.getTemplateParams())) {
|
||||
existingLog.setTemplateParams(pushLog.getTemplateParams());
|
||||
}
|
||||
pushLogMapper.updateById(existingLog);
|
||||
}
|
||||
|
||||
private void refreshPendingLog(PushLogDO existingLog, MessageDTO messageDTO, LocalDateTime sendTime) {
|
||||
existingLog.setSourceSystem(messageDTO.getSourceSystem());
|
||||
existingLog.setUniqueId(messageDTO.getUniqueId());
|
||||
existingLog.setReceiver(messageDTO.getReceiver());
|
||||
existingLog.setChannelType(messageDTO.getChannelType());
|
||||
existingLog.setMsgType(messageDTO.getMsgType());
|
||||
existingLog.setTemplateCode(messageDTO.getTemplateCode());
|
||||
existingLog.setTemplateParams(messageDTO.getTemplateParams() != null ? messageDTO.getTemplateParams().toString() : null);
|
||||
existingLog.setSendTime(sendTime);
|
||||
existingLog.setStatus(0);
|
||||
existingLog.setFailReason(null);
|
||||
pushLogMapper.updateById(existingLog);
|
||||
}
|
||||
|
||||
private boolean hasReachedMaxRetry(PushLogDO pushLogDO) {
|
||||
return normalizeRetryCount(pushLogDO == null ? null : pushLogDO.getRetryCount()) >= MAX_RETRY_COUNT;
|
||||
}
|
||||
|
||||
private int normalizeRetryCount(Integer retryCount) {
|
||||
return retryCount == null ? 0 : retryCount;
|
||||
}
|
||||
|
||||
private String buildFailReason(Exception exception) {
|
||||
if (exception == null) {
|
||||
return "未知异常";
|
||||
}
|
||||
if (StringUtils.hasText(exception.getMessage())) {
|
||||
return exception.getMessage();
|
||||
}
|
||||
return exception.getClass().getSimpleName();
|
||||
}
|
||||
|
||||
private String validateMessage(MessageDTO messageDTO) {
|
||||
Set<ConstraintViolation<MessageDTO>> violations = validator.validate(messageDTO);
|
||||
if (!violations.isEmpty()) {
|
||||
@@ -166,4 +344,30 @@ public class MessageHandleService {
|
||||
private boolean isValidEmail(String receiver) {
|
||||
return receiver != null && receiver.matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$");
|
||||
}
|
||||
|
||||
public record MessageLogDecision(boolean shouldProcess, boolean shouldRequeue, String reason) {
|
||||
|
||||
public static MessageLogDecision process() {
|
||||
return new MessageLogDecision(true, false, null);
|
||||
}
|
||||
|
||||
public static MessageLogDecision skip(String reason) {
|
||||
return new MessageLogDecision(false, false, reason);
|
||||
}
|
||||
|
||||
public static MessageLogDecision reject(String reason) {
|
||||
return new MessageLogDecision(false, false, reason);
|
||||
}
|
||||
}
|
||||
|
||||
public record MessageRetryDecision(boolean shouldRetry, String failReason, int retryCount) {
|
||||
|
||||
public static MessageRetryDecision retry(String failReason, int retryCount) {
|
||||
return new MessageRetryDecision(true, failReason, retryCount);
|
||||
}
|
||||
|
||||
public static MessageRetryDecision reject(String failReason, int retryCount) {
|
||||
return new MessageRetryDecision(false, failReason, retryCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user