Templates by BIGtheme NET

Content-Based Router Using Camel

1) Content-Based Router (CBR) is a message router that routes a message to a destination
based on its content.
2) The content could be a message header, the payload data type, part of the payload itself—pretty much anything in the message exchange.
3) The CBR routes messages based on their content. In this case, the filename extension (as a message header) is used to determine which queue to route to.
9

Consider, in Coming orders can be in two formats.
1) Xml files 2) CSV files

4) Now the routing point should have a predicate to check either the order is xml file or csv file.

from("jms:incomingOrders").choice()
                          .when(predicate).to("jms:xmlOrders")
                          .when(predicate).to("jms:csvOrders");

5) A predicate in Camel is a simple interface that only has a matches method.

public interface Predicate {
       boolean matches(Exchange exchange);
}

6) This predicate, we can use simple condition or regular expression as well.

from("jms:incomingOrders")
.choice()
.when(header("CamelFileName").endsWith(".xml")) // condition as predicate
.to("jms:xmlOrders")
.when(header("CamelFileName").regex("^.*(csv|csl)$")) // regular expression as predicate
.to("jms:csvOrders");

Example :
In this example, In coming orders can be any file type. we are considering if the file type
is xml or csv is valid order. other wise bad orders.
If Xml file type moving to xml processing directory or csv type moving to csv processing directory.
Other wise bad order directory.

Source Code :
ContentBasesRouters.java

package com.venkatjavasource.camel;

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;

public class ContentBasesRouters {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		CamelContext context = new DefaultCamelContext();
		context.addRoutes(new RouteBuilder() {
			public void configure() {
				from("file:/venkatjavasource/camel/from?noop=false").
				process(new Processor() {
					public void process(Exchange exchange) throws Exception {
							System.out.println("Downloaded File is : " + exchange.getIn().getHeader("CamelFileName"));
						}
					}).choice()
						.when(header("CamelFileName").endsWith(".xml"))
						.to("file:/venkatjavasource/camel/to/xml")
						.when(header("CamelFileName").regex("^.*(csv|csl)$"))
						.to("file:/venkatjavasource/camel/to/csv").otherwise()
						.to("file:/venkatjavasource/camel/to/bad_orders");
			}
		});

		context.start();
		Thread.sleep(10000);
		context.stop();
	}
}

Demo :
From directory having 5 files,
10

And to directory is empty,
11

Run the application, ContentBasesRouters.java

Right click “Run As” -> “Java Application”.
Out Put:
Downloaded File is : File1.txt
Downloaded File is : File2.txt
Downloaded File is : Order_bad.bad
Downloaded File is : Order_csv.csv
Downloaded File is : Order_xml.xml

Then xml files should move to C:venkatjavasourcecameltoxml
12
Csv files should move to C:venkatjavasourcecameltocsv
13
Bad files, other than xml and csv moved to C:venkatjavasourcecameltobad_orders
14