Introduction to Routing with Apache Camel

Reading Time: 4 minutes

Introduction

One of the main features of Apache Camel is routing. Routing occurs in many parts of daily life. For Example, When we send a letter to someone, it can go through various cities or states before reaching to its final destination. And similar to this is the case of sending an email. An Email goes through various networks before reaching its receiver.

In Apache Camel’s case, routing done in EMS(enterprise messaging systems) is the process of taking a message from the input queue to different output queues based on the defined conditions. The conditions set between the input and output queues are unknown to them. The message consumer and producer are kept separate from the conditional logic.

In Apache Camel, routing is more of a general concept as its defined as step by step movement of the message from source to destination. The consumer of the message could be getting the message from an external service, polling for it on a system, or even writing it himself. The message goes through a processing node which can be an EIP, then a processor for processing the message, then an interceptor ( a condition to route the message) and then finally the message gets delivered to the endpoint.

Understanding and Consuming messages and sending then from Endpoints

Consuming messages

One of the things that make Camel so user friendly is the endpoint URI. We may identify the component you wish to utilise and how it is setup using an endpoint URI. We can then choose whether to send messages to or consume messages from the component specified by this URI. Lets see how we can easily configure Camel using the URI notation using the below example.

Camel first searches the component registry for the ftp scheme, which resolves to FtpComponent. The FtpComponent then acts as a factory, generating FtpEndpoint from the context path and options.
FtpComponent logs into the FTP server at rider.com on the default FTP port and alter the directory to orders by the context path of rider.com/orders. Finally, the only options supplied are the username and password for logging onto the FTP server.

Since FtpComponent isnt a part of the core-module of Apache Camel, we need to add it explicitly using the maven dependency

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ftp</artifactId>
<version>2.20.1</version>
</dependency>

Sending message to a JMS endpoint

Java Message Service (JMS) is a Java API that allows you to create, send, receive, and read messages. It also mandates asynchronous messaging and specific characteristics of reliability, such as guaranteed and once-and-only-once delivery. In JMS, message consumers and producers communicate with one another via an intermediary JMS destination. Now this destination where the consumer and the producer communicate, could either be a Queue or be a Topic. Queues are strictly uni-directional which means that messages have only one consumer. On the other hand, a Topics operate on publishing/Subscribing technique. Which means that the message may or may not have multiple consumers.

Since JMS Component isnt a part of the core-module of Apache Camel, we need to add it explicitly using the maven dependency

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
<version>2.20.1</version>
</dependency>

JMS also includes a ConnectionFactory that clients (such as Camel) can use to connect to a JMS provider. Because they manage the communication between a message producer and consumer, JMS providers are commonly referred to as brokers.

Advantages of Using JMS

  • JMS is an asynchronous protocol. Clients do not need to inquire whether they are subscribed to a message.
  • JMS is dependable because it guarantees “message delivery.”

Advantages of defining routes in Camel

  • It is faster and more flexible.
  • visualising, managing, maintaining, debugging, and testing (via mock, etc.) becomes easier.
  • has examples that are simpler to follow
  • support for inline processors.

Conclusion

We covered several crucial elements about message-oriented development with the Apache Camel framework in this blog. It enables developers to integrate several technologies and communicate data with various endpoints such as files, queues, and external services while being simple to use.

Reference links : http://camel.apache.org

Stay tuned for more blogs at : https://blog.knoldus.com/

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading