4

I am trying to connect to and consume from two different clusters of rabbitmq using a spring boot app via xml. It works well when a single rabbit:connection-factory bean is created in the application context. However, when the second one is added, it fails to start the application with the error "Parameter 1 of method rabbitListenerContainerFactory in org.springframework.boot.autoconfigure.amqp.RabbitAnnotationDrivenConfiguration required a single bean, but 2 were found:". How do I go about creating different factories per cluster? Please suggest an alternative way of doing this, if it's not the right approach?

Here is the xml snippet:

<rabbit:connection-factory id="firstConnectionFactory" connection-factory="firstSpringConnectionFactory"  />
<rabbit:connection-factory id="secondConnectionFactory" connection-factory="secondSpringConnectionFactory"/>
<bean id="firstSpringConnectionFactory"
class="org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean">
    <property name="useSSL" value="${rabbitmq.ssl.enabled}" />
    <property name="host" value="${rabbitmq.first.host}"/>
    <property name="virtualHost" value="${rabbitmq.vhost}"/>
    <property name="port" value="${rabbitmq.cluster.port}"/>
    <property name="username" value="${rabbitmq.user}"/>
    <property name="password" value="${rabbitmq.first.password}"/>
</bean>

<bean id="secondSpringConnectionFactory"
class="org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean">
    <property name="useSSL" value="${rabbitmq.ssl.enabled}" />
    <property name="host" value="${rabbitmq.second.host}"/>
    <property name="virtualHost" value="${rabbitmq.vhost}"/>
    <property name="port" value="${rabbitmq.cluster.port}"/>
    <property name="username" value="${rabbitmq.user}"/>
    <property name="password" value="${rabbitmq.second.password}"/>
</bean>

And the listener container code:

ConnectionFactory cf = rabbitConnectionFactory;//One of the connnection factories will be injected here from app context
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(cf);
container.setConcurrentConsumers(count);
container.addQueueNames(queueName);
container.setMessageListener(listener);
container.start();

1 Answer 1

6

Since you don't rely on the Spring Boot here and don't use Spring AMQP annotation support I suggest you to exclude RabbitAnnotationDrivenConfiguration from auto-configuration:

@EnableAutoConfiguration(exclude={RabbitAnnotationDrivenConfiguration.class})

spring.autoconfigure.exclude = org.springframework.boot.autoconfigure.amqp.RabbitAnnotationDrivenConfiguration

If you still need @RabbitListener somewhere in other place of your project, you only have a choice to build all the @EnableRabbit infrastructure manually.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Artem, I am able to get past the error by excluding the class RabbitAutoConfiguration @EnableAutoConfiguration(exclude={org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration.class})
sure, I am going to run some tests and see if nothing breaks because of this change and accept your answer:)
how do I properly use RabbitAnnotationDrivenConfiguration then?
You should not use it: the @EnableRabbit does the stuff for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.