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

    }
}
相关推荐
计算机徐师兄19 分钟前
Java基于SpringBoot的运动健康小程序【附源码、文档说明】
spring boot·小程序·运动健康·java运动健康小程序·运动健康小程序·java运动健康微信小程序·运动健康微信小程序
计算机学姐29 分钟前
基于SpringBoot的网吧管理系统
java·spring boot·后端·spring·tomcat·intellij-idea·mybatis
Boop_wu30 分钟前
[Java EE 进阶] SpringBoot 配置文件全解析:properties 与 yml 的使用(1)
java·spring boot·spring·java-ee
ywf12152 小时前
前端的dist包放到后端springboot项目下一起打包
前端·spring boot·后端
大阿明9 小时前
Spring Boot(快速上手)
java·spring boot·后端
哆啦A梦15889 小时前
Springboot整合MyBatis实现数据库操作
数据库·spring boot·mybatis
星轨zb11 小时前
通过实际demo掌握SpringSecurity+MP中的基本框架搭建
数据库·spring boot·spring security·mp
没有bug.的程序员13 小时前
Serverless 弹性扩容引发的全线熔断:Spring Boot 启动耗时从 1s 压缩至 0.3s 的物理级绞杀
java·spring boot·kubernetes·serverless·扩容·线上
luom010215 小时前
SpringBoot - Cookie & Session 用户登录及登录状态保持功能实现
java·spring boot·后端
希望永不加班16 小时前
SpringBoot 核心配置文件:application.yml 与 application.properties
java·spring boot·后端·spring