8

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

  • 项目清单

    from("direct:start").to("activemq:queue: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>
4

2 回答 2

17

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

<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”或任何合适的名称。

于 2012-11-08T11:36:03.413 回答
0

I have been trying to achieve this with the difference that my spring configuration is not in xml. It is helpful to know that you can achieve the same outcome by using spring annotations in a few ways.

The key to achieving this is registering the component with the desired name. For example:

camelContext.addComponent("activemq2", jmsComponentInstance);

There is two ways of achieving this. Namely by creating two beans with qualifiers which identifies them from each other and then wiring those beans and registering them as components. Alternatively (this is preferable) you can create the bean and register the component all at once. Below are examples of both:

1 - Create Bean and Register elsewhere

@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 - Create Bean and Register in one place within your @Configuration 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
}
于 2017-01-09T15:02:15.060 回答