Hi, Claus Thanks a lot for your information. I guess I got your approach working so I post my code here maybe someone else who has the same situation can save a bit of time. The reason why I want to reload the routes defined in my RouteBuilder is that the configure method will dynamically build the routes based on database records. Basically, I defined 2 camel contexts in spring config file, the 2nd will reload the 1st one based on certain condition.
This is the spring config file: ----------------------------------------- <camel:camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring"> <camel:package>my.camel.routes</camel:package> </camel:camelContext> <camel:camelContext id="camel-reloader" xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="jms:queue:reload_routes"/> <to uri="bean:routesReloader?method=onReload"/> </route> </camel:camelContext> <bean id="routeBuilderReloader" class="my.RoutesReloader"> <property name="camelContext" ref="camel" /> </bean> ---------------------------------- And this is my RoutesReloader class: ---------------------------------- public class RoutesReloader implements ApplicationContextAware { private SpringCamelContext camelContext; private String PACKAGE_TO_RELOAD = "my.camel.routes"; private ApplicationContext applicationContext; public void onReload() throws Exception { if(null != camelContext) { printRoutes(); //stop and destroy the old camelContext camelContext.stop(); camelContext.destroy(); camelContext = null; //create and start a new camel context camelContext = SpringCamelContext.springCamelContext(applicationContext); camelContext.start(); // Re-add all the RouteBuilders under PACKAGE_TO_RELOAD package RouteBuilderFinder rbf = new RouteBuilderFinder((SpringCamelContext) camelContext, new String[]{PACKAGE_TO_RELOAD }, this.getClass().getClassLoader(), null); List<RouteBuilder> builders = new ArrayList<RouteBuilder>(); rbf.appendBuilders(builders); for(RouteBuilder rb : builders) { camelContext.addRoutes(rb); } System.out.println("======= after reload ========"); printRoutes(); } } public void setCamelContext(CamelContext arg0) { camelContext = (SpringCamelContext) arg0; } void printRoutes() { for(Route r:camelContext.getRoutes() ){ System.out.println("route=" + r.getEndpoint().getEndpointUri().toString()); } } @Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { this.applicationContext = arg0; } } ---------------------------- Thanks, John (Zuokun) -- View this message in context: http://www.nabble.com/How-to-programatically-add-remove-camel-route-tp20076542s22882p20095418.html Sent from the Camel - Users mailing list archive at Nabble.com.
