Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup multiple topics in a RabbitMQ Java config class using Spring Framework?

I'm trying to create a RabbitMQ configuration class using Spring Framework. The documentation does not say anything on how to setup multiple topics in a TopicExchange. How do I do that? So far, I have this Java code but I'm not clear on how to setup multiple topics in the binding method below since it only returns one binding. Would I not need multiple bindings if I need multiple topics?

@Configuration
@EnableRabbit
public class MessageReceiverConfiguration {

    final static String queueName = "identity";
    final static String topic1 = "NewUserSignedUp";
    final static String topic2 = "AccountCreated";

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Bean
    Queue queue() {
        return new Queue(queueName, false);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("DomainEvents");
    }   

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        // How to setup multiple topics?
        return BindingBuilder.bind(queue).to(exchange).with(topic1);
    }

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {

        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();

        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        container.setAcknowledgeMode(AcknowledgeMode.AUTO);

        return container;
    }

    @Bean
    MessageReceiver receiver() {
        return new MessageReceiver();
    }

    @Bean
    MessageListenerAdapter listenerAdapter(MessageReceiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }    

}
like image 575
Thomas Jaeger Avatar asked Oct 12 '15 17:10

Thomas Jaeger


2 Answers

You can define multiple binding by changing Binding function to return a list instead of a single Binding object.

@Bean
List<Binding> bindings() {

    return Arrays.AsList(BindingBuilder.bind(queue()).to(exchange()).with(topic1), 
                        BindingBuilder.bind(queue()).to(exchange()).with(topic2));
}

Tip: You do not need to pass queue and exchange as method params. You can directly refer the bean methods to pass the information of exchange and queue.

Refer documentation for more details.

like image 155
Nitin Arora Avatar answered Oct 23 '22 11:10

Nitin Arora


List<Binding> bindings() not supported by spring boot 2.+ versions. This one works;

@Bean
public Declarables bindings() {

    return new Declarables(
            BindingBuilder
                    .bind(bookingAddQueue())
                    .to(bookingExchange())
                    .with("add")
                    .noargs(),
            BindingBuilder
                    .bind(bookingEditQueue())
                    .to(bookingExchange())
                    .with("edit")
                    .noargs());
}
like image 1
Inanc Cakil Avatar answered Oct 23 '22 09:10

Inanc Cakil