Tom Donohue Tom D

Understanding Camel routes

Understanding routes and how to use them in Apache Camel

Tags: Comments

Infographic: Camel route and components explained

I created this infographic to show how the different concepts in Camel come together in a route.

Here, 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.