|
|
1
|
+package com.sunyo.wlpt.message.bus.service.rabbit;
|
|
|
2
|
+
|
|
|
3
|
+import com.sunyo.wlpt.message.bus.service.domain.BusExchange;
|
|
|
4
|
+import com.sunyo.wlpt.message.bus.service.domain.BusQueue;
|
|
|
5
|
+import com.sunyo.wlpt.message.bus.service.domain.UserMessageBinding;
|
|
|
6
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
7
|
+import org.springframework.amqp.core.*;
|
|
|
8
|
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
9
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
10
|
+import org.springframework.cloud.context.config.annotation.RefreshScope;
|
|
|
11
|
+import org.springframework.stereotype.Component;
|
|
|
12
|
+
|
|
|
13
|
+import javax.annotation.Resource;
|
|
|
14
|
+
|
|
|
15
|
+/**
|
|
|
16
|
+ * @author 子诚
|
|
|
17
|
+ * Description:
|
|
|
18
|
+ * 时间:2020/7/16 16:32
|
|
|
19
|
+ */
|
|
|
20
|
+@Slf4j
|
|
|
21
|
+@RefreshScope
|
|
|
22
|
+@Component
|
|
|
23
|
+public class DirectRabbitUtils {
|
|
|
24
|
+ @Resource
|
|
|
25
|
+ private AmqpAdmin amqpAdmin;
|
|
|
26
|
+
|
|
|
27
|
+ @Resource
|
|
|
28
|
+ private RabbitTemplate rabbitTemplate;
|
|
|
29
|
+
|
|
|
30
|
+ @Value("${spring.rabbitmq.virtual-host}")
|
|
|
31
|
+ private String v_host;
|
|
|
32
|
+
|
|
|
33
|
+ /**
|
|
|
34
|
+ * 创建交换机(交换机名称,是否持久化,是否删除)
|
|
|
35
|
+ *
|
|
|
36
|
+ * @param busExchange {@link BusExchange}
|
|
|
37
|
+ */
|
|
|
38
|
+ public void createExchange(BusExchange busExchange) {
|
|
|
39
|
+
|
|
|
40
|
+ // 类型-直连路由
|
|
|
41
|
+ String type_direct = "direct";
|
|
|
42
|
+ // 类型-动态路由
|
|
|
43
|
+ String type_topic = "topic";
|
|
|
44
|
+ // 类型-广播
|
|
|
45
|
+ String type_fanout = "fanout";
|
|
|
46
|
+ // 类型-头部
|
|
|
47
|
+ String type_headers = "headers";
|
|
|
48
|
+
|
|
|
49
|
+ // 创建交换机,直连接类型
|
|
|
50
|
+ if (type_direct.equals(busExchange.getExchangeType())) {
|
|
|
51
|
+ amqpAdmin.declareExchange(
|
|
|
52
|
+ new DirectExchange(busExchange.getExchangeName(), busExchange.getDurability(), busExchange.getAutoDelete())
|
|
|
53
|
+ );
|
|
|
54
|
+ log.info("创建了交换机:{};类型:{};", busExchange.getExchangeType(), type_direct);
|
|
|
55
|
+ }
|
|
|
56
|
+ // 创建交换机,扇形交换机
|
|
|
57
|
+ if (type_topic.equals(busExchange.getExchangeType())) {
|
|
|
58
|
+ amqpAdmin.declareExchange(
|
|
|
59
|
+ new TopicExchange(busExchange.getExchangeName(), busExchange.getDurability(), busExchange.getAutoDelete())
|
|
|
60
|
+ );
|
|
|
61
|
+ log.info("创建了交换机:{};类型:{};", busExchange.getExchangeType(), type_topic);
|
|
|
62
|
+ }
|
|
|
63
|
+ // 创建交换机,广播(主题)交换机
|
|
|
64
|
+ if (type_fanout.equals(busExchange.getExchangeType())) {
|
|
|
65
|
+ amqpAdmin.declareExchange(
|
|
|
66
|
+ new FanoutExchange(busExchange.getExchangeName(), busExchange.getDurability(), busExchange.getAutoDelete())
|
|
|
67
|
+ );
|
|
|
68
|
+ log.info("创建了交换机:{};类型:{};", busExchange.getExchangeType(), type_fanout);
|
|
|
69
|
+ }
|
|
|
70
|
+ // 创建交换机,首部交换机
|
|
|
71
|
+ if (type_headers.equals(busExchange.getExchangeType())) {
|
|
|
72
|
+ amqpAdmin.declareExchange(
|
|
|
73
|
+ new HeadersExchange(busExchange.getExchangeName(), busExchange.getDurability(), busExchange.getAutoDelete())
|
|
|
74
|
+ );
|
|
|
75
|
+ log.info("创建了交换机:{};类型:{};", busExchange.getExchangeType(), type_headers);
|
|
|
76
|
+ }
|
|
|
77
|
+ }
|
|
|
78
|
+
|
|
|
79
|
+ /**
|
|
|
80
|
+ * 根据交换机名称,删除虚拟机
|
|
|
81
|
+ *
|
|
|
82
|
+ * @param exchangeName 交换机名称
|
|
|
83
|
+ */
|
|
|
84
|
+ public void deleteExchange(String exchangeName) {
|
|
|
85
|
+ boolean flag = amqpAdmin.deleteExchange(exchangeName);
|
|
|
86
|
+ }
|
|
|
87
|
+
|
|
|
88
|
+ /**
|
|
|
89
|
+ * 创建队列
|
|
|
90
|
+ *
|
|
|
91
|
+ * @param busQueue {@link BusQueue}
|
|
|
92
|
+ */
|
|
|
93
|
+ public void createQueue(BusQueue busQueue) {
|
|
|
94
|
+ amqpAdmin.declareQueue(
|
|
|
95
|
+ new Queue(busQueue.getQueueName(), busQueue.getDurability(), false, busQueue.getAutoDelete())
|
|
|
96
|
+ );
|
|
|
97
|
+ }
|
|
|
98
|
+
|
|
|
99
|
+ /**
|
|
|
100
|
+ * 删除队列,根据队列名称
|
|
|
101
|
+ *
|
|
|
102
|
+ * @param queueName 队列名称
|
|
|
103
|
+ */
|
|
|
104
|
+ public void deleteQueue(String queueName) {
|
|
|
105
|
+ boolean flag = amqpAdmin.deleteQueue(queueName);
|
|
|
106
|
+ }
|
|
|
107
|
+
|
|
|
108
|
+ public void createBing(UserMessageBinding userMessageBinding) {
|
|
|
109
|
+ amqpAdmin.declareBinding(
|
|
|
110
|
+ new Binding(userMessageBinding.getQueueName(), Binding.DestinationType.QUEUE, userMessageBinding.getExchangeName(), userMessageBinding.getRoutingKeyName(), null)
|
|
|
111
|
+ );
|
|
|
112
|
+ }
|
|
|
113
|
+} |