@Autowired
RabbitTemplate rabbitTemplate;
@PostConstruct
public void initExchangeQueue() {
rabbitTemplate.execute(channel -> {
List<ExchangeQueue> exchangeQueues = Constant.exchangeQueues;
for (ExchangeQueue exchangeQueue : exchangeQueues) {
Exchange exchange = exchangeQueue.getExchange();
channel.exchangeDeclare(exchange.getName(), exchange.getType(), exchange.isDurable(), exchange.isAutoDelete(), exchange.getArguments());
Queue queue = exchangeQueue.getQueue();
channel.queueDeclare(queue.getName(), queue.isDurable(), queue.isExclusive(), queue.isAutoDelete(), null);
String routingKey = exchangeQueue.getRoutingKey();
if (routingKey != null) {
channel.queueBind(queue.getName(), exchange.getName(), routingKey);
}
}
return null;
});
}
存在问题
如果能够把交换机,队列,路由键的信息配置到配置中心,就可以动态创建相应的交换机,消息队列了,但是交换机,队列涉及到的参数有点多,配置起来有点麻烦
比如和交换机有关的参数:
String exchange, String type, boolean durable, boolean autoDelete,Map<String, Object> arguments
和队列有关的参数
String queue, boolean durable, boolean exclusive, boolean autoDelete,Map<String, Object> arguments