我有一个拥有2个队列的FanoutExchange。每个队列都有一个Listener,当我为每个ListenerAdapter创建两个SimpleMessageListenerContainer时,只有第一个工作。
我将代码留在这里以获得更好的解释:
public static final String BROADCAST_PERSISTENCE_QUEUE = "persistence.rabbit.queue";
public static final String BROADCAST_WEBAPP_QUEUE = "webapp.rabbit.queue";
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
"localhost");
return connectionFactory;
}
@Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}
@Bean
public Queue persistenceQueue() {
return new Queue(BROADCAST_PERSISTENCE_QUEUE);
}
@Bean
public Queue webAppQueue() {
return new Queue(BROADCAST_WEBAPP_QUEUE);
}
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
return rabbitTemplate;
}
@Bean
public FanoutExchange trashRouteExchange() {
FanoutExchange exchange = new FanoutExchange("trashroute");
return exchange;
}
@Bean
public Binding trashRouteBinding() {
return BindingBuilder.bind(persistenceQueue()).to(trashRouteExchange());
}
@Bean
public Binding webAppBinding() {
return BindingBuilder.bind(webAppQueue()).to(trashRouteExchange());
}
@Bean
MessageListenerAdapter persistenceListenerAdapter(PersistenceListener persistenceListener) {
return new MessageListenerAdapter(persistenceListener, "receiveMessage");
}
@Bean
SimpleMessageListenerContainer persistenceListenerContainer(
ConnectionFactory connectionFactory,
@Qualifier("persistenceListenerAdapter") MessageListenerAdapter persistenceListenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueues(persistenceQueue(), webAppQueue());
container.setMessageListener(persistenceListenerAdapter);
return container;
}
@Bean
MessageListenerAdapter webAppListenerAdapter(WebAppListener webAppListener) {
return new MessageListenerAdapter(webAppListener, "receiveMessage");
}
@Bean
SimpleMessageListenerContainer webAppListenerContainer(
ConnectionFactory connectionFactory,
@Qualifier("webAppListenerAdapter") MessageListenerAdapter webAppListenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueues(persistenceQueue(), webAppQueue());
container.setMessageListener(webAppListenerAdapter);
return container;
}
@Bean
PersistenceListener persistenceListener() {
return new PersistenceListener();
}
@Bean
WebAppListener webAppListener() {
return new WebAppListener();
}
奇怪的情况是,如果我首先放入persistenceListenerContainer()方法,这是正确工作的监听器,但是,如果我更改我的代码并放置webAppListenerContainer()方法,则PersistenceListener不起作用和WebAppListener开始工作了。
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
正如你所说的那样MessageListenerAdapter
你首先放置的是自动装配的。这是因为自动装配是byType。如果您使用自动装配byName
,则可以修复此问题。
@Bean(name="persistenceListenerAdapter")
MessageListenerAdapter persistenceListenerAdapter(PersistenceListener persistenceListener) {
return new MessageListenerAdapter(persistenceListener, "receiveMessage");
}
@Bean(name="webListenerAdapter")
MessageListenerAdapter webAppListenerAdapter(WebAppListener webAppListener) {
return new MessageListenerAdapter(webAppListener, "receiveMessage");
}