RabbitMQ延迟队列修改订单状态

//业务描述: 需要根据订单的超时时间更改当前订单的状态

第一步:配置MQconfig

public class RabbitConfig {
    
    // 死信交换器
    @Bean
    TopicExchange deadExchange() {
        return new TopicExchange("parts.order.endtime.dead.exchange");
    }

    // 死信队列
    @Bean
    Queue deadQueue() {
        // 当前队列如果超时  会转发到 死信交换器中
        return QueueBuilder.durable("parts.order.endtime.dead.queue")
                .withArgument("x-dead-letter-exchange", "parts.order.endtime.forwarding.exchange") // DLX,dead letter发送到的exchange
                .withArgument("x-dead-letter-routing-key", "order.endtime") // dead letter携带的routing key
                .build();
    }

    //绑定死信队列与死信交换器
    @Bean
    public Binding  preBinding() {
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with("parts.pre_order_key");
    }

    // 转发队列
    @Bean
    Queue delayProcessQueue() {
        return QueueBuilder.durable("parts.order.endtime.forwarding.queue")
                .build();
    }


    // 转发交换器
    @Bean
    TopicExchange delayExchange() {
        return new TopicExchange("parts.order.endtime.forwarding.exchange");
    }

// MQ初始化配置

    @Bean
    public Jackson2JsonMessageConverter converter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate(@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        ObjectMapper om = new ObjectMapper();
        om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
       /* om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);*/
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter(om));
        rabbitTemplate.setConnectionFactory(connectionFactory);
        return rabbitTemplate;
    }

}

首先创建两个队列 两个交换器,两两绑定, 死信队列会设置一个header,超时时间到了之后 会转发到交换器2中,然后再代码块里监听交换器2就能达到延迟的效果

流程 1.代码块中发送消息并设置死亡时间->

2.死信队列接收消息,死亡时间超过之后,会根据配置,转发到交换器2中(转发交换器) ->

3.代码块监听转发队列,进行业务操作

代码块

        // 失效时间
        long times = failuerDay * ONE_DAY_MILLISECONDS;
        amqpTemplate.convertAndSend( "parts.order.endtime.dead.exchange","parts.pre_order_key", orderString, message -> {
            message.getMessageProperties().setExpiration(times + "");
            return message;
        });
        
        
        
        
 =======================================================
        
          // 消费转发队列(队列与交换器绑定可以设置到config中)
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = "parts.order.endtime.forwarding.exchange", type = "topic"),
            value = @Queue(value = "parts.order.endtime.forwarding.queue", durable = "true"),
            key = "order.endtime"
    ))
    public void orderDelayQueue(String orderString, Message message, Channel channel) {
        log.info("接收死信队列消息");
        try {
            // 查询该订单是否支付 如果未支付将订单状态改为取消
            // 业务逻辑
        } catch (IOException e) {
            log.error("死信队列订单信息转换失败!");
            e.printStackTrace();
        }
        log.info("死信队列订单状态更新成功!");
    }

你可能感兴趣的:(记录)