招标解析异步任务

This commit is contained in:
cwchen 2025-11-25 13:26:09 +08:00
parent 10c49e55bc
commit 155a5ab913
1 changed files with 8 additions and 13 deletions

View File

@ -6,7 +6,6 @@ import com.bonus.common.domain.rabbitmq.dto.RabbitMqMessage;
import com.bonus.file.config.MinioConfig; import com.bonus.file.config.MinioConfig;
import com.bonus.file.util.MinioUtil; import com.bonus.file.util.MinioUtil;
import com.bonus.ocr.service.OcrService; import com.bonus.ocr.service.OcrService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.amqp.rabbit.annotation.RabbitListener;
@ -39,8 +38,6 @@ public class RabbitMQConsumerService {
@Resource @Resource
private MinioUtil minioUtil; private MinioUtil minioUtil;
private final ObjectMapper objectMapper = new ObjectMapper();
/** /**
* 主消息消费者 - 使用手动确认模式 * 主消息消费者 - 使用手动确认模式
*/ */
@ -84,7 +81,7 @@ public class RabbitMQConsumerService {
log.info("消息处理完成并确认 - ID: {}, 投递标签: {}", messageId, deliveryTag); log.info("消息处理完成并确认 - ID: {}, 投递标签: {}", messageId, deliveryTag);
} catch (BusinessException e) { } catch (BusinessException e) {
// 业务异常记录日志但不重试 // 业务异常记录日志但不重试直接拒绝消息并不重新入队
log.error("业务异常,拒绝消息并不重新入队 - ID: {}", messageId, e); log.error("业务异常,拒绝消息并不重新入队 - ID: {}", messageId, e);
try { try {
channel.basicReject(deliveryTag, false); channel.basicReject(deliveryTag, false);
@ -92,14 +89,11 @@ public class RabbitMQConsumerService {
log.error("拒绝消息失败 - ID: {}", messageId, ioException); log.error("拒绝消息失败 - ID: {}", messageId, ioException);
} }
} catch (Exception e) { } catch (Exception e) {
// 其他异常记录日志并重新入队进行重试 // 其他异常直接抛出异常让重试拦截器处理
log.error("处理消息异常,将重新入队 - ID: {}", messageId, e); // 关键修复不要手动调用 basicReject(deliveryTag, true)否则会绕过重试拦截器的重试次数限制
try { // 重试拦截器会在重试次数耗尽后调用 RejectAndDontRequeueRecoverer它会拒绝消息并不重新入队
channel.basicReject(deliveryTag, true); log.error("处理消息异常,将触发重试机制 - ID: {}, 异常: {}", messageId, e.getMessage(), e);
} catch (IOException ioException) { // 直接抛出异常让重试拦截器处理重试逻辑
log.error("拒绝消息失败 - ID: {}", messageId, ioException);
}
// 重新抛出异常让重试拦截器处理
throw new RuntimeException("消息处理失败,需要重试: " + e.getMessage(), e); throw new RuntimeException("消息处理失败,需要重试: " + e.getMessage(), e);
} }
} }
@ -279,7 +273,8 @@ public class RabbitMQConsumerService {
private OcrResponse performOcrRecognition(File file) { private OcrResponse performOcrRecognition(File file) {
try { try {
OcrRequest ocrRequest = buildOcrRequest(file); OcrRequest ocrRequest = buildOcrRequest(file);
OcrResponse ocrResponse = ocrService.callOcrService(ocrRequest); // OcrResponse ocrResponse = ocrService.callOcrService(ocrRequest);
OcrResponse ocrResponse = null;
// 修复检查 OCR 响应是否为 null // 修复检查 OCR 响应是否为 null
if (Objects.isNull(ocrResponse)) { if (Objects.isNull(ocrResponse)) {
throw new BusinessException("OCR服务返回结果为空"); throw new BusinessException("OCR服务返回结果为空");