如何在从站在故障转移中变为活动状态时,如何在从属ActiveMQ上启动Camel路由?

Tom*_* L. 6 failover activemq-classic apache-camel master-slave

我对嵌入式Camel路由中的远程JMS队列有持久的消费者.是否可以使用主从配置进行此类路由?现在看来,当启动从属ActiveMQ时,Camel路由已经启动并激活,而不是在发生实际故障转移时.

现在它导致从属实例接收也发送给主服务器的相同消息,这会导致重复消息在故障转移时到达队列.

我正在使用ActiveMQ 5.3和Apache Camel 2.1.

Jas*_*man 3

不幸的是,当从代理启动时,CamelContext 和路由也会启动。不过,您可以通过执行以下操作来实现此目的:

在使用从代理部署的camelContext上添加以下autoStartup属性以防止路由启动:

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring" autoStartup="false">

...

</camelContext>
Run Code Online (Sandbox Code Playgroud)

接下来,您需要创建一个实现 ActiveMQ 服务接口的类。示例如下:

package com.fusesource.example;

import org.apache.activemq.Service;
import org.apache.camel.spring.SpringCamelContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Example used to start and stop the camel context using the ActiveMQ Service interface
*
*/
public class CamelContextService implements Service
{
private final Logger LOG = LoggerFactory.getLogger(CamelContextService.class);
SpringCamelContext camel;

@Override
public void start() throws Exception {
    try {
        camel.start();
    } catch (Exception e) {
        LOG.error("Unable to start camel context: " + camel);
        e.printStackTrace();
    }
}

@Override
public void stop() throws Exception {
    try {
        camel.stop();
    } catch (Exception e) {
        LOG.error("Unable to stop camel context: " + camel);
        e.printStackTrace();
    }
}

public SpringCamelContext getCamel() {
    return camel;
}

public void setCamel(SpringCamelContext camel) {
    this.camel = camel;
}
}
Run Code Online (Sandbox Code Playgroud)

然后在代理的配置文件 activemq.xml 中添加以下内容来注册服务:

<services>
      <bean xmlns="http://www.springframework.org/schema/beans" class="com.fusesource.example.CamelContextService">
          <property name="camel" ref="camel"/>
      </bean>
</services>
Run Code Online (Sandbox Code Playgroud)

现在,一旦从属代理接管为主代理,将在服务类上调用 start 方法并启动路由。

我还在这里发布了关于此的博客:http://jason-sherman.blogspot.com/2012/04/activemq-how-to-startstop-camel-routes.html