首页 > 解决方案 > 使用 Spring Integration XML 配置连接到多个 ActiveMQ 服务器

问题描述

几天来,我一直在尝试使用 XML 配置在 Spring Integration 中设置多个 ActiveMQ 连接。

我正在使用 spring boot,SI 在上下文中查找名为 jmsConnectionFactory 的 bean 并使用它。但是,如果我必须从/到不同的 ActiveMQ 服务器发送/收听 jms 消息怎么办?

我现在拥有的是这样的:

<bean id="jmsConnectionFactory1"
    class="org.springframework.jms.connection.CachingConnectionFactory">
    <property name="targetConnectionFactory">
        <bean class="org.apache.activemq.ActiveMQConnectionFactory">
            <property name="brokerURL" value="tcp://localhost:61616" />
        </bean>
    </property>
    <property name="sessionCacheSize" value="10" />
    <property name="cacheConsumers" value="false" />
</bean>

<bean id="jmsConnectionFactory2"
    class="org.springframework.jms.connection.CachingConnectionFactory">
    <property name="targetConnectionFactory">
        <bean class="org.apache.activemq.ActiveMQConnectionFactory">
            <property name="brokerURL" value="tcp://192.168.1.59:61616" />
        </bean>
    </property>
    <property name="sessionCacheSize" value="10" />
    <property name="cacheConsumers" value="false" />
</bean>
...
<jms:message-driven-channel-adapter channel="jmsInChannel" destination-name="queue.demo" />
<int:channel id="jmsInChannel" />
...

尝试启动 spring boot 应用程序时出现此错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean named 'jmsConnectionFactory' that could not be found.

The following candidates were found but could not be injected:
- Bean method 'jmsConnectionFactory' in 'ActiveMQXAConnectionFactoryConfiguration' not loaded because @ConditionalOnClass did not find required class 'javax.transaction.TransactionManager'


Action:

Consider revisiting the entries above or defining a bean named 'jmsConnectionFactory' in your configuration.

我遇到了这个解决方案,https://stackoverflow.com/a/43401330/3367392这是一个java配置。我也调查了这个,但它使用的是骆驼https://stackoverflow.com/a/13288312/3367392

有没有办法使用 XML 配置为 Spring Integration 实现相同的目标?

标签: spring-bootspring-integrationactivemqjavabeansspring-jms

解决方案


好的,我明白了,对于一个简单的情况,我只需要像这样将正确的 connectionFactory 添加到适配器

<jms:message-driven-channel-adapter channel="jmsInChannel"
    destination-name="queue.demo"
    connection-factory="jmsConnectionFactory1" />

推荐阅读