liusheng
2024-05-10 bb60b5747d5f4b85655a541d4990ec7464497b1b
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
package com.smartor.config;
 
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import java.util.HashMap;
import java.util.Map;
 
@Configuration
public class RabbitMqConfig {
//    public static final String EXCHANGE_NAME = "phone_topic_exchange2";
//    public static final String QUEUE_NAME = "phone_queue";
    //
//    /**
//     * topic交换机,并持久化
//     */
//    @Bean(EXCHANGE_NAME)
//    public Exchange phoneExchange() {
//        Map<String, Object> arguments = new HashMap<>();
//        //指定通信方式为topic
//        arguments.put("x-delayed-type", "topic");
//        //使用CustomExchange类创建,类型要指定为“x-delayed-message”类型
//        Exchange exchange = new CustomExchange(EXCHANGE_NAME, "x-delayed-message", true, false, arguments);
//        return exchange;
//
////
////        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
//    }
//
//
//    /**
//     * 队列
//     */
//    @Bean(QUEUE_NAME)
//    public Queue phoneQueue() {
//        return QueueBuilder.durable(QUEUE_NAME).build();
//    }
//
//    /**
//     * 交换机与队列的绑定
//     */
//    @Bean
//    public Binding bindQueueExchange(@Qualifier(QUEUE_NAME) Queue phoneQueue, @Qualifier(EXCHANGE_NAME) Exchange phoneExchange) {
//        return BindingBuilder.bind(phoneQueue).to(phoneExchange).with(" phone.#").noargs();
//    }
//定义延迟队列
    public static final String phone_queue = "phone_queue";
    //定义延迟交换机
    public static final String phone_exchange = "phone_exchange";
    //定义路由键
    public static final String routing_key = "phone.123";
 
 
    /**
     * 定义延迟队列
     *
     * @return
     */
    @Bean
    public Queue delayQueue() {
        return new Queue(phone_queue, true);
    }
 
    /**
     * 延时队列交换机
     * 交换机类型:CustomExchange
     *
     * @return
     */
    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "topic");
        return new CustomExchange(phone_exchange, "x-delayed-message", true, false, args);
    }
 
    /**
     * 为延迟队列绑定交换机
     *
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding delayBinding(Queue queue, CustomExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(routing_key).noargs();
    }
 
}