1
votes

I have one listener method annotated with @RabbitListener currently its configured to listen from 1 rabbit cluster. Now i have another rabbit cluster(cluster2) having the same queue/exchange/routing key defined and can possibly publish message.

Is there a way to use the same listener method to listen from both clusters? As a listener i dont care from which cluster the message came from and i would like to do the same processing for both.

    // Config

    @Bean
    @ConfigurationProperties("cluster1.rabbitmq")
    public RabbitProperties rabbitProps() {
        return new RabbitProperties();
    }

    // Listener code

    @RabbitListener(id = "My App", bindings = @QueueBinding(value = @Queue(value = "Some_Queue_Name"), 
            exchange = @Exchange(value = "Some_Exchange_Name"), 
            key = "Some_Routing_Key"))
    public void onMessage(final Message message) {
        // ....
    }

My application.yml has the below configs defined

cluster1.rabbitmq.host
cluster1.rabbitmq.username
cluster1.rabbitmq.password
1

1 Answers

1
votes

Create two listener container factories and add a second @RabbitListener to the method

@RabbitListener(id = "My App 1", 
            bindings = @QueueBinding(value = @Queue(value = "Some_Queue_Name"), 
            exchange = @Exchange(value = "Some_Exchange_Name"), 
            key = "Some_Routing_Key"))
@RabbitListener(id = "My App 2", containerFactory="secondContainerFactory"
            bindings = @QueueBinding(value = @Queue(value = "Some_Queue_Name"), 
            exchange = @Exchange(value = "Some_Exchange_Name"), 
            key = "Some_Routing_Key"))
public void onMessage(final Message message) {
    // ....
}

The first one will use the default rabbitListenerContainerFactory.