RabbitMqBindingConfig.java
3.35 KB
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
package com.topdraw.config;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.naming.ConfigurationException;
import java.util.List;
import java.util.Map;
//@Component
public class RabbitMqBindingConfig {
/**************************************************数据源选择*************************************************************/
/*@Autowired
private RabbitMqSourceConfig rabbitMqSourceConfig;
@Autowired
private RabbitMqCustomConfig rabbitMqCustomConfig;
@Resource(name = "managementRabbitAdmin")
private RabbitAdmin managementRabbitAdmin;
@Resource(name = "serviceRabbitAdmin")
private RabbitAdmin serviceRabbitAdmin;
@PostConstruct
public void initBinding() throws ConfigurationException {
List<Map<String, String>> list = rabbitMqCustomConfig.getList();
if (CollectionUtils.isNotEmpty(list)) {
for (Map<String, String> map : list) {
String exchange = map.get("exchange");
String exchangeType = map.get("exchange-type");
Exchange exchange_ = null;
switch (exchangeType) {
case ExchangeTypes.TOPIC:
exchange_ = ExchangeBuilder.topicExchange(exchange)
.durable(true).build();
break;
case ExchangeTypes.FANOUT:
exchange_ = ExchangeBuilder.fanoutExchange(exchange)
.durable(true).build();
break;
case ExchangeTypes.HEADERS:
exchange_ = ExchangeBuilder.headersExchange(exchange)
.durable(true).build();
break;
default:
exchange_ = ExchangeBuilder.directExchange(exchange)
.durable(true).build();
break;
}
String queue = map.get("queue");
Queue queue_ = new Queue(queue);
String routingKey = map.get("routing-key");
if (StringUtils.isBlank(routingKey)) {
routingKey = queue;
}
Binding binding_ = BindingBuilder.bind(queue_).to(exchange_).with(routingKey).and(null);
String active = map.get("active");
switch (active) {
case "management":
this.managementRabbitAdmin.declareExchange(exchange_);
this.managementRabbitAdmin.declareQueue(queue_);
this.managementRabbitAdmin.declareBinding(binding_);
break;
case "service":
this.serviceRabbitAdmin.declareExchange(exchange_);
this.serviceRabbitAdmin.declareQueue(queue_);
this.serviceRabbitAdmin.declareBinding(binding_);
break;
default:
break;
}
}
}
}*/
}