RabbitMQ 死信队列 延时

 

 

package com.hs.services.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * rabbitmq 配置
 * @author ZHANGYUKUN
 *
 */
@Configuration
public class RabbitMQConfig {
    
    //钱包交换机
    public static final String walletExchange  = "walletExchange";
    
    //红包交换机
    public static final String redpacketExchange  = "redpacketExchange";
    
    @Bean
    public DirectExchange walletExchange() {
        return new DirectExchange(RabbitMQConfig.walletExchange, true, false);
    }
    @Bean
    public DirectExchange redpacketExchange() {
        return new DirectExchange(RabbitMQConfig.redpacketExchange, true, false);
    }
    
    
    
    
    
    
    

    /**
     * 钱包
     */
    public static final String walletAmountChange = "walletAmountChange";
    public static final String walletAmountChangeDL = "walletAmountChangeDL";
    
    /**
     * 红包
     */
    public static final String redpacketChange = "redpacketChange";
    public static final String redpacketChangeDL = "redpacketChangeDL";

    
    /**
     * 钱包余额 相关
     * @return
     */
    @Bean
    public Queue walletAmountChange() {
        return new Queue( RabbitMQConfig.walletAmountChange );
    }
    @Bean
    public Queue walletAmountChangeDL() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", RabbitMQConfig.walletExchange);
        arguments.put("x-dead-letter-routing-key", RabbitMQConfig.walletAmountChangeDL);
        return new Queue(RabbitMQConfig.walletAmountChangeDL, true, false, false, arguments);
    }
    @Bean
    public Binding  walletAmountChangeBind(Queue walletAmountChange ,DirectExchange walletExchange ) {
        return BindingBuilder.bind( walletAmountChange ).to( walletExchange ).with( RabbitMQConfig.walletAmountChange );
    }
    @Bean
    public Binding  walletAmountChangeBindDL(Queue walletAmountChangeDL ,DirectExchange walletExchange ) {
        return BindingBuilder.bind( walletAmountChangeDL ).to( walletExchange ).with( RabbitMQConfig.walletAmountChangeDL );
    }
    
    
    
    /**
     * 钱包余额 相关
     * @return
     */
    @Bean
    public Queue redpacketChange() {
        return new Queue( RabbitMQConfig.redpacketChange );
    }
    @Bean
    public Queue redpacketChangeDL() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", RabbitMQConfig.redpacketExchange);
        arguments.put("x-dead-letter-routing-key", RabbitMQConfig.redpacketChangeDL);
        return new Queue(RabbitMQConfig.redpacketChangeDL, true, false, false, arguments);
    }
    @Bean
    public Binding  redpacketChangeBind(Queue redpacketChange ,DirectExchange redpacketExchange ) {
        return BindingBuilder.bind( redpacketChange ).to( redpacketExchange ).with( RabbitMQConfig.redpacketChange );
    }
    @Bean
    public Binding redpacketChangeBindDL(Queue redpacketChangeDL ,DirectExchange redpacketExchange ) {
        return BindingBuilder.bind( redpacketChangeDL ).to( redpacketExchange ).with( RabbitMQConfig.redpacketChangeDL );
    }
 

}

 

 

上述 ,配了 死信队列,如果 如果 发送到 死信里面的消息 如果 超时,就会被转交给 对应的  正常队列。 死信 的这个特点可以做延时消息。

posted on 2019-03-27 14:37  zhangyukun  阅读(239)  评论(0)    收藏  举报

导航