Apache Camel is the most commonly used application versatile open-source integration framework that easily integrates various systems producing or consuming data. So, I came up with this comprehensive Apache Camel Tutorial to briefly understand Apache Camel, EIPs, DSLs, etc.
These days, Data sharing between companies has increased enormously. Numerous integration of applications has increased as well. The user interface uses various technologies, data formats, and protocols. Yet, integrating such applications is modelled in a customized way, concerning efficiency and support by automatic tests.
For such standardized integration, Enterprise Integration Patterns (EIP) is used to document, implement, and define integration issues. Apache Camel came into existence to implement EIPs and provide customized, domain-specific language (DSL) to integrate applications easily.
Apache Camel Tutorial - Table of content
- What is Apache Camel
- Features of Apache Camel
- Apache Camel Architecture
- Core components of Apache Camel
- Enterprise Integration Pattern
- Camel context
- Domain-Specific language
- How to create a new project
Do you want to become an Apache Camel Developer and build your career in this domain? Then enrol in "Apache Camel Training" this course will help you to achieve excellence in this domain.
What is Apache Camel?
Apache Camel is an open-source integration framework depending on Enterprise Integration Patterns. It allows users to define mediation rules and routing in several specific languages, such as Java-based Spring, Blueprint XML, or Fluent API configuration files.
Apache Camel executes EIPs and provides a standardized, internal Domain-specific language (DSL) to integrate applications. Hence, the framework can be deployed with several standalone Java applications and servers such as Tomcat, Wildfly, or fully-fledged enterprise service.
Features of Apache Camel
The features of Apache Camel are as follows:
Camel offers an extensive library of more than 80 components, and these components allow Camel to connect with transports, data formats, and use APIs.
It has a modular architecture that enables the component to load into Camel.
Camel supports the POJO model and allows users to plugin Javabeans at several points.
It makes testing of asynchronous and distributed systems using messaging.
Camel uses an easy and intuitive URI configuration to configure endpoints directly in routes.
It has an in-built type-converter mechanism that transfers more than 150 converters. So, there is no need to configure the type-converter from byte arrays.
It provides a Test kit that makes it easy to test the camel applications. The kit contains test-specific components that can help to test the real endpoints.
Apache Camel Architecture
The Apache Camel architecture includes three main functions as Components, Processor, Integration Engine, and Router. The Camel core includes 13 essential components and 80+ components of minor components. It helps in maintaining low dependency where it can be deployed.
The components offer an Endpoint interface to the eternal world. The Processor is used to mediate and manipulate messages between Endpoints.
Processor and Endpoints are wired with each other in the Integration Engine and router using DSLs. EIPs are executed in this module. When wiring, use the filters to filter messages depending on user requirements.
Core components of Apache Camel
Apache Camel includes various in-built components. The following are the few important components of Camel core.
- Bean: The Bean component binds beans to Camel message exchanges. The URI format is bean: beanName, where beanID can be a string used to track the bean in Registry. The Bean Endpoint can be configured using URI syntax.
Control Bus: The Control Bus from the EIP patterns allows the integration system to be monitored and managed within the framework. It offers easy management of Camel applications depending on the Control Bus EIP pattern.
Where command can be any string to identify the type of command to use.
Data Format: The Data format component enables the use of Data Format as a Camel component.
dataformat: name: (marshal/unmarshal)
Where name is the name of the Data Format. It is used for configuring the Data Format in use.
Direct: The Direct component offers the consumer's synchronous invocation in a JVM when the producer sends a message in return.
This endpoint can be used to connect existing routes in the same camel context.
Where someName can be any string to identify the endpoint uniquely.
File: The File component provides access to file systems, allowing files to be processed by any other Camel Components or messages from other components to be saved to disk.
file:directoryName or file://directoryName
Where directoryName represents the underlying file directory.
Log: The Log component logs message transfers to the basic logging method that allows users to configure logging through the following:
Java Util Logging
Where the logging category is the name of the logging category to use.
REST API: A REST API component is used for providing Swagger API of the REST services, which has been defined using the rest-DSL in Camel.
Its endpoint is configured by URI syntax:
Scheduler: The Scheduler component helps to generate message exchanges when a scheduler requires. It is as same as the Timer component, but it offers more functions in terms of scheduling. Also, this component uses JDK ScheduledExecutorService. Then the timer uses a JDK.
Where name is the name of the scheduler that is created and shared over the endpoints.
Validator: The Validation component executes XML validation of the message body by JAXP Validation API, and depending on any supported XML languages, that defaults to the XML Schema.
Where someLocalOrRemoteResource is URL to the local resource on the classpath or to a remote resource on the file system that includes the XSD to validate.
XSLT: The XSLT component allows you to process a message using an XSLT template. This can be ideal when using Templating to generate a response for requests.
The URI format includes templateName that can be
Subscribe to our youtube channel to get new updates..!
The complete URL of the remote template.
The classpath-local URI of the template to invoke.
Enterprise Integration Pattern
Enterprise Integration Patterns (EIP)s can be used to break the integration issues into smaller pieces and create them using standardized graphics. Even a non-technical person can understand the models easily. With the help of EIPs, Apache Camel reduces the gap between implementation and modelling. There is a relationship between Apache Camels EIP and the DSL models.
CamelContext offers access to all other services in Apache Camel. The CamelContext is the Apache Camels runtime system that connects various concepts such as endpoints, components, or routes.
The Registry module is a JNDI registry that holds the name of several Java beans by default in the application. The Type converters include several loaded type converters that convert the input from one format to another. The Components consist of the components used by the application. Data formats include loaded data formats.
The default CamelContext offers a concrete implementation of CamelContext.The runtime system includes JVM, OSGi container, JEE application server (e.g., IBM WebSphere AS), and a web container (e.g., Tomcat).
Domain-Specific Language (DSL)
Domain-Specific Languages (DSL)s enables the realization of the complex project using a high abstraction level. Camel offers several DSL models. Scala, Java, and Groovy and use object-oriented concepts provide a particular method for EIPs. Also, Spring XML DSL uses XML configuration and is built on the Spring framework.
Java DSL has IDE support. Scala and Groovy DSLs are as same as Java DSL. Besides, they provide features of JVM languages such as closures or concise code. Spring XML DSL requires XML, and it gives a powerful Spring-based dependency injection technique and abstractions to basic configurations (such as JMS or JDBC connections).
Even any combination can be made. Many developers use Spring XML to configure while routes are noticed in Groovy, Java, or Scala.
How to create a new project?
To build a Camel project, do the following steps:
1. Creating a New Project
To create a new project, specify the following.
Select the default location of the project or specify the directory.
2. Adding Dependencies
Open pom.xml to add the dependencies.
Note: Make sure to use a minimum number of dependencies of our application. As we use more components from the library, it is required to add the respective dependencies in the .XML file.
3. Creating Java DSL
Write the filtering and routing code in Java DSL. Create a new Java class known as DistributeOrderDSL.
We create CamelContext by a default implementation offered as DefaultCamelContext class.
Now, add a route by creating an unknown RouteBuilder.
Override the configure method to add a route from the direct URI DisributeOrderDSL to the system console.
Start the context
Add the code for creating the direct URI- DistributeOrderDSL.
Now, start the processing.
Java DSL code is completed, add the order.xml file to the project, and it is good to go for testing.
Run the application; you will get the following output.
Ensure that only orders are listed. If you want to store this results to a local file, then comment the stream. out and uncomment the configure method by the following line.
This is how we create a new Maven project.
We recreate the application using Spring. To create Camel routing in XML instead of DSL, do the following steps.
1. Creating a New Project
Create a new project and specify the following.
Select the default location or specify the directory.
2. Adding Dependencies
With the core dependencies used in the earlier application, add a few more dependencies to use Spring. Open pom.xml to add more dependencies.
3. Creating Java DSL for Spring
Create a new Java class known as DistributeOrderXML and add the code given below.
We create an example of ApplicationContext that is the central interface in the Spring application. Now specify the XML file name that includes routing and filtering details.
Create CamelContext defining the ApplicationContext created.
Start the CamelContext using the start method. We define the Endpoint for loading the order.xml file and start processing.
4. Creating Application Context
Add a new XML file to the project and call for SpringRouteContext.xml. Use the code given below.
Here, we define the XPath query. Note that we now select all orders.
The output Endpoints are numerous. The first endpoint defines the order folder, and the second endpoint represents the console.
Now, run the application.
5. Test Results
After running the application, you will get the following output on the screen.
Check the order folder in the path specified. You will find the newly created file that includes the above XML code.
Apache Camel offers a framework to implement EIPs to make it easy for integrating the projects. It also supports coding in Domain-specific Languages (DSL) and even the XML file. Camel supports all the requirements used in Enterprise projects such as scalability, monitoring, transactions, and error handling. The most important advantage is that the same syntax and concepts can be used anytime, even for automatic tests, whatever the technologies have to be integrated.