Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beans not found in registry when Camel load routes from XML file

I have a issue in camel, loading routes from XML at run time. I searched for my problem and found related discussion at : http://grokbase.com/p/camel/users/117w8m6rbm/injecting-data-to-routes-loaded-from-xml-file.

My issue is little bit differen. i want to run that route which is given below.

<routes xmlns="http://camel.apache.org/schema/spring">
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:camel="http://camel.apache.org/schema/spring"
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.xsd        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd" >
<bean id="myDb" class="com.mongodb.MongoClient">
<constructor-arg name="host" value="localhost" />
    <constructor-arg name="port" value="27017" />
</bean>
<bean id="mongodb" class="org.apache.camel.component.mongodb.MongoDbComponent"></bean>
</beans>
<route id="_route1">
        <description>here is a sample which subscribe data( proper json object) from mqtt topic  from broker and push into Mongo Db</description>
        <from id="_from1" uri="paho:iot/test/#?brokerUrl=tcp://localhost:1883"/>
        <to id="_to2" uri="mongodb:myDb?database=Volt&amp;collection=dummyData&amp;operation=insert"/>
</route>
</routes>

it gives me exception :

14:14:58.249 INFO  c.v.integration.route.CustomRouter - Exception {}
org.apache.camel.FailedToCreateRouteException: Failed to create route _route1: Route(_route1)[[From[paho:iot/test/#?brokerUrl=tcp://localho... because of No bean could be found in the registry for: myDb of type: com.mongodb.MongoClient
    at org.apache.camel.impl.RouteService.warmUp(RouteService.java:143) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.impl.DefaultCamelContext.doWarmUpRoutes(DefaultCamelContext.java:3619) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.impl.DefaultCamelContext.safelyStartRouteServices(DefaultCamelContext.java:3526) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRoutes(DefaultCamelContext.java:3312) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3166) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:183) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:2961) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:2957) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:2980) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:2957) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:2924) ~[camel-core-2.18.3.jar:2.18.3]
    at com.volt.integration.route.CustomRouter.process(CustomRouter.java:36) ~[classes/:na]
    at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63) [camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) [camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:542) [camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) [camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:120) [camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:83) [camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) [camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:460) [camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:227) [camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:191) [camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:175) [camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:102) [camel-core-2.18.3.jar:2.18.3]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_121]
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [na:1.8.0_121]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_121]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_121]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_121]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_121]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]
Caused by: org.apache.camel.NoSuchBeanException: No bean could be found in the registry for: myDb of type: com.mongodb.MongoClient
    at org.apache.camel.util.CamelContextHelper.mandatoryLookup(CamelContextHelper.java:159) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.component.mongodb.MongoDbEndpoint.doStart(MongoDbEndpoint.java:288) ~[camel-mongodb-2.18.3.jar:2.18.3]
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:75) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.processor.SendProcessor.doStart(SendProcessor.java:236) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:75) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:60) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:104) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:90) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.processor.DelegateAsyncProcessor.doStart(DelegateAsyncProcessor.java:80) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:75) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:60) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:104) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:90) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.processor.RedeliveryErrorHandler.doStart(RedeliveryErrorHandler.java:1448) [camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.support.ChildServiceSupport.start(ChildServiceSupport.java:44) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.support.ChildServiceSupport.start(ChildServiceSupport.java:31) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:75) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:60) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:104) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:90) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.processor.interceptor.DefaultChannel.doStart(DefaultChannel.java:156) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:75) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:60) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:104) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startServices(ServiceHelper.java:90) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.processor.DelegateAsyncProcessor.doStart(DelegateAsyncProcessor.java:80) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:75) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.impl.RouteService.startChildService(RouteService.java:348) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.impl.RouteService.doWarmUp(RouteService.java:191) ~[camel-core-2.18.3.jar:2.18.3]
    at org.apache.camel.impl.RouteService.warmUp(RouteService.java:141) ~[camel-core-2.18.3.jar:2.18.3]
    ... 31 common frames omitted

how can i put the bean in registry at run time ? or how can i referencing the bean which is required for mongodb(client) ? or is there other way to run this route ? i am unable to run this route at run time. kindly suggest me a solution. thanks in Advance .

like image 799
Khalid Shah Avatar asked May 12 '17 06:05

Khalid Shah


2 Answers

I defined beans in different XML file and route in different XML file. my spring XML file is

<?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.xsd">

    <bean id="voltMongo" class="com.mongodb.MongoClient">
            <constructor-arg name="host" value="localhost" />
        <constructor-arg name="port" value="27017" />
    </bean>
</beans>

and my route xml file is

<routes xmlns="http://camel.apache.org/schema/spring">
<route id="_route1">
            <description>here is a sample which processes the subscribe the mqtt topic  from broker and push into Mongo Db</description>
            <from id="_from1" uri="paho:iot/test/#?brokerUrl=tcp://localhost:1883"/>
        <convertBodyTo type="java.lang.String"/>
            <to id="_to2" uri="mongodb:voltMongo?database=Volt&amp;collection=dummyData&amp;operation=insert"/>
</route>
</routes>

and in java code i load FileSystemXmlApplicationContext from spring xml, get bean definations from that context and store them into jndi registry and then it pass to the camel context and then start the camel route. now it running perfect. here is my java code

CamelContext context= null;
ApplicationContext applicationContext =new FileSystemXmlApplicationContext("springconfig.xml");
JndiRegistry registry=null;
if (applicationContext != null) {
    String[] beanNames=applicationContext.getBeanDefinitionNames();
    if (beanNames != null) {
      Map<String,String> enviroment= new HashMap<String,String>();
      enviroment.put("java.naming.factory.initial", "org.apache.camel.util.jndi.CamelInitialContextFactory");
      registry= new JndiRegistry(enviroment);
      for (String name : beanNames) {
            registry.bind(name,applicationContext.getBean(name));
      }
    }
}
context= new DefaultCamelContext(registry);
InputStream is = new FileInputStream(file);
RoutesDefinition routes = context.loadRoutesDefinition(is);
context.addRouteDefinitions(routes.getRoutes());
context.start();
like image 193
Khalid Shah Avatar answered Sep 25 '22 20:09

Khalid Shah


You cannot put <bean> inside the <routes> element, as the former is Spring Framework only, and the latter is Camel only. Put your beans in the spring xml files instead, eg the ones that starts with <beans>.

like image 32
Claus Ibsen Avatar answered Sep 24 '22 20:09

Claus Ibsen