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 );
    }
 

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.

 

 

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