Springboot升级后 RabbitMQ自动声明队列问题

RabbitAdmin的initialize()方法
spring-rabbit-2.0.3 版本采用集合类的方式

@SuppressWarnings("rawtypes")
Collection<Collection> collections = this.declareCollections
	? this.applicationContext.getBeansOfType(Collection.class, false, false).values()
	: Collections.emptyList();
for (Collection<?> collection : collections) {
	if (collection.size() > 0 && collection.iterator().next() instanceof Declarable) {
		for (Object declarable : collection) {
			if (declarable instanceof Exchange) {
				contextExchanges.add((Exchange) declarable);
			}
			else if (declarable instanceof Queue) {
				contextQueues.add((Queue) declarable);
			}
			else if (declarable instanceof Binding) {
				contextBindings.add((Binding) declarable);
			}
		}
	}
}

所以采用以下方法,可以自动声明多个队列

public List queues(){
	return new List();
}

但是具体之后的版本就取消以上的声明方式。

在Spring-rabbit-2.2.18版本以上
假如要声明多个队列、交换器、绑定则需要采用Declarables类进行封装

private void processDeclarables(Collection<Exchange> contextExchanges, Collection<Queue> contextQueues,
			Collection<Binding> contextBindings) {

		Collection<Declarables> declarables = this.applicationContext.getBeansOfType(Declarables.class, false, true)
				.values();
		declarables.forEach(d -> {
			d.getDeclarables().forEach(declarable -> {
				if (declarable instanceof Exchange) {
					contextExchanges.add((Exchange) declarable);
				}
				else if (declarable instanceof Queue) {
					contextQueues.add((Queue) declarable);
				}
				else if (declarable instanceof Binding) {
					contextBindings.add((Binding) declarable);
				}
			});
		});
	}

你可能感兴趣的:(消息队列,rabbitmq,spring,java)