Tom Donohue Tom Donohue

What are Camel routes?

Understanding routes and how to use them in Apache Camel

Tags: Comments

If you’re still learning the basics of Camel then you might have come across the term route.

But what exactly is a Camel route?

A route in Apache Camel is a sequence of steps, executed in order by Camel, that consume and process a message. A Camel route starts with a consumer, and is followed by a chain of endpoints and processors.

So firstly, a route receives a message, using a consumer – perhaps from a file on disk, or a message queue. Then, Camel executes the rest of the steps in the route, which either process the message in some way, or send it to endpoints (which can include other routes) for further processing.

How do you create a route in Camel?

To create a route in Camel, you first define it in code. This is called a route definition, and it is usually written in Java or XML.

Then, you start Camel, passing it your route definition. Camel reads the route definition and creates the route inside the Camel Context. The Camel Context is an engine which is part of Camel, and which runs your routes.

If you’re using some frameworks (like Spring Boot), Camel will try to discover your routes automatically.

Once the Camel Context has started, you can inspect it to see all of the routes that are running.

An example Camel route

To see what a route actually looks like, here’s an example.

First, this route is written in the Java syntax or DSL (Domain-Specific Language). In Java, routes go inside a RouteBuilder class, which has a configure() method that you add your route code into:

import org.apache.camel.builder.RouteBuilder;

public class MyFirstRouteBuilder extends RouteBuilder {

    public void configure() throws Exception {
        from("jms:queue:HELLO.WORLD")
            .log("Received message - ${body}");
    }
}

This route uses the JMS component, to receive a message from a queue called HELLO.WORLD. Then, it writes a log message to the console, containing the body of the message (${body}).

Camel route and components explained (Infographic)

Sometimes it can be difficult to understand exactly what a route does, and how it works. So I created this infographic to show how the different concepts in Camel come together in a route.

In this diagram, we have one route. We use the File component to read and write files. We also use a content-based router to filter files based on their filename. The File component helps us by adding headers to the Exchange, which we can use in a Predicate expression.

Infographic showing a Camel route, featuring components, endpoint URI, EIP, Predicate, content-based router and a Camel route
Routes in Apache Camel explained

How do you call a Camel route from Java code?

If you’ve already embedded Camel in your application, then you can send a message to a Camel endpoint by using the Camel API.

To send a message to a Camel route from your code, grab the ProducerTemplate object. Then:

  • to send a message asynchronously, use one of the sendXXX() methods
  • to send a message synchronously (and use the response), use one of the requestXXX() methods

For example, if you were using Spring you might do something like this to send a message asynchronously (fire-and-forget) to an endpoint:

import org.apache.camel.ProducerTemplate;

public class InvokeCamelRoute {

    @Autowired
    protected ProducerTemplate template;

    public void invokeRoute() {
        template.sendBody("direct:start", "Your message body goes here");
    }

}

Comments

What do you think? You can use Markdown in your comment. To write code, indent each line with 4 spaces.