You are on page 1of 7

HardWay

Using DSL in Camel with Processor::


HelloBean.java
package com; public class HelloBean { public String hello(String name) { System.out.println("Calling HelloBean with " + name); return "Hello " + name; } }

InvokeWithProcessorRoute.java
package com; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; public class InvokeWithProcessorRoute extends RouteBuilder { @Override public void configure() throws Exception { from("direct:hello") .process(new Processor() { public void process(Exchange exchange) throws Exception { // extract the name parameter from the Camel message which we want to use // when invoking the bean String name = exchange.getIn().getBody(String.class); // now create an instance of the bean HelloBean hello = new HelloBean(); // and invoke it with the name parameter String answer = hello.hello(name); // store the reply from the bean on the OUT message exchange.getOut().setBody(answer);

} }); } }

InvokeWithProcessorTest.java
package test; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.Test; import com.InvokeWithProcessorRoute; public class InvokeWithProcessorTest extends CamelTestSupport { @Test public void testHelloBean() throws Exception { String reply = template.requestBody("direct:hello", "GovindBirajdar ", String.class); assertEquals("GovindBirajdar", reply); } @Override protected RouteBuilder createRouteBuilder() throws Exception { return new InvokeWithProcessorRoute(); } }

HardWay: Using SpringBeans in Camel with Processor::


HelloBean.java
package com; public class HelloBean { public String hello(String name) { System.out.println("Calling HelloBean with " + name); return "Hello " + name; } }

InvokeWithProcessorSpringRoute.java
package com; import import import import org.apache.camel.Exchange; org.apache.camel.Processor; org.apache.camel.builder.RouteBuilder; org.springframework.beans.factory.annotation.Autowired;

public class InvokeWithProcessorSpringRoute extends RouteBuilder { @Autowired private HelloBean hello; @Override public void configure() throws Exception { from("direct:hello") .process(new Processor() { public void process(Exchange exchange) throws Exception { // extract the name parameter from the Camel message which we want to use // when invoking the bean

String name = exchange.getIn().getBody(String.class); // invoke the bean which should have been injected by Spring String answer = hello.hello(name); // store the reply from the bean on the OUT message exchange.getOut().setBody(answer); } }); } }

InvokeWithProcessorSpringTest.java
package test; import org.apache.camel.test.junit4.CamelSpringTestSupport; import org.junit.Test; import org.springframework.context.support.AbstractXmlApplicationContex t; import org.springframework.context.support.ClassPathXmlApplicationConte xt; public class InvokeWithProcessorSpringTest extends CamelSpringTestSupport { @Override protected AbstractXmlApplicationContext createApplicationContext() { return new ClassPathXmlApplicationContext("com/InvokeWithProcessor.xml"); } @Test public void testHelloBean() throws Exception { String reply = template.requestBody("direct:hello", "GovindSpring", String.class); assertEquals("GovindSpring", reply); } }

XML FILE:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context2.5.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd "> <!-- to use spring @Autowired for dependency injection --> <context:annotation-config/> <bean id="helloBean" class="com.HelloBean"/> <bean id="route" class="com.InvokeWithProcessorSpringRoute"/> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <routeBuilder ref="route"/> </camelContext> </beans>

Easiest Way

HelloBean.java
package com; public class HelloBean { public String hello(String name) { System.out.println("Calling HelloBean with " + name); return "Hello " + name; } }

InvokeWithProcessorSpringTest.java
package EasiestWay; import org.apache.camel.test.junit4.CamelSpringTestSupport; import org.junit.Test; import org.springframework.context.support.AbstractXmlApplicationContex t; import org.springframework.context.support.ClassPathXmlApplicationConte xt; public class InvokeWithProcessorSpringTest extends CamelSpringTestSupport { @Override protected AbstractXmlApplicationContext createApplicationContext() { return new ClassPathXmlApplicationContext("EasiestWay/InvokeWithProcessor.x ml"); } @Test public void testHelloBean() throws Exception { String reply = template.requestBody("direct:hello", "GovindSpring", String.class); assertEquals("GovindSpring", reply); } }

XML FILE:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context2.5.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd "> <!-- to use spring @Autowired for dependency injection --> <context:annotation-config/> <bean id="helloBean" class="EasiestWay.HelloBean"/> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:hello"></from> <bean ref="helloBean" method="hello"/> </route> </camelContext> </beans>

You might also like