打印增加等待锁,防止重复打印

This commit is contained in:
张松
2025-03-18 16:56:51 +08:00
parent d422699e41
commit 06c3fd012d
3 changed files with 168 additions and 28 deletions

View File

@@ -6,11 +6,13 @@ import com.alibaba.fastjson2.JSONObject;
import com.czg.account.entity.PrintMachine;
import com.czg.account.service.PrintMachineService;
import com.czg.config.RabbitConstants;
import com.czg.config.RedisCst;
import com.czg.order.entity.MqLog;
import com.czg.order.entity.OrderInfo;
import com.czg.order.service.MqLogService;
import com.czg.order.service.OrderInfoService;
import com.czg.service.order.print.PrinterHandler;
import com.czg.service.order.utils.FunUtil;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
@@ -33,25 +35,27 @@ import java.util.function.Consumer;
public class PrintMqListener {
@Resource
private MqLogService mqLogService;
@Resource
private FunUtil funUtil;
@Lazy
@Resource
private PrinterHandler printerHandler;
private <T> void invokeFun(String type, String plat, T data, Consumer<T> consumer) {
long startTime = DateUtil.date().getTime();
log.info("接收到{}打印消息:{}", type, data);
MqLog mqLog = new MqLog().setQueue(RabbitConstants.Queue.ORDER_MACHINE_PRINT_QUEUE).setMsg(data.toString())
.setType(type).setPlat(plat).setCreateTime(DateUtil.date().toLocalDateTime());
try {
consumer.accept(data);
} catch (Exception e) {
log.error("订单打印失败", e);
mqLog.setErrInfo(JSONObject.toJSONString(e));
mqLog.setDuration(DateUtil.date().getTime() - startTime);
mqLog.setFailTime(DateUtil.date().toLocalDateTime());
mqLogService.save(mqLog);
private <T> void invokeFun(String type, String plat, T data, Consumer<T> consumer) {
long startTime = DateUtil.date().getTime();
log.info("接收到{}打印消息:{}", type, data);
MqLog mqLog = new MqLog().setQueue(RabbitConstants.Queue.ORDER_MACHINE_PRINT_QUEUE).setMsg(data.toString())
.setType(type).setPlat(plat).setCreateTime(DateUtil.date().toLocalDateTime());
try {
consumer.accept(data);
} catch (Exception e) {
log.error("订单打印失败", e);
mqLog.setErrInfo(JSONObject.toJSONString(e));
mqLog.setDuration(DateUtil.date().getTime() - startTime);
mqLog.setFailTime(DateUtil.date().toLocalDateTime());
mqLogService.save(mqLog);
}
}
}
@RabbitListener(queues = {"${spring.profiles.active}-" + RabbitConstants.Queue.ORDER_MACHINE_PRINT_QUEUE})
public void orderPrint(String req) {
@@ -62,7 +66,10 @@ public class PrintMqListener {
throw new RuntimeException("订单打印失败未传递orderId");
}
Boolean printOrder = jsonObject.getBoolean("printOrder");
printerHandler.handler(orderId, printOrder != null && !printOrder ? PrinterHandler.PrintTypeEnum.ONE : PrinterHandler.PrintTypeEnum.ONE_AND_ORDER);
funUtil.runFunAndCheckKey(() -> {
printerHandler.handler(orderId, printOrder != null && !printOrder ? PrinterHandler.PrintTypeEnum.ONE : PrinterHandler.PrintTypeEnum.ONE_AND_ORDER);
return null;
}, RedisCst.getLockKey("orderPrint", orderId));
});
}