Getting started with Apache Camel using Java

This example shows how routers will be controlled based on the given condition and can be called based on the input.
The example is interactive - it reads input from the console, and then transforms the input to upper case and call appropriate router based on the channel/payload/input.

The vm: component provides asynchronous SEDA behavior, exchanging messages on a BlockingQueue and invoking consumers in a separate thread pool.
This component differs from the Seda component in that VM supports communication across CamelContext instances - so you can use this mechanism to communicate across web applications (provided that camel-core.jar is on the system/boot classpath).
An exactly identical VM endpoint URI must be used for both the producer and the consumer endpoint. Otherwise, Camel will create a second VM endpoint despite that the queueName portion of the URI is identical. For example:
  from("stream:in?promptMessage=Enter something: ").to("vm:channel?concurrentConsumers=5"); 
  from("vm:channel?concurrentConsumers=5").to("file://output");
Notice that we have to use the full URI, including options in both the producer and consumer. In Camel 2.4 this has been fixed so that only the queue name must match. Using the queue name bar, we could rewrite the previous exmple as follows:

  from("direct:foo").to("vm:bar");
  from("vm:bar?concurrentConsumers=5").to("file://output");

Example for Apache Camel Multi Routers using VM component

Router in Xml Configuration:(applicationContext-camel.xml)
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:camel="http://camel.apache.org/schema/spring"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd          
		http://camel.apache.org/schema/spring 
		http://camel.apache.org/schema/spring/camel-spring.xsd">

	<camelContext xmlns="http://camel.apache.org/schema/spring">
		<route>
			<!-- read input from the console using the stream component -->
			<from uri="stream:in?promptMessage=Enter something: " />
			<!-- transform the input to upper case using the simple language -->
			<!--
				you can also use other languages such as groovy, ognl, mvel,
				javascript etc.
			-->
			<transform>
				<simple>${body.toUpperCase()}</simple>
			</transform>

			<choice>
				<when>
					<simple>${body} contains 'PHONE'</simple>
					<to uri="vm:phone" />
				</when>
				<when>
					<simple>${body} contains 'EMAIL'</simple>
					<to uri="vm:email" />
				</when>
				<otherwise>
					<to uri="vm:letter" />
				</otherwise>
			</choice>
			<!-- and then print to the console -->
			<to uri="stream:out" />
		</route>

		<route id="phone">
			<!-- incoming requests from the servlet is routed -->
			<from uri="vm:phone" />
			<transform>
				<constant>Phone Karo</constant>
			</transform>
			<to uri="stream:out" />
		</route>
		<route id="email">
			<!-- incoming requests from the servlet is routed -->
			<from uri="vm:email" />
			<transform>
				<constant>Send EMAIL</constant>
			</transform>
			<to uri="stream:out" />
		</route>
		<route id="letter">
			<!-- incoming requests from the servlet is routed -->
			<from uri="vm:letter" />
			<transform>
				<constant>Post Letter</constant>
			</transform>
			<to uri="stream:out" />
		</route>
	</camelContext>
</beans>

Maven entries for pom.xml:In this example we integrate with the console using the Stream component. For this required camel-stream jar to support cosole example
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>2.13.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-spring</artifactId>
			<version>2.13.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-stream</artifactId>
			<version>2.13.0</version>
		</dependency>


Below is my application execution
package com.javavillage.camel.proj;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
    	AbstractApplicationContext ctx = 
				new ClassPathXmlApplicationContext("applicationContext-camel.xml");
    	ctx.start();
    	System.out.println("Entered>>>>>");
    	ctx.stop();
    }
}

Apache Camel Console Structure

Apache Camel Console Structure

Execute Apache Camel Multi Router Example:

Execute Apache Camel Multi Router Example

Note: Vm component is async call, so will not wait for execution to be completed.