/** * Declares all the exchanges, queues and bindings in the enclosing application context, if any. It should be safe * (but unnecessary) to call this method more than once. */ @Override// NOSONAR complexity publicvoidinitialize() { if (this.applicationContext == null) { this.logger.debug("no ApplicationContext has been set, cannot auto-declare Exchanges, Queues, and Bindings"); return; } this.logger.debug("Initializing declarations"); Collection<Exchange> contextExchanges = newLinkedList<Exchange>(this.applicationContext.getBeansOfType(Exchange.class).values()); Collection<Queue> contextQueues = newLinkedList<Queue>(this.applicationContext.getBeansOfType(Queue.class).values()); Collection<Binding> contextBindings = newLinkedList<Binding>(this.applicationContext.getBeansOfType(Binding.class).values()); Collection<DeclarableCustomizer> customizers = this.applicationContext.getBeansOfType(DeclarableCustomizer.class).values(); processDeclarables(contextExchanges, contextQueues, contextBindings); final Collection<Exchange> exchanges = filterDeclarables(contextExchanges, customizers); final Collection<Queue> queues = filterDeclarables(contextQueues, customizers); final Collection<Binding> bindings = filterDeclarables(contextBindings, customizers); for (Exchange exchange : exchanges) { if ((!exchange.isDurable() || exchange.isAutoDelete()) && this.logger.isInfoEnabled()) { this.logger.info("Auto-declaring a non-durable or auto-delete Exchange (" + exchange.getName() + ") durable:" + exchange.isDurable() + ", auto-delete:" + exchange.isAutoDelete() + ". " + "It will be deleted by the broker if it shuts down, and can be redeclared by closing and " + "reopening the connection."); } }
for (Queue queue : queues) { if ((!queue.isDurable() || queue.isAutoDelete() || queue.isExclusive()) && this.logger.isInfoEnabled()) { this.logger.info("Auto-declaring a non-durable, auto-delete, or exclusive Queue (" + queue.getName() + ") durable:" + queue.isDurable() + ", auto-delete:" + queue.isAutoDelete() + ", exclusive:" + queue.isExclusive() + ". " + "It will be redeclared if the broker stops and is restarted while the connection factory is " + "alive, but all messages will be lost."); } }
/** * Is an exchange internal; i.e. can't be directly published to by a client, used for exchange-to-exchange binding only. * @return true if internal. * @since 1.6 */ booleanisInternal();
通过这样简单的配置就可以将消费者绑定到对应的队列上。同时在listener结点上,还可以设置的部分属性有: priority:优先级,设置该listener的消费者的优先级。 method:接收到消息的时候调用的方法,如果不设置,那么消费者类应该去实现MessageListener接口。 response-exchange、response-routing-key:如果消息不设置reply-to,但是设置了这两个属性,那么对于上面调用method方法返回的值,将会被发送到response-exchange设置的交换机。 原文注释: response-exchange: The name of the default response Exchange to send response messages to. This will be applied in case of a request message that does not carry a "replyTo" property. Note: This only applies to a listener method with a return value, for which each result object will be converted into a response message. response-routing-key: The routing key to send along with a response message.