Apache Camel And Its Components

Reading Time: 3 minutes

What is Apache Camel

Apache Camel is an open source integration framework designed to make integrating systems simple and easy. It allows end users to integrate various systems using the same API, providing support for multiple protocols and data types, while being extensible and allowing the introduction of custom protocols.

 Domain-Specific Language

Routes and routing engine are the central part of Camel. Routes contain the flow and logic of integration between different systems.

In order to define routes more easy and clean, Camel offers several different domain-specific languages (DSL) for programming languages like Java or Groovy. On the other hand, it also provides defining routes in XML with Spring DSL.

Using either Java DSL or Spring DSL is mostly user preference, as most of the features are available in both.Java DSL offers a bit more features which are not supported in Spring DSL. However, Spring DSL is sometimes more beneficial as XML can be changed without the need to recompile the code.

Four Approaches to Application Integration

There are four application integration approaches with each approach addressing some of the integration guidelines better than others. The four integration approaches include File Transfer, Shared Database, Messaging, and Remote Procedure Invocation. Here is a brief explanation of each one:

  • File Transfer: One application produces files of shared data for others to consume, and vice versa.
  • Shared Database: Applications can store the data they wish to share in a common database.
  • Messaging: An application connects to a shared messaging system, exchanges data, and invokes behaviour using messages.
  • Remote Procedure Invocation: An application exposes its APIs so that they can be invoked remotely by other applications to run its behaviour and exchange data.

Components of Apache Camel

here we will discuss a few important components of Apache Camel from the camel-core module.

Bean

The Bean component binds beans to Camel message exchanges. The URI to create an Endpoint is specified as bean:beanID, where beanID is the name of the bean as specified in the Registry.

JndiContext jndiContext = new JndiContext();
jndiContext.bind("MilkOrder", new MilkOrderProcessor());
CamelContext camelContext = new DefaultCamelContext(jndiContext);

camelContext.addRoutes(new RouteBuilder() {
   public void configure() {
      from("direct:bigBasket")
         .to("bean:MilkOrder?method=placeOrder");
   }
});

Direct

You must have noticed the use of Direct in our previous examples. To send an order to an oil vendor, we used direct:oil in the Endpoint specification. The use of Direct component allows you to synchronously invoke an endpoint. The following two code snippets from our previous examples illustrate the use of Direct −

.when(header("order").isEqualTo("oil"))
.to("direct:oil")

And,

from("direct:DistributeOrderDSL")
.process(myProcessor);

File

The File component provides access to the file system on your machine. Using this component, you will be able to save messages from other components to a local disk. In addition, it allows other Camel components to process the local files. You may use either file:directoryName[?options] or file://directoryName[?options] as a URI format while using the File component. You have earlier seen the use of this component −

from ("file:/order").to("jms:orderQueue");

Note that the File component by default takes the directory name. Therefore, the contents of the order directory will be taken as input contents. To specify a particular file in the order directory, you will use the following statement −

from ("file:/order?fileName = order.xml").to("jms:orderQueue");

Log

The Log component allows you to log messages to the underlying logging mechanism. Camel uses Simple Logging Facade for Java (SLF4J) as an abstraction to various logging frameworks. You may use java.util.logging, logback, log4j for logging. This code snippet illustrates the use of the Log component −

from("direct:DistributeOrderDSL")
.to("bean:MilkOrder?method = placeOrder")
.to("log:com.example.com?level = INFO&showBody = true");

SEDA

The SEDA component allows you to asynchronously call another endpoint in the same CamelContext. If you want to call across CamelContext instances, you need to use VM component. The use of SEDA is illustrated here −

from("direct:DistributeOrderDSL")
// send it to the seda queue that is async
.to("seda:nextOrder")

In this route, we will simply route the orders to nextOrder asynchronous queue. A client who has subscribed to this queue will pick up the messages from this queue.

Timer

The Timer component is used for sending out messages at regular intervals and can thus be very useful while testing Camel applications. The code snippet here fires a test message to the console every two seconds −

from("timer://testTimer?period = 2000")
.setBody()
.simple("This is a test message ${header.timer}")
.to("stream:out");

Conclusion:

Apache Camel is an excellent framework for integrating applications with different technologies and protocols. The best part — besides exceptional support for various technologies — is that we can always use the same concepts. in this tutorial we learn how we can deal with different Apache camel components.

refrences:- https://blog.knoldus.com/apache-camel-an-integration-framework

Written by 

I'm a Software Consultant at Knoldus Inc. I have done Post Graduation from Quantum University Roorkee. I have knowledge of various programming languages. I'm passionate about Java development and curious to learn Java Technologies. I'm always ready to learn new technologies and my hobbies are cricket and action movies.

Leave a Reply