首页 文章

多个Activemq远程代理的Spring配置

提问于
浏览
7

如何在spring上下文中配置多个远程activemq代理(不同的IP地址)?以下是1个远程代理的配置 . 我使用camel创建路由,从多个远程代理中的不同队列生成和使用消息 . 根据以下路由,系统如何知道每个队列属于哪个远程代理?

  • 列出项目

从( “直接:启动”)来 . ( “ActiveMQ的:队列:outgoingRequests”)

  • 列出项目

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

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

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

2 回答

  • 15

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

    <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”或其他适合的东西 .

  • 0

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

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

    camelContext.addComponent("activemq2", jmsComponentInstance);
    

    有两种方法可以实现这一目标 . 即通过创建两个带有限定符的bean,这些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
    }
    

相关问题