【问题标题】:Spring configuration for multiple Activemq remote brokers多个 Activemq 远程代理的 Spring 配置
【发布时间】:2012-10-28 13:20:13
【问题描述】:

如何在 spring 上下文中配置多个远程 activemq 代理(不同的 IP 地址)?以下是 1 个远程代理的配置。我正在使用骆驼创建路由,这些路由在多个远程代理中的不同队列之间产生和使用消息。基于以下路由,系统如何知道每个队列属于哪个远程代理?

  • 列表项

    from("direct:start").to("activemq:queue:outgoingRequests")

  • 列表项

    from("activemq:queue:incomingOrders").to("log:Events? showAll=true").to("bean:jmsService")

1 个代理的 Spring 上下文 org.camel.routes

<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="tcp://10.1.11.97:61616" />
</bean>

<bean id="pooledConnectionFactory"
    class="org.apache.activemq.pool.PooledConnectionFactory" init-
            method="start" destroy-method="stop">
    <property name="maxConnections" value="8" />
    <property name="connectionFactory" ref="jmsConnectionFactory" />
</bean>

<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
    <property name="connectionFactory" ref="pooledConnectionFactory"/>
    <property name="concurrentConsumers" value="10"/>
</bean>

<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
    <property name="configuration" ref="jmsConfig"/>
</bean>

【问题讨论】:

    标签: spring activemq apache-camel


    【解决方案1】:

    只需添加更多不同名称的组件

    <bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
       <property name="configuration" ref="jmsConfig"/>
    </bean>
    
    <bean id="activemq2" class="org.apache.activemq.camel.component.ActiveMQComponent">
       <property name="configuration" ref="myOtherJmsConfig"/>
    </bean>
    

    然后简单地使用名称:

    <from uri="activemq:queue:MY.QUEUE"/><!-- get from "1st" ActiveMQ -->
    <to uri="activemq2:queue:MY.QUEUE"/> <!-- put to same queue name on other ActiveMQ -->
    

    实际上,您可以随意称呼它们,例如“EuropeanMarketBroker”或任何合适的名称。

    【讨论】:

    • 可以将connectionFactory 设置为URI 选项吗? &lt;to uri="activemq:queue:MY.QUEUE?connectionFactory=activemq2pool"/&gt;,我假设它是 ConnectionFactory 类型,而不是组件?
    【解决方案2】:

    我一直在尝试实现这一点,不同之处在于我的 spring 配置不在 xml 中。知道您可以通过几种方式使用 spring 注释来实现相同的结果是有帮助的。

    实现这一点的关键是使用所需名称注册组件。 例如:

    camelContext.addComponent("activemq2", jmsComponentInstance);
    

    有两种方法可以实现这一点。即通过创建两个带有限定符的 bean 来识别它们,然后连接这些 bean 并将它们注册为组件。或者(这是更可取的)您可以一次创建 bean 并注册组件。以下是两者的示例:

    1 - 创建 Bean 并在别处注册

    @Configuration
    public class ClassA{    
      @Bean @Qualifier("activemq2") public JmsComponent createJmsComponent(){
          return JmsComponent.jmsComponentAutoAcknowledge(..);//Initialise component from externalised configs
      }
    }
    
    @Component
    public class ClassB{
    
      @Autowired private CamelContext camelContext;
    
      @Autowired @Qualifier("activemq2")
      private JmsComponent jmsComponent;
    
      public void someMethod(){
        camelContext.addComponent("activemq2", jmsComponent);
      }
    }
    

    2 - 在 @Configuration bean 中的一处创建 Bean 并注册。

    @Bean @Autowired public JmsComponent createJmsComponent(CamelContext camelContext){
        JmsComponent component = JmsComponent.jmsComponentAutoAcknowledge(..);//Initialise component from externalised configs
        camelContext.addComponent("activemq2", component);//Add Component to camel context
        return component;//Return component instance
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-20
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 2015-03-11
      • 2011-01-11
      • 2018-02-16
      • 2015-05-21
      相关资源
      最近更新 更多