RabbitMQ 实现延迟队列

/**
 * @author zhangxiao
 * @qq 490433117
 * @create_date 2021/12/28 20:32
 */


import java.util.HashMap;
@Configuration
public class MyRabbitMQConfig {
    @Autowired(required = false)
    private ItsmQuestionnairesSendingConfigMapper configMapper;

    /**
     * 死信队列
     *
     * @return
     */
    @Bean
    public org.springframework.amqp.core.Queue orderDelayQueue() {
        Integer recycltime = 10;
        ItsmQuestionnairesSendingConfig config = configMapper.selectOne(null);
        if (config != null){
            recycltime = config.getDefaultRecycltime();
            recycltime = recycltime * 24 * 3600;
        }
     /*
            Queue(String name,  队列名字
            boolean durable,  是否持久化
            boolean exclusive,  是否排他
            boolean autoDelete, 是否自动删除
            Map<String, Object> arguments) 属性
         */
        HashMap<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        arguments.put("x-message-ttl", recycltime * 1000); // 消息过期时间 20秒

        return new org.springframework.amqp.core.Queue("order.delay.queue", true, false, false, arguments);
    }


    /**
     * 普通队列
     *
     * @return
     */
    @Bean
    public Queue orderReleaseQueue() {

        return new org.springframework.amqp.core.Queue("order.release.order.queue", true, false, false);
    }

    /**
     * TopicExchange
     *
     * @return
     */
    @Bean
    public Exchange orderEventExchange() {
        /*
         *   String name,
         *   boolean durable,
         *   boolean autoDelete,
         *   Map<String, Object> arguments
         * */

        return new TopicExchange("order-event-exchange", true, false);
    }

    @Bean
    public Binding orderCreateBinding() {
        /*
         * String destination, 目的地(队列名或者交换机名字)
         * DestinationType destinationType, 目的地类型(Queue、Exhcange)
         * String exchange,
         * String routingKey,
         * Map<String, Object> arguments
         * */
        return new Binding("order.delay.queue",
            Binding.DestinationType.QUEUE,
            "order-event-exchange",
            "order.create.order",  // 路由key一般为事件名
            null);
    }

    @Bean
    public Binding orderReleaseBinding() {

        return new Binding("order.release.order.queue",
            Binding.DestinationType.QUEUE,
            "order-event-exchange",
            "order.release.order",
            null);
    }

}
本作品采用《CC 协议》,转载必须注明作者和本文链接
zhaozhangxiao
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!