Introduction:
Routing and mediation play a vital role in system integration, enabling the seamless flow of data and messages between various endpoints. In this section, we will explore how Apache Camel simplifies routing and mediation tasks, allowing developers to define flexible integration routes and implement powerful mediation patterns. We will delve into the features and capabilities provided by Apache Camel and provide code samples to illustrate its routing and mediation capabilities.

2.1.1 Message Routing:
Apache Camel provides a rich set of routing capabilities, allowing you to define complex integration routes with ease. Whether you need to route messages based on content, headers, or specific conditions, Apache Camel offers a wide range of routing options and patterns.

Let’s consider an example that showcases message routing in Apache Camel:

Java
from("file:inputFolder")
.choice()
.when(header("fileType").isEqualTo("xml"))
.to("jms:xmlQueue")
.when(header("fileType").isEqualTo("csv"))
.to("jms:csvQueue")
.otherwise()
.to("jms:defaultQueue");

In this example, Apache Camel reads files from an input folder and routes them based on the value of the “fileType” header. If the header value is “xml,” the message is routed to the “xmlQueue” endpoint. If the header value is “csv,” the message is routed to the “csvQueue” endpoint. Otherwise, the message is routed to the “defaultQueue” endpoint. This example illustrates how Apache Camel enables flexible and dynamic message routing in integration solutions.

2.1.2 Content-Based Routing:
Content-based routing is a powerful pattern that allows you to route messages based on their content. Apache Camel provides robust support for content-based routing, enabling you to define complex routing rules based on message content, such as XML elements, JSON properties, or custom expressions.

Let’s consider an example that demonstrates content-based routing in Apache Camel:

Java
from("jms:inputQueue")
.choice()
.when(xpath("/order[@status='pending']"))
.to("jms:pendingQueue")
.when(xpath("/order[@status='approved']"))
.to("jms:approvedQueue")
.otherwise()
.to("jms:defaultQueue");

In this example, Apache Camel consumes messages from the “jms:inputQueue” and applies content-based routing using XPath expressions. Messages that match the XPath expression “/order[@status=’pending’]” are routed to the “pendingQueue,” while messages that match “/order[@status=’approved’]” are routed to the “approvedQueue.” All other messages are routed to the “defaultQueue.” This example highlights how Apache Camel facilitates content-based routing, allowing for precise message routing based on content criteria.

2.1.3 Message Transformation and Mediation:
Apache Camel excels at message transformation and mediation, enabling seamless data transformation and mediation between different systems and formats. It provides a wide range of data transformation capabilities, including content conversion, message enrichment, filtering, and more.

Let’s consider an example that showcases message transformation and mediation in Apache Camel:

Java
from("file:inputFolder")
.convertBodyTo(String.class)
.filter(body().contains("important"))
.enrich("http://api.example.com/data", new DataEnricher())
.to("jms:outputQueue");

In this example, Apache Camel reads files from an input folder and converts the message body to a string using the “convertBodyTo” method. The “filter” method is then used to filter messages that contain the word “important.” The message is enriched by making an

HTTP call to an API endpoint using the “enrich” method. The response from the API is processed by a custom enricher (DataEnricher) to add additional data to the message. Finally, the transformed and enriched message is sent to the “jms:outputQueue.” This example demonstrates how Apache Camel facilitates message transformation and mediation, allowing you to manipulate and enhance messages during the integration process.

Conclusion:
Routing and mediation are crucial aspects of system integration, enabling the seamless flow of data and messages between different endpoints. Apache Camel provides powerful routing capabilities, including message routing based on content and flexible routing patterns. It also excels at message transformation and mediation, allowing for seamless data transformation and enrichment. The code samples provided illustrate how Apache Camel simplifies routing and mediation tasks, empowering developers to build flexible and powerful integration solutions.

In the next section, we will explore the various data integration patterns supported by Apache Camel, enabling you to implement common integration scenarios with ease.