springboot rabbitmq 延时队列消息确认收货订单已完成

供应商后台-点击发货-默认3天自动收货确认,更新订单状态已完成。

1 pom.xml 引入依赖:

复制代码
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>

2 运营后台-订单-发货按钮:生产者发布延时消息

复制代码
// 5. 发送自动确认收货消息
        // 计算延迟时间
        String autoDeliveryDays = configService.getConfigVal(SettingsEnum.AUTO_DELIVERY_DAYS);
        if (StrUtil.isNotBlank(autoDeliveryDays)) {
            // long delayTime = Long.parseLong(autoDeliveryDays) * 24 * 60 * 60 * 1000;
            BigDecimal delayTime = new BigDecimal(autoDeliveryDays).multiply(new BigDecimal(24 * 60 * 60 * 1000));
            rabbitTemplate.convertAndSend(
                    RabbitMQConfig.DELAY_EXCHANGE,
                    RabbitMQConfig.ORDER_CONFIRM_RECEIPT_ROUTING_KEY,
                    order.getOrderId(),
                    message -> {
                        message.getMessageProperties().setHeader("x-delay", delayTime.longValue());
                        return message;
                    }
            );
        }

3 RabbitMQ消息队列,路由键,交换机配置

复制代码
package com.tigshop.common.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig { 
 // 路由键
   public static final String ORDER_CONFIRM_RECEIPT_ROUTING_KEY = "order.confirm.receipt.routing.key";
   // 队列名称
   public static final String ORDER_CONFIRM_RECEIPT_QUEUE = "orderConfirmReceiptQueue";
   
   
   /**
     * 直连交换机(普通交换机)
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(DIRECT_EXCHANGE);
    }

    /**
     * 延迟交换机
     */
    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DELAY_EXCHANGE, "x-delayed-message", true, false, args);
    }
	
	
	@Bean
    public Queue orderConfirmReceiptQueue() {
        return QueueBuilder.durable(ORDER_CONFIRM_RECEIPT_QUEUE).build();
    }
	
     @Bean
    public Binding orderConfirmReceiptBinding() {
        return BindingBuilder
                .bind(orderConfirmReceiptQueue())
                .to(delayExchange())
                .with(ORDER_CONFIRM_RECEIPT_ROUTING_KEY)
                .noargs();
    }
	
}

4 消费者实现监听器消费

复制代码
@RequiredArgsConstructor
@Service
@Slf4j
public class RabbitMqConsumer{

    @RabbitListener(queues = RabbitMQConfig.ORDER_CONFIRM_RECEIPT_QUEUE)
    public void receiveOrderConfirmReceiptMessage(Integer orderId) {
        log.info("收到订单自动确认收货消息:{}", orderId);

        // 判断是否已经收货
        Long receivedCount = orderService.lambdaQuery()
                .eq(Order::getOrderId, orderId)
                .eq(Order::getShippingStatus, ShippingStatusEnum.SHIPPED.getCode())
                .count();
        if (receivedCount == 1) {
            return;
        }

        // 判断订单是否售后中
        Long aftersalesCount = aftersalesService.lambdaQuery()
                .eq(Aftersales::getOrderId, orderId)
                .eq(Aftersales::getStatus, AftersalesStatusEnum.IN_REVIEW.getCode())
                .count();
        if (aftersalesCount == 1) {
            return;
        }

        try {
            orderService.confirmReceipt(orderId);
        } catch (GlobalException e) {
            log.error("【异常】收到订单自动确认收货消息 RabbitMQ:{}", e.getMessage());
        }

    }
}
相关推荐
Dolphin_Home2 分钟前
笔记:SpringBoot静态类调用Bean的2种方案(小白友好版)
java·spring boot·笔记
刘一说1 小时前
Nacos 权限控制详解:从开源版 v2.2+ 到企业级安全实践
spring boot·安全·spring cloud·微服务·nacos·架构·开源
Q_Q5110082852 小时前
python+django/flask+vue的大健康养老公寓管理系统
spring boot·python·django·flask·node.js
czlczl200209252 小时前
通过哪些条件确定用哪个消息转换器
spring boot
毕设源码-朱学姐3 小时前
【开题答辩全过程】以 个人健康管理系统为例,包含答辩的问题和答案
java·spring boot
qq_12498707533 小时前
基于微信小程序的线下点餐系统的设计与实现(源码+论文+部署+安装)
spring boot·微信小程序·小程序·毕业设计
IT_Octopus4 小时前
Java GZip 压缩实践 +实践思考 +进一步压榨性能和存储方案思考:Protobuf+ GZip
java·spring boot
毕设源码-郭学长4 小时前
【开题答辩全过程】以 高校教材大管家系统为例,包含答辩的问题和答案
java·spring boot
qq_12498707535 小时前
基于SpringBoot+vue的小黄蜂外卖平台(源码+论文+部署+安装)
java·开发语言·vue.js·spring boot·后端·mysql·毕业设计
i02085 小时前
Java 17 + Spring Boot 3.2.5 使用 Redis 实现“生产者–消费者”任务队列
java·spring boot·redis