On April 6-7, for the sixth time in Moscow, the
JPoint will be
held - an international Java conference for experienced developers. This time Claus Ibsen (Claus Ibsen) - Red Hat senior engineer, Apache guru will take part in it. Below is a translation of the beginning of his book Camel in Action - exclusively for readers of our blog.

Meet Camel
Creating complex systems from scratch is costly and almost always doomed to failure. It is much more efficient and safer to assemble them as a jigsaw puzzle from existing and proven components. In everyday life, we often rely on such integrated systems: from telephone communications and financial transactions to medical services and travel and entertainment planning.
')
A puzzle can be assembled only if you have all its fragments, and they are easily and securely connected to each other. The same is true for systems integration projects. But if the pieces of this puzzle are created in order to form a single picture, then with IT systems, the situation is usually the opposite. As a developer, you, as a rule, are not very worried about the internal structure and operation of integrable systems, you are much more interested in how to interact with it from the outside. A good integration framework (platform) implies two things: firstly, a simple and convenient abstraction apparatus for describing complex integrable systems, and secondly, a connecting mechanism that allows to combine them into a single whole.
An example of such a platform is Apache Camel. This book will help you understand what Camel is, how to use it, and why we consider it one of the best integration platforms. The first chapter begins with an introduction to Camel and a description of its main features. Then we look at the Camel distribution and explain how to follow the examples in this book. At the end of the chapter, we present the basic concepts of Camel, necessary for understanding its architecture.
Ready? Then meet Camel.
Introducing Camel
Camel is an application integration platform designed to work productively and with pleasure. Camel's history began in 2007, and today it is a mature open source project that is licensed under the Apache 2 license and around which there is a powerful development community.
Camel's main goal is to simplify integration. We hope that by reading this book, you will appreciate the capabilities of Camel and include it in your arsenal of essential tools.
Camel is part of the Apache family of projects, getting its name thanks to the brevity and ease of remembering. There is a version that the project is obliged by the name of the brand of cigarettes that one of its founders smoked. Other versions can be found in the list of questions and answers on
the project website .
What is Camel
The core of the Camel platform is the routing mechanism, more precisely, the linker of routing mechanisms. With the help of the linker, you can define your own routing rules, decide which sources to receive messages from, and determine how to process and send these messages further. The Camel integration language allows you to specify complex routing rules that are close to business processes. As shown in Fig. 1.1, Camel is the link between unrelated systems.
One of the fundamental principles of Camel is that he makes no assumptions about the data being processed. This is an important point, because it allows you, as a developer, to integrate systems of various types without converting data into the canonical format.
Fig. 1.1 Camel is the link between unrelated systems.Thanks to high-level abstractions, Camel allows you to use the same API to interact with different systems using different protocols and data types. APIs for specific protocols and data types are implemented as corresponding components. By default, Camel supports more than 280 protocols and data types, and, thanks to an extensible modular architecture, allows you to independently implement support for the necessary protocols, both proprietary and open. This eliminates unnecessary data conversion and makes Camel not only faster, but less voracious. As a result, Camel is great for embedding in projects that require advanced processing capabilities. In particular, Camel is already used for integration tasks in open source projects such as Apache ServiceMix, Karaf, and ActiveMQ.
Now about what Camel is not. This is not an ESB service bus, although some people call it the lightweight version of ESB because it supports routing, transformation, orchestration, monitoring, and other functions. Camel does not have a container and a reliable messaging bus, although it can be used as such, for example, in the already mentioned Apache ServiceMix. Therefore, we prefer to call Camel an integration platform, not an ESB solution.
If at the mention of ESB you immediately come to mind complex monstrous systems, then we hasten to reassure: Camel with the same success can be used in very small things, like microservices or IoT (Internet of Things) gateways.
To understand what Camel is, consider its main functions.
Why use camel
Camel is a practical embodiment of new ideas in the field of integration. Next, we will look at Camel’s capabilities in detail, but for now let's list them:
- Clearing House and Routing Mechanism
- Extensible Component Library
- Enterprise Application Integration Patterns (EIP)
- Domain-specific language (DSL)
- Payload formatted router
- Plug-in-based architecture
- Model Plain Old Java Object (POJO)
- Easy to configure
- Automatic type converters
- Lightweight core suitable for embedding in microservices
- Cloud support
- Testing Package
- Active development community
Now we will sort this list on points.
Clearing House and Routing Mechanism
The mediation and routing mechanism is the core of Camel. The routing mechanism selectively moves messages according to the route configuration. In Camel, routes are configured using integration templates and a subject language (more about them later).
Extensible Component Library
Camel has an extensive library of more than 280 components, allowing it to connect through a variety of transports, use different APIs and understand different data formats. In Fig. 1.2. you will surely find technologies that have already been used or would like to use. Of course, in the framework of this book it is impossible to consider all the components of Camel, but we still discuss two dozen of the main ones.
Fig. 1.2 Ability to connect to anything: Camel supports over 280 transports, APIs, and data formats.Enterprise Application Integration Patterns (EIP)
With all the variety of integration problems, many of them, as noted by Gregor Hohpe and Bobby Woolf, have similar solutions. These two have released the book "Enterprise Integration Patterns" (translated into Russian - "Patterns of integration of corporate applications"), which should read anyone who considers himself an expert in the field
of application integration . In addition, this book will help you understand Camel concepts much faster and easier.
Integration patterns (EIP) are useful not only because they offer proven solutions for various integration problems, but also because they simplify the description and presentation of these problems themselves by providing appropriate semantics. Camel is largely based on EIP templates. However, although EIP templates both describe problems and solutions, and also provide a common dictionary, this dictionary is not formalized. Camel removes this gap by providing a language for describing integration solutions. There is an almost one-to-one relationship between the patterns described in the book “Enterprise Integration Patterns” and the subject language CamelDSL.
Domain-specific language (DSL)
At the time Camel appeared, his main contribution to the cause of integration was the domain-oriented language (DSL). Over time, this example was followed by other integration frameworks, and now DSL is in Java, XML and other programming languages. DSL allows the developer to focus on the integration tasks, without being distracted by the features of the tool used (programming language). Consider a couple of DSL examples that use different formats, but are completely equivalent in functionality:
Java DSL1 from("file:data/inbox").to("jms:queue:order");
XML DSL 1 <route> 2 <from uri="file:data/inbox"/> 3 <to uri="jms:queue:order"/> 4 </route>
This is real code, and it shows how easy it is to send a file from the desired folder to the JMS (Java Message Service) queue. Since DSL is based on a popular programming language, the developer still has access to familiar and convenient tools of the IDE environment, such as auto-completion when typing or detecting compiler errors, as shown in Fig. 1.3.
Fig. 1.3 Camel DSL languages ​​are based on popular programming languages ​​like Java, so you have access to convenient and familiar features of the IDE environment.The figure shows how the auto-completion feature when typed into the Eclipse IDE offers a list of possible DSL expressions.
Payload formatted router
Camel can route useful data in any form, not limited to any single normalized format, for example, XML payload. Therefore, in order to route the load, it does not need to be converted to the canonical format.
Plug-in-based architecture
The modular architecture of Camel allows you to load into it any components, both included in the package and those created by third-party developers, including you. In addition, you can configure almost everything in Camel, many of its functions are pluggable and customizable: ID generation, flow control, completion sequence, stream caching, and more.
POJO model
Java beans (or Plain Old Java Objects, POJOs) in Camel are first-class citizens, so you can use them in integration projects almost anywhere and anytime. In many cases, you can even extend the built-in functionality of Camel with your code.
Easy to configure
Configuring in Camel is minimized wherever possible. To set endpoints directly in routes, a simple and clear URI configuration is used.
For example, you can configure a Camel route that will start from a file endpoint and recursively scan subfolders for .txt files, just by writing the following:
1 from("file:data/inbox?recursive=true&include=.*txt$")...
Automatic type converters
Camel has a built-in type conversion mechanism with more than 350 converters. You no longer need to configure the conversion rules, for example, to convert an array of bytes to a string. And even if Camel does not have the necessary converter, you can always write it yourself. And the best part is that it all works autonomously, without your participation.
Camel components also use autoconversion of types, so they can take most types of data and convert them to the type they can use. In the Camel community, this feature is the top favorite, and some even wonder why it is not in Java.
Lightweight core suitable for embedding in microservices
Kernel Camel really weighs quite a bit: the size of the library is about 4.9 MB, another 1.3 MB take runtime dependencies. Therefore, Camel is easily embedded anywhere and deployed anywhere, including standalone applications, microservices, web applications, Spring applications, Java EE applications, OSGi, Spring Boot, WildFly, and cloud platforms such as AWS, Kubernetes or Cloud Foundry. Camel was not originally created as a server system or an ESB solution, but as something that can be embedded in any executable things where there is Java.
Cloud support
Camel is not only cloud-oriented, but also offers many components for connecting to cloud services, such as:
- Amazon DynamoDB, EC2, Kinesis, SimpleDB, SES, SNS, SQS, SWF, and S3
- Braintree (PayPal, Apple, Android Pay, etc.)
- Dropbox
- Facebook
- Github
- Google Big Query, Calendar, Drive, Mail and Pub Sub
- HipChat
- LinkedIn
- Salesforce
- Twitter
- and many others
Testing Package
Camel comes with a package for testing applications. This package is actively used for testing Camel itself and has more than 18 thousand unit tests. The package contains special components that allow, for example, to simulate real endpoints or to set the expected result so that Camel can check whether the application meets the specified requirements or not.
Active development community
The Camel project has an active and active community, which at the time of writing this book has been living and developing for over 10 years. Having such a community is an important factor when choosing open source software to build your own applications. If the project is inactive, you can hardly count on the support of the community and you are left alone with your problems. Conversely, participants in an active project, such as Camel, and, both developers and ordinary users, are always ready to help.
Now that we have briefly reviewed the main features of Camel, it's time to move on to practice: get its distribution and consider a few examples.
Getting started
In this section, we describe how to get the Camel distribution, and briefly describe its contents. Then we will show how to perform the source code examples in this book using Apache Maven.
Let's start with the distribution.
Download and install Camel
Camel is available
on the Apache Camel project website . There you will find a list of all versions and download links for the latest release of Camel.
In this book, we use Camel 2.20.1. To get this version, use the corresponding link on the download page. At the end of this page there are links to two distributions: a zip file for Windows and a tar.gz for macOS / Linux. Download the desired file and unzip it to a local disk.
Open the console and go to the folder where you unpacked the Camel distribution. It will contain the following files:
1 [janstey @ ghost apache-camel-2.20.1] $ ls
2 doc examples lib LICENSE.txt NOTICE.txt README.txt
As you can see, the distribution has a simple and clear structure, namely:
- doc - folder with the Camel manual in HTML format. This is just a copy of the Camel site at the time of release. It is useful in those moments when you do not have access to the site or not at hand of this book.
- examples - folder with 97 examples of Camel
- lib - folder with all Camel libraries. Later we will show how to use Maven to simply load dependencies for non-core components.
- LICENSE.txt - file with the text of the license for the Camel distribution. Since this is an Apache project, the license is an Apache License, version 2.0
- NOTICE.txt - file with copyright information on third-party dependencies that are part of the Camel distribution
- README.txt - a file with a brief introduction to Camel and a list of links that will help beginners to get started quickly
Now we will understand how to perform the examples given in this book by Camel.
First launch camel
So, above we told how to get the Camel distribution, and briefly described its contents. Among other things, there are examples with explanations and instructions, which we advise to study.
However, in this book we will not use the distribution at all. All the examples of source code shown here use Apache Maven, which automatically loads all the necessary Camel libraries. Therefore, you will not need to control that they are registered in the classpath.
All code examples from this book can be found on the
GitHub website.
The first example that we will consider is such a “hello world” in the realities of integration, in other words, it is file routing. Suppose you need to read the files in one folder (data / inbox), process them in a certain way and write the results to another folder (data / outbox). For simplicity, we omit the processing, and then the task will be reduced to copying the source files, as shown in Fig. 1.4.
Fig. 1.4 Routing files from the data / inbox folder to the data / outbox folder.Easier, nowhere right? This is how the implementation of this task looks like on “pure” Java, without using Camel.
Listing 1.1 Routing files from one folder to another in "clean" Java 1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.io.OutputStream; 6 7 public class FileCopier { 8 9 public static void main(String args[]) throws Exception { 10 File inboxDirectory = new File("data/inbox"); 11 File outboxDirectory = new File("data/outbox"); 12 outboxDirectory.mkdir(); 13 File[] files = inboxDirectory.listFiles(); 14 for (File source : files) { 15 if (source.isFile()) { 16 File dest = new File( 17 outboxDirectory.getPath() 18 + File.separator 19 + source.getName()); 20 copyFile(source, dest); 21 } 22 } 23 } 24 25 private static void copyFile(File source, File dest) 26 throws IOException { 27 OutputStream out = new FileOutputStream(dest); 28 byte[] buffer = new byte[(int) source.length()]; 29 FileInputStream in = new FileInputStream(source); 30 in.read(buffer); 31 try { 32 out.write(buffer); 33 } finally { 34 out.close(); 35 in.close(); 36 } 37 } 38 }
It would seem a very simple task, but FileCopier already has 37 lines of code. In addition, I had to use low-level file APIs, and - importantly - I had to remember to close the resources correctly. Now imagine that you only need to copy new files from the data / inbox folder. Then you need to set a timer and keep track of which files have already been copied. The difficulty grows right before our eyes.
Fortunately, the integration tasks, like the one just discussed, have already been solved a thousand times. Therefore, it is not necessary to reinvent the wheel and write code with your hands. Let's see how all this functionality is implemented by means of an integration framework, such as Apache Camel.
Listing 1.2 Routing files from one folder to another on Apache Camel 1 import org.apache.camel.CamelContext; 2 import org.apache.camel.builder.RouteBuilder; 3 import org.apache.camel.impl.DefaultCamelContext; 4 5 public class FileCopierWithCamel { 6 7 public static void main(String args[]) throws Exception { 8 CamelContext context = new DefaultCamelContext(); 9 context.addRoutes(new RouteBuilder() { 10 public void configure() { 11 from("file:data/inbox?noop=true") 12 .to("file:data/outbox"); 13 } 14 }); 15 context.start(); 16 Thread.sleep(10000); 17 context.stop(); 18 } 19 }
Most of the code in this example is a template. Any Camel application uses CamelContext, which subsequently starts and then stops. We also added a sleep method to give Camel time to copy files. The main thing to look out for in Listing 1.2 is the route.
The routes in Camel are determined in the same order in which they are read. For example, the route from the example above can be read as follows: accept messages from the file (from file) in the data / inbox folder with the noop parameter and send to the file (to file) in the data / outbox folder. The noop parameter means that the source files must be left in place, otherwise Camel will transfer them. Most people who have never come across Camel may well understand what this route is doing. In addition, note that if we discard the template code, the whole task is reduced to two lines of Java code.
To run this example, you need to download and install
Apache Maven . After it is ready, open the console and go to the chapter1 / file-copy folder in the source directory of the examples in this book. In it you will find:
- data - in this folder there is an inbox folder with a single file message1.xml
- src - source folder of examples from this chapter
- pom.xml - a file with information for building examples, namely the Maven POM (Project Object Model) XML file
NOTEMaven 3.5.0 was used to write this book. Other versions of Maven may not work exactly as shown here.
The content of the POM file is shown in the listing below.
Listing 1.3 The Maven POM file required to use the Camel core library 1 <project xmlns="http://maven.apache.org/POM/4.0.0" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 4 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 <parent> 7 <groupId>com.camelinaction</groupId> 8 <artifactId>chapter1</artifactId> 9 <version>2.0.0</version> 10 </parent> 11 12 <artifactId>chapter1-file-copy</artifactId> 13 <name>Camel in Action 2 :: Chapter 1 :: File Copy Example</name> 14 15 <dependencies> 16 <dependency> 17 <groupId>org.apache.camel</groupId> 18 <artifactId>camel-core</artifactId> 19 </dependency> 20 <dependency> 21 <groupId>org.slf4j</groupId> 22 <artifactId>slf4j-log4j12</artifactId> 23 </dependency> 24 </dependencies> 25 </project>
Maven is a separate big topic, and here we will touch it by the very minimum only so that you can work with examples.
The Maven POM file in Listing 1.3 is probably one of the shortest POM files in your life, because it uses almost all of the default POM settings. In addition to them, some settings are set in the parent (parent) POM. Perhaps the most important part here is a dependency on the Camel library. Using this POM, Maven does the following:
- Creates a search path based on groupId, artifactId and version. For the version element, the camel-version property is defined, defined in the POM referenced by the parent element, and eventually equal to 2.20.1. The dependency type is not set, so it is assumed that this is a JAR file. The search path thus becomes org / apache / camel / camel-core / 2.20.1 / camel-core-2.20.1.jar.
- Since Listing 1.3 does not contain any instructions on where to look for Camel dependencies, Maven searches them in its central repository at .
- By combining the search path and URL of the repository, Maven tries to download the file .
- The JAR file is saved to the local Maven cache, which is usually located in the home directory .m2 / repository. In Linux / macOS, this is the ~ / .m2 / repository folder, in Windows it is C: \ Users \ [Username] \. M2 \ repository.
- When you run the application code from Listing 1.2, the Camel JAR file is added to the classpath.
To run the example from listing 1.2, go to the chapter1 / file-copy folder and run the following command:
1 mvn compile exec: java
As a result, Maven will compile the source in the src directory and run the FileCopierWithCamel class with the camel-core JAR file in the classpath.
NOTEAll the examples in this book are designed to be connected to the Internet, since Apache Maven will actively download JAR dependencies. The total size of the libraries downloaded from the execution of all the examples from this book will be several hundred megabytes.
Run the Maven command from the folder1 / file-copy folder and after it is finished, go to the data / outbox folder to make sure that the file is copied. Congratulations, you have just completed your first example. Yes, it is very simple, but now you know how to run almost all the other examples from this book.

</ end of translation>