Apache Camel

How To Use Choice and When in Apache Camel?

Rohan Ravindra Kadam
Javarevisited
Published in
4 min readMay 11, 2022

--

Hello🖐 My Self Rohan Ravindra Kadam, Full Stack Developer

Welcome, Developer Community !! In the article, we try to understand the exception handling using Apache Camel. Before directly jumping into the article lets us try to understand certain question or queries related to the topic. We may have questions like What is Apache Camel? Why used Apache Camel? How To Use Choice and When in Apache Camel?

credits Rohan Ravindra Kadam How to use Choice and When in Apache Camel?

Hey Community, let us, deep-dive, into our Questions and find the answer for them.

⚡ What is Apache Camel?

Apache Camel ™ is a versatile open-source integration framework based on known Enterprise Integration Patterns.

Camel empowers you to define routing and mediation rules in various domain-specific languages (DSL, such as Java, XML, Groovy, Kotlin, and YAML). This means you get smart completion of routing rules in your IDE, whether in a Java or XML editor.

Apache Camel uses URIs to work directly with any kind of transport or messaging models such as HTTP, ActiveMQ, JMS, JBI, SCA, MINA or CXF, as well as pluggable Components and Data Format options. Apache Camel is a small library with minimal dependencies for easy embedding in any Java application. Apache Camel lets you work with the same API regardless of which kind of transport is used — so learn the API once, and you can interact with all the Components provided out-of-box.

Apache Camel supports Bean Binding and seamless integration with popular frameworks such as CDI and Spring. Camel also has extensive support for unit testing your routes.

⚡ Why used Apache Camel?

Let us try to list down certain use cases for Apache Camel

  1. Provides Support to Enterprise Integration Patterns(EIP)
  2. Runs Everywhere:- Apache Camel is standalone and can be embedded as a library within Spring Boot, Quarkus, Application Servers, and in the clouds. Camel subprojects focus on making your work easy.
  3. Packed With Components:- Packed with several hundred components used to access databases, message queues, APIs or anything under the sun. Helping you integrate with everything.
  4. Support Over 50 Data Formats:- Camel supports around 50 data formats, allowing to translate messages in multiple formats and with support from industry-standard formats from finance, telco, healthcare, and more.

⚡ How To Use Choice and When in Apache Camel?

Hey 🖐Community, in the prior section of the article section we tried to cover the Why & What about the Apache Camel? In section, we will try to understand the Implementation of the same.

Before jumping directly into the implementation do not forget to check out How to write the first route using Apache Camel? from the below link

Step 1: How to create Project Using Spring Initializr?

Step 2: What are the dependencies to include in the project?

Credit: Rohan Ravindra Kadam Dependencies

Step 3: How to use choice and when in apache camel?

Apache Camel contains a powerful feature called content-based routers. This allows you to process the message differently relying on the content.

They are quite similar to the if/else statement in Java. Regardless, in Camel, the equivalent words are when and otherwise.

package com.example.apachecameltest.route;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class ChoiceWhenRoute extends RouteBuilder {
@Override
public void configure() throws Exception {

from("direct:startWhenChoiceRoute")
.routeId("direct:startWhenChoiceRoute")
.log( "${body}")
.choice()
.when(body().isNull())
.log("Message body is empty.")
.log("${body}")
.end();
}
}

Step 4: Testing our camel Routes?

To test our camel route will be using the controller endpoint

package com.example.apachecameltest;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.ExchangeBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controler {

@Autowired
private ProducerTemplate producerTemplate;

@Autowired
private CamelContext camelContext;

@GetMapping
public void function() {
final Exchange
requestExchange = ExchangeBuilder
.anExchange(camelContext)
.build();

producerTemplate
.send("direct:startWhenChoiceRoute",requestExchange);

}
}

Hey 🖐 To Understand more about ProducerTemplate & Camel Context Checkout the below link

Step 5:Output of Camel Route?

 direct:startWhenChoiceRoute              : null
direct:startWhenChoiceRoute : Message body is empty.
direct:startWhenChoiceRoute : null

🖐Conclusion:-

In the article, we tried to implement choice and when in apache camel. We also tried to understand a bit about the apache camel as well. Please do share and like 💕if you find the article useful. Follow me on medium Rohan Ravindra Kadam and on Twitter at rohankadam25

⚡Bibliography

Thank You, Viewers — Rohan Ravindra Kadam

--

--