Binding new bean into camelContex register in RouteBuilder
andr2ot Dec 4, 2013 8:22 AMI am trying to use a camel route to build other routes.
Hence I have created two route builders:
- FactoryRouteBuilder which reads configuration from db and creates a set of routes using (DroolsRouteBuilder)
- DroolsRouteBuilder which gets some date from database and then passes the data to drools endpoint
Camel context and FactoryRouteBuilder are declared vi spring configuration file. This approach works fine.
Now the problem is that drools endpoint which I am using DroolsRouteBuilder requires couple additional beans being registered in camel context registry. See line (158)
node = component.getCamelContext().getRegistry().lookup( nodeId, GridNode.class );
I would like to add the required beans to camel registry in the configure method of FactoryRoutesBuilder.
public void configure() throws Exception { //Instantiate beans getCamelContext().getRegistry().bind("customer1Node",node);
However it appears that Registry getCamelContext().getRegistry() is read only registry, so I can lookup a bean but not bind new one. Is there a workaround I could use?
I have seen some other posts with similar problem but none of the solution provided is working for me:
public class FactoryRoutesBuilder extends RouteBuilder { @Override public void configure() throws Exception { List<String> customerList = new ArrayList<>(); // route queries database to retrieve list of customers for (String customerId : customerList) { DroolsRouteBuilder route = new DroolsRouteBuilder(); route.setCustomerId(customerId); getContext().addRoutes(route); } } }
public class DroolsRouteBuilder extends RouteBuilder { private String customerId; @Override public void configure() throws Exception { String routeId = customerId + "DroolsRoute"; String droolsEndpointUrl = "drools:" + routeId + "Node/" + routeId + "Ksession?action=execute"; from("{{droolsRoute.from}}").routeId(routeId) .setBody(MvelExpression.mvel("request.headers.id")) .to("mongodb:mongoDb?database={{db.name}}&collection=person&operation=findById") .convertBodyTo(Person.class) .setHeader("person",MvelExpression.mvel("request.body")) .to(droolsEndpointUrl) .end(); } }
<?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:camel="http://camel.apache.org/schema/spring" xmlns:drools="http://drools.org/schema/drools-spring" xmlns:context="http://www.springframework.org/schema/context" 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 http://drools.org/schema/drools-spring http://drools.org/schema/drools-spring.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:property-placeholder location="classpath*:test.properties" /> <camelContext useMDCLogging="true" xmlns="http://camel.apache.org/schema/spring"> <propertyPlaceholder location="classpath:test.properties" id="properties" /> <routeBuilder ref="factoryRoutesBuilder" /> </camelContext> <context:property-placeholder location="classpath:test.properties" /> <bean id="drools" class="org.drools.camel.component.DroolsComponent" /> <bean id="factoryRoutesBuilder" class="com.drools.routes.FactoryRoutesBuilder" /> </bean