Running Apache Camel standalone

Want to create the smallest, simplest Camel route? A “Hello World” example? You can write and start a Camel route in just a single Java class.

In a standalone Java application with a main() method, you can define a CamelContext, add routes and then start Camel. It’s a very simple way to run Camel routes.

If you have a simple Camel route that you want to run periodically (for example, once per day), then you might find this approach useful.

In this article I’m going to set up a very simple Camel route and show how to run it.

TL;DR? See a demo here.

Want to see it in action first? Check out an online version of this solution, implemented on the online IDE, REPL.it.

Click the Play button to run the example.

Running a Camel route from a Java main() method - example

Follow the steps below to create a a new CamelContext containing a route, and keep it running for 5 seconds.

In this example, I keep the route running for a few seconds using Java’s Thread.sleep() method, before stopping the route.

  1. Create a new Maven project and add the camel-core dependency to your Maven POM:

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
        <version>2.20.1</version>
    </dependency>
    

    If you’re just running a simple Camel route, then you just need the camel-core dependency.

  2. Create a new Java class, MyCamelRoute.class. In principle, the class needs to do these things:

    • Define a CamelContext by creating a new DefaultCamelContext() object

    • Use the addRoutes() method and the RouteBuilder() to add routes to the CamelContext.

    • Use the start() method to start the CamelContext.

    • Sleep for a few seconds, using Thread.sleep().

    • Use the stop() method to stop the CamelContext.

    The following example Java class shows how to do this:

    package com.cleverbuilder.examples;
    
    public class CamelDemoStartStop {
        public static void main(String args[]) throws Exception {
            CamelContext context = new DefaultCamelContext();
    
            context.addRoutes(new RouteBuilder() {
                public void configure() {
                    from("timer://foo?period=5000")  // Create a message every 5 seconds
                    .setBody().simple("Hello, world!")
                    .to("log:mylogger");
                }
            });
    
            context.start(); // start the route
            Thread.sleep(30000L); // let the route run for 30 seconds
            context.stop(); // stop the route
        }
    }
    
  3. Now compile your app using Maven:

    mvn clean package
    

    This will compile your class into the target/ directory.

  4. Then, once you’ve compiled your class, you can just use the following to run your route locally:

    mvn exec:java -Dexec.mainClass=com.cleverbuilder.examples.CamelDemoStartStop
    

    If you’ve used my example class above, then you’ll see a log message is written every 5 seconds, and the CamelContext is terminated after 30 seconds.

    Running Camel in a standalone Java app from the command line

  5. (Optional) If you want to run your route without Maven, then you need to make sure that you’ve got all the dependencies you need. You can do this by first running:

    mvn clean package dependency:copy-dependencies
    

    And then, from the target/ directory, you can run your route using:

    java -cp "dependency/*:classes/" com.cleverbuilder.examples.CamelDemoStartStop
    

    or:

    java -cp "dependency/*:camel-standalone-helloworld-1.0-SNAPSHOT.jar" com.cleverbuilder.examples.CamelDemoStartStop
    

Want to get the example code? You can get the full code and run it on your desktop, just pick it up from GitHub:

Get the example code on GitHub


Thoughts? Post your feedback in the comments below! Cheers!