Apache Camel Slip EIP Pattern | CodeUsingJava








Apache Camel Slip Routing EIP Pattern


We will be using Apache Camel example to implement Routing Slip EIP Pattern In this tutorial, using Camel Example, we will try to understand the patterns of Routing Slip Enterprise Integration.
slip-eip

Table Of Contents :


Technology Stack

We will be making use of-
  • Java 1.8
  • Apache Camel 3.0.0-M2
  • Maven

The following will be our pom file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.codeusingjava</groupId>
	<artifactId>camel-routing-slip-eip</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>3.0.0-RC1</version>
		</dependency>
	</dependencies>

</project>

Next we build a Camel Routes class. The EIP pattern routing slip allows you to route a message through a series of processing steps in which the sequence of steps is not known at the time of design and can vary for each message. In the example below we set the routing slip in the following sequence to conduct the stream
myroute3->myroute2->myroute1


package com.codeusingjava.routebuilder;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;

public class MyRouteBuilder extends RouteBuilder {
	
	@Override
	public void configure() throws Exception {
		from("file:C:/inputFolder?noop=true").split().tokenize("\n").process(new Processor() {
			public void process(Exchange exchange) {
				String body = exchange.getIn().getBody().toString();
				String response;
				if (body.contains("codeusingjava")) {
					// the following routes will be called sequentially if our text file contains "codeusingjava"
					response = "direct:routeOne,direct:routetwo,direct:routeThree";
				} else
					// the following routes will be called if our text file dosen't contains "codeusingjava"
					response = "direct:routeThree,direct:routetwo,direct:routeOne";
					//set the route slip message in the header
				exchange.getIn().setHeader("myRoutingSlipHeader", response);
			}
		}).routingSlip(header("myRoutingSlipHeader"));

		from("direct:routeOne").process(new Processor() {
			public void process(Exchange exchange) {
				String body = exchange.getIn().getBody().toString();
				body = body + " in slip-eip-route-one";
				System.out.println(body);
				exchange.getOut().setBody(body);
			}
		});

		from("direct:routetwo").process(new Processor() {
			public void process(Exchange exchange) {
				String body = exchange.getIn().getBody().toString();
				body = body + " in slip-eip-route-two";
				System.out.println(body);
				exchange.getOut().setBody(body);
			}
		});

		from("direct:routeThree").process(new Processor() {
			public void process(Exchange exchange) {
				String body = exchange.getIn().getBody().toString();
				body = body + " in slip-eip-route-three";
				exchange.getOut().setBody(body);
				System.out.println(body);
			}
		});
	}

}

When Camel is started, it creates an object of CamelContext containing the definition of the starting route. Below we create the default camel context and load the SimpleRouteBuilder routes

package com.codeusingjava.main;

import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;

import com.codeusingjava.routebuilder.MyRouteBuilder;

public class SlipMain {

	public static void main(String[] args) {

		MyRouteBuilder routeBuilder = new MyRouteBuilder();
        CamelContext ctx = new DefaultCamelContext();
        try {
            ctx.addRoutes(routeBuilder);
            ctx.start();
            Thread.sleep(5 * 60 * 1000);
            ctx.stop();
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }

}


Below is the output if our text file contains "codeusingjava"

Apache Camel Routing Slip EIP Pattern

Below is the output if our text file dosen't contains "codeusingjava"

nothaving-text

Download the code of origin-

Download Apache Camel slip EIP pattern-
Apache Camel slip EIP pattern