Introduction:
In this section, we will explore conditional routing in Apache Camel using the Choice and When processors. Conditional routing allows you to direct messages along different paths based on specific conditions or predicates. By leveraging these processors, you can implement sophisticated routing logic and handle various scenarios in your integration solutions. Let’s dive into conditional routing with the Choice and When processors in Camel, accompanied by code samples.

3.2.2.1 Basic Conditional Routing with the Choice Processor:
The Choice processor provides a powerful mechanism for conditional routing in Camel routes. It evaluates a series of conditions and directs messages to different endpoints based on the satisfied conditions. Let’s consider a simple example:

Java
from("direct:input")
.choice()
.when(header("type").isEqualTo("A"))
.to("direct:routeA")
.when(header("type").isEqualTo("B"))
.to("direct:routeB")
.otherwise()
.to("direct:defaultRoute");

In this example, the choice statement sets up the conditional routing. The when(header("type").isEqualTo("A")) condition checks if the message header “type” is equal to “A”. If the condition is true, the message is routed to “direct:routeA”. Similarly, the when(header("type").isEqualTo("B")) condition checks for the “type” header equaling “B” and routes the message to “direct:routeB”. If none of the conditions match, the message is directed to the “direct:defaultRoute” endpoint.

3.2.2.2 Advanced Conditional Routing with the When Processor:
The When processor provides further flexibility in conditional routing by allowing you to evaluate complex predicates. It enables you to define conditions based on message content, headers, or any other expression supported by Camel. Let’s consider an example:

Java
from("direct:input")
.process(exchange -> {
// Set a custom property based on some logic
boolean isSpecial = checkIfSpecial(exchange.getIn().getBody());

// Set the custom property as a message header
exchange.getIn().setHeader("isSpecial", isSpecial);
})
.choice()
.when(header("isSpecial").isEqualTo(true))
.to("direct:specialRoute")
.otherwise()
.to("direct:normalRoute");

In this example, the process statement includes custom logic to determine if a message is special, and the result is stored as a header named “isSpecial”. The when(header("isSpecial").isEqualTo(true)) condition checks if the “isSpecial” header is true, and if so, routes the message to “direct:specialRoute”. Otherwise, the message is directed to “direct:normalRoute”.

3.2.2.3 Combining Multiple Conditions:
You can also combine multiple conditions within the Choice processor using logical operators such as and, or, and not. This allows for more complex routing scenarios. Let’s see an example:

Java
from("direct:input")
.choice()
.when(header("type").isEqualTo("A").and(body().contains("important")))
.to("direct:routeA")
.when(header("type").isEqualTo("B").or(header("priority").isEqualTo("high")))
.to("direct:routeB")
.otherwise()
.to("direct:defaultRoute");

In this example, the first condition checks if the message header “type” is equal to “A” and if the message body contains the word “important”. If true, the message is routed to “direct:route

A”. The second condition checks if the header “type” is equal to “B” or if the header “priority” is equal to “high”, and if true, the message is routed to “direct:routeB”. If none of the conditions are satisfied, the message is directed to “direct:defaultRoute”.

Conclusion:
In this section, we explored conditional routing in Apache Camel using the Choice and When processors. These processors provide powerful mechanisms for directing messages along different paths based on conditions or predicates. By utilizing the Choice processor, you can set up basic conditional routing scenarios, while the When processor allows for more advanced and flexible routing logic. Understanding how to leverage these processors will enable you to handle various routing scenarios and ensure efficient message flow in your integration solutions. In the next section, we will dive into message transformation and enrichment within Camel routes.