activemq - 仅当从属在故障转移中变为事件状态时,如何在从属 ActiveMQ 上启动 Camel 路由?

标签 activemq failover apache-camel master-slave

在嵌入式 Camel 路由中,我有一个远程 JMS 队列的持久消费者。是否可以通过主从配置进行这种路由?现在看来,Camel 路由在从 ActiveMQ 启动时已经启动和激活,而不是在实际故障转移发生时。

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

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

最佳答案

不幸的是,当从代理启动时,CamelContext 以及路由也会启动。但是,您可以通过执行以下操作来完成此操作:

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

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

...

</camelContext>

接下来,您需要创建一个实现 ActiveMQ 服务接口(interface)的类。示例如下:
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;
}
}

然后在broker的配置文件activemq.xml中,添加如下内容来注册服务:
<services>
      <bean xmlns="http://www.springframework.org/schema/beans" class="com.fusesource.example.CamelContextService">
          <property name="camel" ref="camel"/>
      </bean>
</services>

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

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

关于activemq - 仅当从属在故障转移中变为事件状态时,如何在从属 ActiveMQ 上启动 Camel 路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2064608/

相关文章:

java - ActiveMQ 给出 : "Could not connect to broker URL: tcp://localhost:61616" after around 10 000 messages

java - ActiveMQ 在 Spring Boot 中延迟传递消息

postgresql - 在两台服务器之间复制选定的 postgresql 表?

apache-kafka - 如果第一个代理宕机,Kafka 消费者将无法消费

java - [spring] DefaultShutdownStrategy、[camel] ShutdownAware 和 [camel] 处理器的组合不起作用

java - 从 java List 对象创建 XML 的最有效方法

ios - StompClientLib - 取消订阅 socketclient

activemq master 不会放弃网络故障

tomcat - Grails Quartz Clustering - 调度程序仍然独立运行

java - 在 spring-boot 中从 application.yaml 文件中读取 SSH 私钥?