简体   繁体   中英

Spring AMQP with 2 Listeners, only one listener works

I have one FanoutExchange that has 2 Queue's. Each queue has one Listener, when I create the two SimpleMessageListenerContainer for each ListenerAdapter, only the first works.

I leave my code here for better explanation:

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();
}

The strange situation is that If I put first the persistenceListenerContainer() method, this is the Listener that works correctly, however, if I change my code and put webAppListenerContainer() method, the PersistenceListener doesn't work and WebAppListener starts to work.

Anyone can help me with this issue?

As you told that which ever MessageListenerAdapter you put first that is being autowired. It is because of autowiring is byType. This issuue can be fixed if you use autowiring 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");
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM