RabbitMQ 實現延遲佇列

zhaozhangxiao發表於2021-12-31
/**
 * @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 協議》,轉載必須註明作者和本文連結

相關文章