Templates by BIGtheme NET

Routing with Camel

Message Router :
1) A message router consumes messages from an input channel and, depending on
a set of conditions, sends the message to one of a set of output channels.
4
2) input and output queues are unaware of the conditions in between them. The conditional
logic is decoupled from the message consumer and producer.
3) A customer has two ways of submitting orders to the Rider Auto Parts order-handling system:
Either by uploading the raw order file to an FTP server or
By submitting an order through the Rider Auto Parts web store.
All orders are eventually sent via JMS for processing at Rider Auto Parts.
5

Here, first assignment, you’ll need to implement the FTP module in the Rider order frontend system.
Implementing the FTP module will involve the following steps.
1) Polling the FTP server and downloading new orders
2) Converting the order files to JMS messages
3) Sending the messages to the JMS incomingOrders queue

To complete steps 1 and 3, you’ll need to understand how to communicate over FTP
and JMS using Camel’s endpoints.

end point :
We required two end point configurations in camel context.xml file, one for FTP and another is for JMS.
1) A Camel endpoint URI consists of three parts: a scheme, a context path, and a list of options.
6

HOW TO CONFIGURE CAMEL TO USE A JMS PROVIDER :
The output of the FTP consumer is fed into the input of the JMS producer.
The payload conversion from file to JMS message is done automatically.

from("file:/venkatjavasource/camel/from?noop=false")

Two files File 1.xml and File 2.txt are in
/venkatjavasource/camel/from directory.
3

Once the ftp read two files from the directory, then these two files will be input to active mq.
These two files will be removed from the original directory and back up will be taken in the same directory at the same time.
7

We can add processor in the mix, the output of the FTP consumer is now fed into the processor, and then the output of the processor is fed into the JMS producer.
8
Steps to be followed:
1) Create an ActiveMQConnectionFactory for localhost.

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");

The vm://localhost URI means that you should connect to an embedded broker named “localhost” running
inside the current JVM.
2) Add JMS component to CamelContext.xml file with the .

CamelContext context = new DefaultCamelContext();
context.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));

applicationContext.xml

FtpToJMSExample.java

package com.venkatjavasource.camel;

import javax.jms.ConnectionFactory;

import org.apache.activemq.ActiveMQConnectionFactory;
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.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;

public class FtpToJMSExample {
	public static void main(String args[]) throws Exception {
		CamelContext context = new DefaultCamelContext();
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( "vm://localhost");
		context.addComponent("activemq", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
		
		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("We just downloaded: " + exchange.getIn().getHeader("CamelFileName"));
						}
					}).
					to( "activemq:queue:jms.queue");
			}
		});		
		
		context.start();
		Thread.sleep(10000);
		context.stop();
	}
}

3) Run the FtpToJMSExample.java, right click select “Run As” -> “Java Application”.
Out Put :
We just downloaded: File 1.txt
We just downloaded: File 2.txt