springBoot整合rabbitMq 延遲隊列

1.實現流程 

 

2.實現代碼 

package cn.ucmed.ydyc.yzzx.mq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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


/**
 * 延時隊列
 */
@Configuration
public class DelayQueueConfig {
    /**
     * 接收隊列名
     */
    public static final String receive = "yzzx.delay.receive";

    /**
     * 延遲發送隊列名
     */
    public static final String send = "yzzx.delay.send";

    /**
     * 創建延遲接收交換機
     * @return
     */
    @Bean
    public DirectExchange receiveExchange(){
        return new DirectExchange(receive);
    }

    /**
     * 創建延遲接收隊列
     * @return
     */
    @Bean
    public Queue receiveQueue(){
        return new Queue(receive);
    }

    /**
     * 延遲接收交換機綁定接收隊列
     * @return
     */
    @Bean
    public Binding receiveBinding(Queue receiveQueue,DirectExchange receiveExchange){
        return BindingBuilder.bind(receiveQueue).to(receiveExchange).with(receive);
    }

    /**
     * 創建延遲發送交換機
     * @return
     */
    @Bean
    public DirectExchange delayExchange(){
        return new DirectExchange(send);
    }

    /**
     * 創建延遲發送隊列,並綁定接收的exchange和routingKey
     * @return
     */
    @Bean
    public Queue delayQueue(){
        Map<String,Object> map = new HashMap<>(16);
        map.put("x-dead-letter-exchange", receive);
        map.put("x-dead-letter-routing-key", receive);
        return new Queue(send,true,false,false,map);
    }

    /**
     * 給延遲發送隊列綁定交換機
     * @return
     */
    @Bean
    public Binding delayBinding(Queue delayQueue, DirectExchange delayExchange){
        return BindingBuilder.bind(delayQueue).to(delayExchange).with(send);
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章