Felpfe Inc.
Search
Close this search box.
call 24/7

+484 237-1364‬

Search
Close this search box.

Conditional Routing with Choice and When Processors

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.

About Author
Ozzie Feliciano CTO @ Felpfe Inc.

Ozzie Feliciano is a highly experienced technologist with a remarkable twenty-three years of expertise in the technology industry.

kafka-logo-tall-apache-kafka-fel
Stream Dream: Diving into Kafka Streams
In “Stream Dream: Diving into Kafka Streams,”...
ksql
Talking in Streams: KSQL for the SQL Lovers
“Talking in Streams: KSQL for the SQL Lovers”...
spring_cloud
Stream Symphony: Real-time Wizardry with Spring Cloud Stream Orchestration
Description: The blog post, “Stream Symphony:...
1_GVb-mYlEyq_L35dg7TEN2w
Kafka Chronicles: Saga of Resilient Microservices Communication with Spring Cloud Stream
“Kafka Chronicles: Saga of Resilient Microservices...
kafka-logo-tall-apache-kafka-fel
Tackling Security in Kafka: A Comprehensive Guide on Authentication and Authorization
As the usage of Apache Kafka continues to grow in organizations...
1 2 3 58
90's, 2000's and Today's Hits
Decades of Hits, One Station

Listen to the greatest hits of the 90s, 2000s and Today. Now on TuneIn. Listen while you code.