Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Setting Up a Failover Scenario Using Apache Camel ZooKeeper

April 3, 2018
Chandra Shekhar Pandey
Related topics:
JavaMicroservices
Related products:
Red Hat Fuse

    In this article, we will discuss using the Apache Camel ZooKeeper component, and demonstrate how easily we can set up a fail-over scenario for Apache Camel Routes. While working in a clustered environment, situations arise where a user wants to have a backup or slave route which will become active only when the master (or the currently active) route stops working. There are different ways to achieve this: one can use Quartz to configure a master-slave setup; JGroups can also be used. In a Fabric8 environment, there is a master component which can easily be set up as a failover scenario.

    There is one more way to discuss here in detail using ZooKeeperRoutePolicy. This policy uses a common znode (zookeeper registry) path across all instances. Each instance will be registered with a unique-id in the znode path; thus there will be a list of unique-ids. Each unique-id represents one instance of a route deployed with ZooKeeperRoutePolicy route policy.

    Whichever node is at the top of the list will be considered as the master route. Other remaining nodes will be dormant and not active. Once the master route stops, one of the slaves would be elected as the master route. Then it will continue to serve requests.

    Setting Up The Project and Configuring Camel Zookeeper as the MasterSlave Route

    Now let's discuss the coding portion. I have tested this example in Red Hat JBoss Fuse 6.3.0. You can find the code base at my personal github link.

    1. Create a Maven project with following structure:

    [cpandey@cpandey CamelZookeeperMasterSlave]$ tree
    .
    ├── pom.xml
    ├── ReadMe.txt
    └── src
     └── main
      ├── java
       └── com
        └── test
         └── pckg
          └── MyRouteBuilder.java
      └── resources
       ├── log4j.properties
       └── OSGI-INF
        └── blueprint
         └── blueprint.xml

    2. The pom.xml should have following dependencies:

     <dependency>
     <groupId>org.apache.camel</groupId>
     <artifactId>camel-core</artifactId>
     </dependency>
     <dependency>
     <groupId>org.apache.camel</groupId>
     <artifactId>camel-blueprint</artifactId>
     </dependency>
     <dependency>
     <groupId>org.apache.camel</groupId>
     <artifactId>camel-zookeeper</artifactId>
     </dependency>

    3. A version of these artifacts are picked from the BOM version mentioned in pom.xml:

     <dependencyManagement>
     <dependencies>
     <dependency>
     <groupId>org.jboss.fuse.bom</groupId>
     <artifactId>jboss-fuse-parent</artifactId>
     <version>6.3.0.redhat-310</version>
     <type>pom</type>
     <scope>import</scope>
     </dependency>
     </dependencies>
     </dependencyManagement>

    4. Here, I have used JAVA DSL for the Camel route. Hence in blueprint.xml, I have defined bean, which refer class com.test.pckg.MyRouteBuilder where camel route is defined.

    <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
    
     <camelContext id="_context1" xmlns="http://camel.apache.org/schema/blueprint">
      <routeBuilder ref="myBuilder"/>
     </camelContext>
     <bean class="com.test.pckg.MyRouteBuilder" id="myBuilder"/>
    </blueprint>
    
    

    5. Following the camel route defined in MyRouteBuilder.java</code>:

    @Override
    public void configure() {
    System.out.println("......Inside MyRouteBuilder.....");
    ZooKeeperRoutePolicy policy = new ZooKeeperRoutePolicy("zookeeper:localhost:" + 2181 + "/someapp/somepolicy", 1);
    from("file:/path/to/someFolder").routePolicy(policy).id("Master").log("Body... ${body} "+new Date());
    }
     
    The tasks performed by this piece of code are:
    • Define a ZooKeeperRoutePolicy(String uri, int enabledCount) which creates znode at path /someapp/somepolicy. ZooKeeper Server is listening at localhost:2181.
    • We want to have one instance active at a time. For a Master-slave scenario, the route is configured with 1 route instance and only the first entry in the listing will start its route. However, if there is a scenario when we need two or more route instances to be up at the same time, we can set enabledCount to 2 or more.
    • We then have a file based consumer polling at path/to/someFolder where routePolicy is set to ZooKeeperRoutePolicy.  Finally, we log the body which is set with the content of the file polled or consumed.

    How to Deploy in Red Hat JBoss Fuse with a Fabric8 Environment

    1. Fabric8 uses Apache ZooKeeper (which is a highly reliable distributed coordination service) as its registry to store the cluster configuration and node registration. Hence, we don't need to setup a different ZooKeeper server. We can register znode in an existing ZooKeeper registry and use it to set up MasterSlave route.

    2. Follow the steps below to deploy this application from Karaf terminal of Red Hat JBoss Fuse:

    profile-edit --feature fabric-zookeeper-commands default
    container-create-child root abc
    container-create-child root pqr 
    profile-create testZK
    profile-edit --feature camel-zookeeper --feature camel-blueprint testZK
    profile-edit --bundle mvn:com.mycompany/camel-zookeeper-example/1.0.0-SNAPSHOT testZK
    container-add-profile abc testZK
    container-add-profile pqr testZK

    3. As per our route, at location /path/to/someFolder, create a simple text file and add some text data to it and save it.

    4. In logs for one of the nodes we can check following; which will ensure that  znode is created and that the route is active:

    2018-03-25 17:30:30,075 | WARN | masterSlaveRoute | ZookeeperProducer | 191 - org.apache.camel.camel-core - 2.17.0.redhat-630329 | Node '/someapp/somepolicy/localhost.localdomain-cd65c741-4252-420a-88a4-f084a933fcaf' did not exist, creating it.
    2018-03-25 17:30:30,080 | INFO | masterSlaveRoute | ZooKeeperElection | 195 - org.apache.camel.camel-zookeeper - 2.17.0.redhat-630329 | Candidate node '/someapp/somepolicy/localhost.localdomain-cd65c741-4252-420a-88a4-f084a933fcaf' has been created
    2018-03-25 17:30:30,107 | INFO | masterSlaveRoute | BlueprintCamelContext | 191 - org.apache.camel.camel-core - 2.17.0.redhat-630329 | Route: election-route-localhost.localdomain-cd65c741-4252-420a-88a4-f084a933fcaf started and consuming from: Endpoint[zookeeper://localhost:2181/someapp/somepolicy]
    2018-03-25 17:30:30,112 | INFO | masterSlaveRoute | Master | 191 - org.apache.camel.camel-core - 2.17.0.redhat-630329 | Body... Sun Mar 25 17:27:32 IST 2018

    4. Using the following command from Karaf terminal, we can easily identify which nodes are registered. This unique number we can be identified in logs also:

    zk:list -r /someapp/somepolicy
    #Output we get is like
    /someapp/somepolicy/localhost.localdomain-cd65c741-4252-420a-88a4-f084a933fcaf0000000013

    5. If the route or application is stopped, this znode entry will be deleted.

     How to Deploy in Red Hat JBoss Fuse in a Standalone Environment

    1. Download ZooKeepers latest release from here.  I downloaded zookeeper-3.4.11.

    2. Extract zookeeper-3.4.11.tar.gz. Now go to zookeeper-3.4.11/conf folder:

    $ vi conf/zoo.cfg
    
    tickTime = 2000
    initLimit = 10
    syncLimit = 5
    dataDir = /path/to/zookeeper/data
    clientPort = 2181

    3.  Finally, start the ZooKeeper server:

    $ bin/zkServer.sh start

    4. Now on Red Hat JBoss Fuse 6.3.0 Karaf terminal, run the following commands:

    features:install camel-zookeeper
    osgi:install -s mvn:com.mycompany/camel-zookeeper-example/1.0.0-SNAPSHOT

    5.  As per our route, at location /path/to/someFolder, create a simple text file and add some text data to it and save it. We will get similar logs (as mentioned above) for deployment in the Fabric8 environment. Here we can have multiple Red Hat JBoss Fuse standalone environments running at different nodes having the same application deployment.

    6. If deployment and testing are done, you can stop the ZooKeeper server with the following command:

    bin/zkServer.sh stop

    That's it. I hope it helps you to to set up a master-slave scenario with camel-routes. This might help you to achieve a successful failover scenario.

    Last updated: March 23, 2023

    Recent Posts

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.