The Routing Slip EIP (Enterprise Integration Pattern) is a powerful tool that enables us to route a message through a series of processors based on a predetermined set of rules. The routing slip is essentially a list of destinations that a message must pass through before reaching its final destination. In this tutorial, we will learn how to use the Routing Slip EIP in Apache Camel.
The EIP Routing Slip directs a message through various processing steps in a sequential manner. The order of these steps is not predetermined during the design phase and differs for each message.
For instance, a security check pipeline for a credit card enterprise may consist of distinct processing steps depending on the circumstances. Standard security checks may suffice for a purchase made within the same country, whereas an overseas purchase would require supplementary endpoints to verify if the purchase is legitimate and not fraudulent.
Example 1: using an Header to determine the routing
In this Camel Route, a header field (the slip) contains one or more endpoints required in the processing steps. At run time, Apache Camel reads this header and constructs the pipeline.
import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.ProducerTemplate; public class MyCamelExample { public static void main(String[] args) throws Exception { CamelContext context = new DefaultCamelContext(); context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .process(new Processor() { public void process(Exchange exchange) throws Exception { exchange.getIn().setHeader("myHeader", "direct:route2"); } }) .routingSlip(header("myHeader")); from("direct:route2") .log("Called route2!"); } }); context.start(); ProducerTemplate template = context.createProducerTemplate(); template.sendBody("direct:start", "Hello, World!"); Thread.sleep(1000); context.stop(); } }
By starting the Camel Route you will see that, by sending a text body to the direct:start endpoint, the Routing Slip will use the Header “myHeader” to determine which is the next step.
[.sample.RoutingSlipDemo.main()] DefaultCamelContext INFO Route: route1 started and consuming from: direct://start [.sample.RoutingSlipDemo.main()] DefaultCamelContext INFO Route: route2 started and consuming from: direct://route2 [.sample.RoutingSlipDemo.main()] DefaultCamelContext INFO Total 2 routes, of which 2 are started. [.sample.RoutingSlipDemo.main()] DefaultCamelContext INFO Apache Camel 2.19.1 (CamelContext: camel-1) started in 0.211 seconds [.sample.RoutingSlipDemo.main()] route2 INFO Called route2!
Example 2: Using a Bean to create the list of destinations
In the second example we will use a Java bean to calculate the sequence of endpoints and adds the result to the myHeader header:
import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.ProducerTemplate; public class MyCamelExample { public static void main(String[] args) throws Exception { CamelContext context = new DefaultCamelContext(); context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .bean(MyBeanImplementation.class, "calculateSequence") .routingSlip(header("myHeader")); from("direct:route2") .log("Called route2!"); } }); context.start(); ProducerTemplate template = context.createProducerTemplate(); template.sendBody("direct:start", "Hello, World!"); Thread.sleep(1000); context.stop(); } public static class MyBeanImplementation { public void calculateSequence(Exchange exchange) { exchange.getIn().setHeader("myHeader", "direct:route2"); } } }
In this example, we define a MyBeanImplementation class with a calculateSequence method. This method takes an Exchange as an argument and sets the myHeader header to the value direct:route2. In our route, we use the bean method to call this method and add the result to the myHeader header. The routing slip then uses this header to determine the next route to call.
Found the article helpful? if so please follow us on Socials