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());
        }

    }
}
相关推荐
默 语18 小时前
基于 Spring Boot 3 + LangChain4j 快速构建企业级 AI 应用实战
人工智能·spring boot·后端
Dontla18 小时前
aio-pika介绍(基于asyncio的Python异步消息队列客户端,用于操作RabbitMQ,并实现对AMQP协议支持)
python·rabbitmq·ruby
薪火铺子19 小时前
SpringBoot WebServer启动与监听器原理深度解析
spring boot·后端·tomcat
KmSH8umpK19 小时前
SpringBoot 分布式锁实战:从单机锁到Redis分布式锁全覆盖,解决超卖、重复下单、幂等并发问题
spring boot·redis·分布式
jay神19 小时前
基于团队模式的C程序设计课程辅助教学管理系统
java·spring boot·vue·web开发·管理系统
长河21 小时前
基于 Jib 实现无 Dockerfile 的 Spring Boot 应用容器化
java·spring boot·后端
Arya_aa21 小时前
一:病虫害 AI 识别系统项目初期准备与Docker初识,VM虚拟机
spring boot
敖正炀21 小时前
Spring MVC 启动全景:DispatcherServlet 与父子容器
spring boot
绿草在线1 天前
基于SpringBoot4+Mybatis+Thymeleaf的用户管理系统开发实战
java·spring boot·thymeleaf