Camel in questions and answers

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




    Introducing Camel


    Creating complex systems from scratch is a costly and almost always doomed business. It is much more efficient and safer to assemble them as a 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 reliably connected to each other. The same is true for systems integration projects. But if pieces of a real puzzle are created in order to form a single picture, then with IT systems the situation is, as a rule, the opposite. As a developer, you, as a rule, are not very worried about the internal structure and work of the integrated systems; you are much more interested in how to interact with it from the outside. A good integration framework (platform) involves two things: firstly, a simple and convenient abstraction apparatus for describing complex integrable systems, and secondly, a connecting mechanism that allows you 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 the book. At the end of the chapter, we will introduce the basic Camel concepts needed to understand its architecture.

    Ready? Then meet Camel.

    Introducing Camel


    Camel is an application integration platform designed to work productively and with pleasure. The history of Camel began in 2007, and today it is a mature open source project that is distributed under the Apache 2 license and around which a powerful development community has developed.

    The main goal of Camel 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 required tools.

    Camel is part of the Apache project family, gaining its name due to its brevity and ease of remembering. There is a version that the project owes its name to 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. Using the linker, you can set your own routing rules, decide which sources to receive messages from, and determine how to process and send these messages further. Camel integration language allows you to set 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 it 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 a canonical format.

    Fig. 1.1 Camel is the link between unrelated systems.



    Thanks to high-level abstractions, Camel allows you to use the same APIs to interact with different systems using different protocols and data types. APIs for specific protocols and data types are implemented as appropriate components. By default, Camel supports more than 280 protocols and data types, and thanks to an extensible modular architecture it 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 also less gluttonous. 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 call it a 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 rather than an ESB solution.

    If at the mention of ESB complex monster systems immediately come to mind, we are in a hurry to reassure: Camel can be used with the same success in very small things, like microservices or IoT gateways (the Internet of things).

    To understand what Camel is, consider its main functions.

    Why use Camel


    Camel is the practical embodiment of new ideas in the field of integration. Next, we will take a closer look at the features of Camel, but for now, list them:

    • Mediation and Routing Mechanism
    • Extensible Component Library
    • Enterprise Application Integration Templates (EIP)
    • Subject Oriented Language (DSL)
    • Payload Format Independent Router
    • Plug-in Architecture
    • Plain Old Java Object (POJO) Model
    • Ease of configuration
    • Automatic Type Converters
    • Lightweight core suitable for integration into microservices
    • Cloud support
    • Testing Toolkit
    • Active development community

    Now we will analyze this list by points.

    Mediation 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 you have already used or would like to use. Of course, within the framework of this book it is impossible to consider all the components of Camel, but we will discuss the two dozen main ones.

    Fig. 1.2 Ability to connect to anything: Camel supports more than 280 transports, APIs and data formats.



    Enterprise Application Integration Templates (EIP)


    For all the variety of integration issues, many of them, as Gregor Hohpe and Bobby Woolf have noted, have similar solutions. These two released the book “Enterprise Integration Patterns” (translated into Russian - “Enterprise Application Integration Templates”), which should be read by 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 Templates (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, providing appropriate semantics. Camel is heavily based on EIP patterns. However, although the EIP templates describe problems and solutions and also provide a common dictionary, this dictionary is not formalized. Camel fills this gap by providing a language for describing integration solutions. There is almost a one-to-one relationship between the patterns described in the Enterprise Integration Patterns book and the CamelDSL subject language.

    Subject Oriented Language (DSL)


    At the time Camel appeared, its main contribution to integration was the subject-oriented language (DSL). Over time, other integration frameworks followed this example, and now DSL is in Java, XML, and other programming languages. DSL allows the developer to focus on integration tasks without being distracted by the features of the used tool (programming language). Let's look at a couple of DSL examples that use different formats, but are completely equivalent in functionality:

    Java DSL

    1	from("file:data/inbox").to("jms:queue:order");
    

    XML DSL

    1	
    2	  
    3	  
    4	

    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 an IDE environment, like autocompletion when entering 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 can use the convenient and familiar features of the IDE.


    The figure shows how the autocomplete function as you type into the Eclipse IDE offers a list of possible DSL expressions.

    Payload Format Independent Router


    Camel can route useful data in any form, not limited to just one normalized format, for example, the XML payload. Therefore, to route the load, it does not need to be converted to the canonical format.

    Plug-in Architecture


    The modular architecture of Camel allows you to load into it any components, both included in the delivery 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 much more.

    Model POJO


    The bean components of Java (or Plain Old Java Objects, POJOs) in Camel are first-class citizens, so they can be used in integration projects almost anywhere, anytime. In many cases, you can even extend Camel's built-in functionality with your code.

    Ease of configuration


    Camel configuration is minimized wherever possible. To define endpoints directly in routes, a simple and understandable URI configuration is used.

    For example, you can configure the Camel route, which will start from the 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 engine with over 350 converters. You no longer need to configure conversion rules, for example, to convert an array of bytes to a string. And even if Camel does not have the converter you need, you can always write it yourself. And the best part is that all this works autonomously, without your participation.

    Camel components also use auto-type conversion, so they can accept most data types and convert them to the type they are able to use. In the Camel community, this feature is among the top favorites, and some even wonder why it is not in Java.

    Lightweight core suitable for integration into microservices


    The Camel kernel really weighs very little: the library is about 4.9 MB in size, and runtime dependencies take up about 1.3 MB. Therefore, Camel can easily be embedded anywhere and deployed anywhere, including stand-alone applications, microservices, web applications, Spring applications, Java EE, OSGi, Spring Boot, WildFly applications, as well as cloud platforms such as AWS, Kubernetes or Cloud Foundry. Camel was originally created not as a server system or ESB solution, but as something that can be embedded in any executable things where Java is.

    Cloud support


    Camel is not only focused on cloud use, 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
    • Hip chat
    • LinkedIn
    • Salesforce
    • Twitter
    • and many others

    Testing Toolkit


    Camel includes a package for testing applications. This package is also actively used to test Camel itself and has more than 18 thousand unit tests. The package contains special components that allow, for example, to simulate real endpoints or 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. The presence of such a community is an important factor in choosing open source software for building your own applications. If the project is inactive, you can hardly count on community support and remain face to face with your problems. And vice versa, 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 kit and consider a few examples.

    Getting started


    In this section, we will tell you how to get the Camel distribution, and briefly describe its contents. Then we show how to run 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 Camel release.

    In this book, we use Camel 2.20.1. To get this version, use the appropriate link on the download page. At the end of this page there are links to two distributions: a zip file for Windows and tar.gz for macOS / Linux. Download the desired file and unzip it to your local drive.

    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 understandable structure, namely:

    • doc - Camel manual folder in HTML format. This is just a copy of the Camel website at the time of release. It will come in handy in those moments when you do not have access to the site or if this book is not at hand.
    • examples - folder with 97 Camel examples
    • lib - folder with all Camel libraries. Later we show how to use Maven to easily 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 for third-party dependencies included in the Camel distribution
    • README.txt - a file with a brief introduction to Camel and a list of links that will help beginners get started quickly

    Now let’s see how to follow the Camel examples in this book.

    The first launch of Camel


    So, above, we talked about how to get the Camel distribution, and briefly described its contents. Among other things, there are examples with explanations and instructions that we recommend studying.

    However, in this book we will not use the distribution at all. All the source code examples here use Apache Maven, which automatically downloads all the necessary Camel libraries. Therefore, you will not need to control that they are registered in the classpath.

    All sample code 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, file routing. Suppose you need to read 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 is 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.


    Nowhere is easier, right? Here's how the implementation of this task looks in "pure" Java, without using Camel.

    Listing 1.1 Routing files from one folder to another in pure 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 like a very simple task, but FileCopier already has as many as 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. Complexity is growing right before our eyes.

    Fortunately, the integration tasks, like the one just considered, have already been solved a thousand times. Therefore, you do not need to reinvent the wheel and write code with your hands. Let's see how all this functionality is implemented using the 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 a CamelContext, which subsequently starts and then stops. We also added a sleep method to give the Camel application time to copy files. The main thing to note in Listing 1.2 is the route.

    Routes in Camel are defined in the same order in which they are read. For example, the route from the example above can be read as follows: receive messages from a file (from file) in the data / inbox folder with the noop parameter and send it to a 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 encountered Camel may well understand what this route does. Also, note that if you drop the boilerplate code, then the whole task comes down to two lines of Java code.

    To run this example, you will 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 message1.xml file
    • src is the source folder for the examples in this chapter.
    • pom.xml - a file with information for building examples, namely the Maven POM (Project Object Model) XML file

    NOTE

    When writing this book, Maven 3.5.0 was used. Other versions of Maven may not work exactly as shown here.

    The contents of the POM file are shown in the listing below.

    Listing 1.3. Maven POM file needed to use the main Camel library

    1	
    5	  4.0.0
    6	  
    7	    com.camelinaction     
    8	chapter1          
    9	    2.0.0                   
    10	  
    11	
    12	  chapter1-file-copy
    13	  Camel in Action 2 :: Chapter 1 :: File Copy Example
    14	
    15	  
    16	    
    17	      org.apache.camel     
    18	camel-core     
    19	    
    20	    
    21	      org.slf4j     
    22	slf4j-log4j12     
    23	                                  
    24	  
    25	

    Maven is a separate big topic, and here we will touch on it at the very minimum just 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, since it uses almost all of the default POM settings. In addition to them, some settings are set in the parent POM. Perhaps the most important part here is the dependency on the Camel library. Using this POM, Maven does the following:

    1. Создает search path на основе groupId, artifactId и version. Для элемента version задано свойство camel-version, определенное в POM, на который ссылается элемент parent, и равное в итоге 2.20.1. Тип зависимости не задан, поэтому считается, что это файл JAR. Search path таким образом превращается в org/apache/camel/camel-core/2.20.1/camel-core-2.20.1.jar.
    2. Поскольку листинг 1.3 не содержит никаких указаний, где искать зависимости Camel, Maven ищет их в своем центральном репозитории по адресу.
    3. Объединив search path и URL репозитория, Maven пытается загрузить файл.
    4. JAR-файл сохраняется в локальный кэш Maven, который обычно находится в домашнем каталоге .m2/repository. В Linux/macOS это папка ~/.m2/repository, в Windows – C:\Users\[Username]\.m2\repository.
    5. 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 compiles the source in the src directory and executes the FileCopierWithCamel class with the camel-core JAR file in the classpath.

    NOTE

    All of 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 as a result of all the examples from this book will be several hundred megabytes.

    Run the Maven command from the chapter1 / file-copy folder and after its completion go to the data / outbox folder to make sure that the file was 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 in this book.


    Also popular now: