English 中文(简体)
How to start Camel routes on slave ActiveMQ only when slave becomes active in failover?
原标题:

I have a durable consumer to a remote JMS queue in embedded Camel routing. Is it possible to have this kind of routing with master-slave configuration? Now it seems that the Camel routes are started and activated already when slave ActiveMQ is started and not when the actual failover happens.

Now it causes the slave instance to receive the same messages that are also sent to master and this causes duplicate messages to arrive to the queue on failover.

I m using ActiveMQ 5.3 along with Apache Camel 2.1.

最佳回答

Unfortunately, when the slave broker starts so does the CamelContext along with the routes. However you can accomplish this by doing the following:

On the camelContext deployed with slave broker add the following autoStartup attribute to prevent the routes from starting:

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

...

</camelContext>

Next you need to create a class that implements the ActiveMQ Service Interface. A sample of this would be as follows:

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

Then in broker s configuration file, activemq.xml, add the following to register the service:

<services>
      <bean xmlns="http://www.springframework.org/schema/beans" class="com.fusesource.example.CamelContextService">
          <property name="camel" ref="camel"/>
      </bean>
</services>

Now, once the slave broker takes over as the master, the start method will be invoked on the service class and the routes will be started.

I have also posted a blog about this here: http://jason-sherman.blogspot.com/2012/04/activemq-how-to-startstop-camel-routes.html

问题回答

this shouldn t be an issue because the Camel context/routes on the slave will not start until it becomes the master (when the message store file lock is released by the master)

With camel routepolicies you can decide to suspend/resume certain routes based on your own conditions. http://camel.apache.org/routepolicy.html

There is an existing ZookeeperRoutePolicy that can be used to do leader election. http://camel.apache.org/zookeeper.html (see bottom of the page)





相关问题
Geronimo vs Glassfish

For a production environment, is Apache Geronimo better for applications that uses ActiveMQ, Derby, Solr?

Anyone using multicast with ActiveMQ

I m having trouble setting up multicast transport using ActiveMQ. I noticed with version 5.2 there is a bug that prevents it from even building the correct factory (fixed in 5.3). The fact this bug ...

How to optimize activemq

I m using ActiveMQ on a simulation of overloading servers in Java. And mainly it goes ok, but when I get over 600 requests the thing just go WTF! I think the bottleneck is my Master Server which is ...

ActiveMQ Consumer / Producer Connection Listener

I can t seem to find a way to listen for new producer and consumer connections (or connection interrupts) in ActiveMQ (Java Version). I want to be able to tell the consumers (or they can find out ...

热门标签