springboot整合rabbitmq消息队列之延迟队列

延迟队列是ttl和dlx(死信队列)结合实现的
延迟队列实现


    /*
        延迟队列
     */


    public static final String NORMAL_DELAY_QUEUE = "normal_delay_queue";

    public static final String NORMAL_DELAY_Exchange = "normal_delay_exchange";

    //ttl
    private static final int NORMAL_DELAY_EXPIRATION = 10000;

    //设置队列长度限制

    private static final int NORMAL_DELAY_LENGTH = 10;


    public static final String DELAY_DLX_QUEUE = "delay_dlx_queue";

    public static final String DELAY_DLX_Exchange = "delay_dlx_exchange";


    //绑定死信队列

    @Bean
    public Queue normalDelayQueue(){
        return QueueBuilder.durable(NORMAL_DELAY_QUEUE)
                .withArgument("x-dead-letter-exchange", DELAY_DLX_Exchange)
                .withArgument("x-dead-letter-routing-key", "dlx.hehe")
                .withArgument("x-message-ttl", NORMAL_DELAY_EXPIRATION)
                .withArgument("x-max-length",NORMAL_DELAY_LENGTH)
                .build();
    }

    @Bean
    public TopicExchange normalDelayExchange(){
        return new TopicExchange(NORMAL_DELAY_Exchange);
    }

    @Bean
    public Binding normalDelayBinding(){
        return BindingBuilder.bind(normalDelayQueue()).to(normalDelayExchange()).with("test.dlx.#");
    }

    //声明死信队列,和死信交换机




    @Bean
    public Queue delayQueue(){
        return new Queue(DELAY_DLX_QUEUE);
    }

    @Bean
    public TopicExchange delayExchange(){
        return new TopicExchange(DELAY_DLX_Exchange);
    }

    @Bean
    public Binding delayBinding(){
        return BindingBuilder.bind(delayQueue()).to(delayExchange()).with("dlx.#");
    }

消费端代码

@Component
public class DelayListener {

   //死信队列,监听死信的队列

    @RabbitListener(queues = "delay_dlx_queue")
    public void myAckListener(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws Exception {


        try {
            System.out.println(message);
            System.out.println("处理业务逻辑...");
            System.out.println("查询订单状态...");
            System.out.println("判断状态是否为支付成功...");
            System.out.println("取消订单,回滚库存...");

            /**
             * 无异常就确认消息
             * basicAck(long deliveryTag, boolean multiple)
             * deliveryTag:取出来当前消息在队列中的的索引;
             * multiple:为true的话就是批量确认,如果当前deliveryTag为5,那么就会确认
             * deliveryTag为5及其以下的消息;一般设置为false
             */
//            int p =1/0;
            //手动签收
            channel.basicAck(tag, true);
        }catch (Exception e){
            /**
             * 有异常就绝收消息
             * basicNack(long deliveryTag, boolean multiple, boolean requeue)
             * requeue:true为将消息重返当前消息队列,还可以重新发送给消费者;
             *         false:将消息丢弃
             */
            System.out.println();
            //出现异常拒绝接受
            channel.basicNack(tag,true,false);
        }

    }





}

测试延迟队列

@Test
    void Delay() throws InterruptedException {


        rabbitTemplate.convertAndSend(RabbitMQConfig.NORMAL_DELAY_Exchange,"test.dlx.haha","订单信息:id=1234");

        

    }

就实现订单10s后自动处理,未支付自动回滚

你可能感兴趣的:(springboot,rabbitmq)