2017-08-09 91 views
3

我想通過xml使用spring啓動應用程序連接並使用兩個不同的rabbitmq羣集。當在應用程序上下文中創建單個rabbit:connection-factory bean時,它可以很好地工作。但是,添加第二個時,無法啓動應用程序並出現錯誤「org.springframework.boot.autoconfigure.amqp.RabbitAnnotationDrivenConfiguration中方法rabbitListenerContainerFactory的參數1需要單個bean,但找到2個:」。我如何着手爲每個羣集創建不同的工廠?如果這不是正確的方法,請提出一個替代方法來做到這一點?春季啓動無法創建多個兔子連接工廠

這裏是XML片段:

<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> 

與聽者容器代碼:

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

回答

2

既然你不依賴於Spring啓動這裏不使用Spring AMQP註釋支持我建議你excludeRabbitAnnotationDrivenConfiguration從自動配置:

@EnableAutoConfiguration(exclude={RabbitAnnotationDrivenConfiguration.class}) 

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

如果您仍然需要@RabbitListener在項目的其他位置,您只能選擇構建所有@EnableRabbit基礎設施manually

+0

Thanks @Artem,通過排除類RabbitAutoConfiguration @EnableAutoConfiguration(排除= {org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration.class}) – tj23

+0

非常棒!現在考慮這個:https://stackoverflow.com/help/someone-answers –

+0

當然,我要運行一些測試,看看是否因爲這個改變而沒有任何破解,並接受你的回答:) – tj23