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

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

My problem is a little different. I want to run this 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>

this gives me an 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 a bean in the registry at runtime? or how can I reference the bean that is required for mongodb (client)? or is there another way to start this route? I cannot run this route at runtime. kindly offer me a solution. Thanks at Advance.

+4
2

beans XML XML. my spring 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.xsd">

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

xml

<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>

java- FileSystemXmlApplicationContext spring xml, bean jndi, , . . Java

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();
+2

<bean> <routes>, Spring Framework, - Camel. beans Spring xml , , , <beans>.

+1

Source: https://habr.com/ru/post/1676981/


All Articles