Search code examples
javaspring-rabbit

Spring AMQP cannot create bean to return a List<Binding>


I am trying to use Spring AMQP, version 2.1.2.release, to create multiple bindings to a topic exchange.

I found this question: How to setup multiple topics in a RabbitMQ Java config class using Spring Framework?

Which seemed to have the answer. I also found the documention which provides the same solution.

However, the Bindings are not being created when I return a List in my Bean. If I return a single Binding, then it does work. I cannot add a comment to that question due to lack of reputation.

Here is my code:

    @Bean
public TopicExchange topicExchange() {
    return new TopicExchange("topicExchange");
}

@Bean
public Queue testQueue() {
    return new Queue("testQueue");
}

@Bean
List<Binding> multipleBindings() {
    return Arrays.asList(
            BindingBuilder.bind(testQueue()).to(topicExchange()).with("t1"),
            BindingBuilder.bind(testQueue()).to(topicExchange()).with("t2"));
}

@Bean
Binding singleBinding() {
    return BindingBuilder.bind(testQueue()).to(topicExchange()).with("t3");
}

In this code, I get the "t3" topic binding, but do not see "t1" or "t2" when I look at the Rabbit Management console.

Please help, as this code looks very simple and it follows the documentation. What am I missing?

Thank you


Solution

  • You are referring to very old documentation. According the version you use, there is already a Declarables container instead of List to use: https://docs.spring.io/spring-amqp/docs/2.1.4.RELEASE/reference/#collection-declaration