事务执行消息推送
This commit is contained in:
parent
0e53da8d18
commit
22042eb66e
|
|
@ -66,11 +66,6 @@ public class PrintMqListener {
|
|||
|
||||
@RabbitListener(queues = {"${spring.profiles.active}-" + RabbitConstants.Queue.ORDER_MACHINE_PRINT_QUEUE})
|
||||
public void orderPrint(String req) {
|
||||
// 使用异步线程池执行延迟任务,不阻塞当前消费者线程
|
||||
CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
// 延迟3秒处理
|
||||
TimeUnit.SECONDS.sleep(3);
|
||||
// 执行核心打印逻辑
|
||||
invokeFun("orderPrint", "java.order", req, (data) -> {
|
||||
JSONObject jsonObject = JSONObject.parseObject(data);
|
||||
|
|
@ -84,15 +79,33 @@ public class PrintMqListener {
|
|||
return null;
|
||||
}, RedisCst.getLockKey("orderPrint", orderId));
|
||||
});
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
// 记录中断日志
|
||||
log.warn("打印任务被中断,req:{}", req, e);
|
||||
} catch (Exception e) {
|
||||
// 记录业务异常日志
|
||||
log.error("打印任务处理失败,req:{}", req, e);
|
||||
}
|
||||
}, asyncExecutor);
|
||||
// // 使用异步线程池执行延迟任务,不阻塞当前消费者线程
|
||||
// CompletableFuture.runAsync(() -> {
|
||||
// try {
|
||||
// // 延迟3秒处理
|
||||
// TimeUnit.SECONDS.sleep(3);
|
||||
// // 执行核心打印逻辑
|
||||
// invokeFun("orderPrint", "java.order", req, (data) -> {
|
||||
// JSONObject jsonObject = JSONObject.parseObject(data);
|
||||
// String orderId = jsonObject.getString("orderId");
|
||||
// if (orderId == null) {
|
||||
// throw new RuntimeException("订单打印失败,未传递orderId");
|
||||
// }
|
||||
// Boolean printOrder = jsonObject.getBoolean("printOrder");
|
||||
// funUtil.runFunAndCheckKey(() -> {
|
||||
// printerHandler.handler(orderId, printOrder != null && !printOrder ? PrinterHandler.PrintTypeEnum.ONE : PrinterHandler.PrintTypeEnum.ONE_AND_ORDER);
|
||||
// return null;
|
||||
// }, RedisCst.getLockKey("orderPrint", orderId));
|
||||
// });
|
||||
// } catch (InterruptedException e) {
|
||||
// Thread.currentThread().interrupt();
|
||||
// // 记录中断日志
|
||||
// log.warn("打印任务被中断,req:{}", req, e);
|
||||
// } catch (Exception e) {
|
||||
// // 记录业务异常日志
|
||||
// log.error("打印任务处理失败,req:{}", req, e);
|
||||
// }
|
||||
// }, asyncExecutor);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -55,6 +55,8 @@ import org.apache.dubbo.config.annotation.DubboService;
|
|||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
|
@ -1026,13 +1028,29 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
|
|||
shopTableService.updateById(table);
|
||||
}
|
||||
}
|
||||
orderDetailService.updateChain().set(OrderDetail::getStatus, OrderStatusEnums.DONE.getCode()).eq(OrderDetail::getOrderId, orderInfo.getId()).update();
|
||||
// if (!"after-pay".equals(orderInfo.getPayMode())) {
|
||||
//发送打票信息
|
||||
// 保存消息参数(避免闭包中引用的变量被修改)
|
||||
final Long orderId = orderInfo.getId();
|
||||
final boolean isPrint = orderInfo.getIsPrint() == 1;
|
||||
final String payMode = orderInfo.getPayMode();
|
||||
|
||||
// 【关键改造】注册事务同步器,在事务提交后发送消息
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
|
||||
@Override
|
||||
public void afterCommit() {
|
||||
// 事务成功提交后执行消息发送
|
||||
String printParam = orderId + "_" + (!"after-pay".equals(payMode) ? 1 : 0) + "_1";
|
||||
rabbitPublisher.sendOrderPrintMsg(printParam, isPrint);
|
||||
// log.info("订单{}事务提交后,发送打印消息", orderId);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// 非事务环境下直接发送(兼容无事务场景)
|
||||
String printParam = orderId + "_" + (!"after-pay".equals(payMode) ? 1 : 0) + "_1";
|
||||
rabbitPublisher.sendOrderPrintMsg(printParam, isPrint);
|
||||
// log.info("非事务环境下,直接发送订单{}打印消息", orderId);
|
||||
}
|
||||
|
||||
//orderId_0_0 订单ID_先付后付(1先付0后付)_订单状态 0未完成 1完成
|
||||
rabbitPublisher.sendOrderPrintMsg(orderInfo.getId() + "_" + (!"after-pay".equals(orderInfo.getPayMode()) ? 1 : 0) + "_1", orderInfo.getIsPrint() == 1);
|
||||
// }
|
||||
redisService.del(RedisCst.classKeyExpired.EXPIRED_ORDER + orderInfo.getId());
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue