Exchange, Components, and Endpoints in Apache Camel

Reading Time: 4 minutes

What is Exchange?

A Message is part of an Exchange, which is a larger Camel object. In the Camel documentation, the term Exchange is frequently used. An Exchange is a communication or conversation that is currently occurring within your Camel route.

At the commencement of a journey, an Exchange is formed (from your first step). As a message goes through the steps of the route, it is modified.

The output message from the first step of the route becomes the second step’s input message. This method is then repeated until the route is completed.
The current request and answer Messages are stored in the Exchange object at any point along a route.

What’s in the exchange?

When dealing with Camel, you may want to alter the Exchange object directly in addition to writing code in routes. A custom Java class can be used to do this.

Here’s an example of how you might interact with these objects in Java code. Here, the exchange object is a reference to the current Camel Exchange.

The exchange has a body and a header value, which we can fetch from Camel’s Exchange object:

Message message = exchange.getMessage(); 
String header = message.getHeader("MyHeader", String.class); 
String body = message.getBody(String.class);

Components and Endpoint

Camel’s endpoints are the foundation of your routes. They’re the parts of your pipeline that receive and deliver messages, and they’re supported by components that have the logic to connect to a variety of other services.

Camel has components for things like:

1. file handling

2. messaging

3. web services

4. cloud apps

5. special utilities

What is a Camel component?

Apache Camel is an open-source, lightweight integration library that enables intelligent routing, message transformation, and protocol mediation in your applications. This freshly revised Refcard introduces Apache Camel, describes its benefits, and shows how to use its most important features. Learn how to use JMS and ActiveMQ, as well as the Bean Component, Log and Metrics Components, and Camel’s REST DSL.

Finding components

The first stop should be the Camel website, which has a page that details all of the available components. When you’re trying to figure out if Camel contains a component you require, this is a nice place to start.

How to use a component

When you decide to use a component in Camel, there are two steps you must take:

1. In your Maven pom.xml, add the relevant dependency. In Camel, you’ll need to add the component as a dependency for most components. The documentation page for the component will inform you the Maven dependencies you’ll need to install.

Add the camel-csv dependency to your Maven POM, for example, to use the CSV component, which allows Camel to work with CSV files.

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-csv</artifactId>
</dependency>

2. Add a from step (if you wish to consume from a component endpoint) or a to step to use the component in your routes (if you want to send a message to the endpoint).

Set the URI attribute to your component’s Endpoint URI on the step itself, which instructs the component to establish a new endpoint with the desired configuration.

Defining an endpoint URI

The following is an example of a component endpoint URI:

scheme:mainpart?myParam=XXX&anotherParam=YYY

• The first scheme specifies the component we want to use, for example, file:

• The main part section usually specifies the location, destination, or main argument that the component should use; for example, when using the File component, the main part specifies the folder where we want to read or write a file.

• The optionN parameters at the end are used to give the component more options.

Using the File component as an example

This is the endpoint URI for the File component’s configuration:

file:myfiles/input?fileName=MYFILE.TXT

file: specifies that we want to use Camel’s File component

myfiles/input is the folder that we want to read from OR write to

fileName=MYFILE.TXT is an option we’ve added to the File component, to tell it to consume only files named MYFILE.TXT.

The endpoint can now be used in a route. We’re using two different File endpoints in the same route in the example route below:

Route with two File endpoints (Java DSL)

from("file:myfiles/input?fileName=MYFILE.TXT")
.log("I'm doing something")
.to("file:documents/done");

And the same route in the XML DSL:

<route>
<from uri="file:myfiles/input?fileName=MYFILE.TXT"/>
<to uri="file:documents/done"/>
</route>

Endpoint API

To create producers or consumers for an endpoint, you can use the Java API methods listed below:

1. Create a Producer for sending message exchanges to the endpoint with createProducer().

2. When creating a Consumer, createConsumer() uses the Event-Driven Consumer pattern to consume message exchanges from the endpoint via a Processor.

3. createPollingConsumer() is a PollingConsumer that implements the Polling Consumer pattern for consuming message exchanges from the endpoint.

Conclusion

Apache Component References provides numerous references for messaging, sending data, notifications, and other services that may not only resolve easy messaging and data transfer but also provide data security. for more information visit: https://www.tutorialspoint.com/apache_camel/apache_camel_components.htm

Written by 

Mohd Uzair is a Software intern at Knoldus. He is passionate about java programming. He is recognized as a good team player, a dedicated and responsible professional, and a technology enthusiast. He is a quick learner & curious to learn new technologies. His hobbies include watching movies, surfing youtube, playing video games.

Leave a Reply