You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Amit <am...@gmail.com> on 2014/10/03 15:50:17 UTC
Dynamically added route at runtime does not have access to Camel
context features like intercepter.
Dynamically added route at runtime does not have access to Camel context
features like intercepter.
In below example, We have created two route. The "Route2" We have defined in
applicationContext.xml file and Route1 i added at runtime. When we call
"route2" Interceptor invoke but when we call Route1 interceptor does not
invoke. We am not worry about just interceptor but other features (tag)
defined in <camelContext> are not access though routes added at runtime. I
have attached the maven Unit test project with this post, so you can easily
reproduce the issue. Let us know if you need more info.
applicationContext.xml
=======================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext xmlns="http://camel.apache.org/schema/spring"
id="test-context" autoStartup="false">
<interceptFrom>
<setBody>
<constant>***** Only Route2 is intercepted.</constant>
</setBody>
<to uri="stream:out" />
</interceptFrom>
<route xmlns="http://camel.apache.org/schema/spring" trace="true">
<from uri="direct:route2" />
<setBody><constant>Hi from Route2 (configured inside the camel
context)</constant></setBody>
<to uri="stream:out" />
</route>
</camelContext>
</beans>
=================================== dynamicRoutesInCamel.zip
<http://camel.465427.n5.nabble.com/file/n5757339/dynamicRoutesInCamel.zip>
package com.test.dynamicRoutesInCamel;
import java.io.StringReader;
import java.util.Collections;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.spring.SpringCamelContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
static ApplicationContext appCtx;
static SpringCamelContext myCtx;
static ProducerTemplate template;
static String route1Str = null;
//static String route2Str = null;
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
@Override
protected void setUp() throws Exception {
// TODO Auto-generated method stub
super.setUp();
route1Str="<route id=\"helloWorld\"
xmlns=\"http://camel.apache.org/schema/spring\" trace=\"true\">"+
"<from uri=\"direct:route1\" />"+
"<setBody><constant>Hello from Route1 (configured outside the camel
context)</constant></setBody>"+
"<to uri=\"stream:out\"/>"+
"</route>";
//<to uri="bean:helloWorld1?method=speak" />
}
@Override
protected void tearDown() throws Exception {
// TODO Auto-generated method stub
super.tearDown();
}
/**
* Rigourous Test :-)
*/
public void testApp() throws Exception
{
String route1ResultActual, route2ResultActual= null;
//assertTrue( true );
RouteDefinition route1 = getRouteDefinition(route1Str);
appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
//myCtx = new SpringCamelContext(appCtx);
myCtx = appCtx.getBean(SpringCamelContext.class);
ProducerTemplate template = myCtx.createProducerTemplate();
Thread.sleep(1000);
myCtx.addRouteDefinitions(Collections.singletonList(route1));
myCtx.start();
try {
route1ResultActual= template.requestBody("direct:route1", "Hello",
String.class);
route2ResultActual= template.requestBody("direct:route2", "Hi",
String.class);
//System.out.println("Route1 result =
\n"+route1ResultActual+"\n\n");
//System.out.println("Route2 result =
\n"+route2ResultActual+"\n\n");
} catch(Exception e) {
//e.printStackTrace(System.out);
System.out.println("Route1 Exception:" + e);
}
}
@SuppressWarnings("restriction")
static RouteDefinition getRouteDefinition(String xmlRouteDef) throws
JAXBException {
RouteDefinition route = null;
JAXBContext context = JAXBContext.newInstance(
"org.apache.camel:org.apache.camel.model:org.apache.camel.model.config:org.apache.camel.model.dataformat:org.apache.camel.model.language:org.apache.camel.model.loadbalancer");
Unmarshaller unmarshaller = context.createUnmarshaller();
Object value = unmarshaller.unmarshal(new StringReader(xmlRouteDef));
if (value instanceof RouteDefinition) {
route = (RouteDefinition)value;
} else {
System.out.println("Failed to unmarshall route definition for
xmlRouteDef: "+xmlRouteDef);
}
return route;
}
}
dynamicRoutesInCamel.zip
<http://camel.465427.n5.nabble.com/file/n5757339/dynamicRoutesInCamel.zip>
--
View this message in context: http://camel.465427.n5.nabble.com/Dynamically-added-route-at-runtime-does-not-have-access-to-Camel-context-features-like-intercepter-tp5757339.html
Sent from the Camel - Users mailing list archive at Nabble.com.