CamelContext加载,但路由定义为空

rhfm7lfc  于 2022-11-07  发布在  Apache
关注(0)|答案(3)|浏览(204)

我尝试手动加载CamelContextapplicationContext.xml文件,看起来像这样:

<beans:beans xmlns="http://www.springframework.org/schema/integration"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xsi:schemaLocation="                                                                                                                                                                                                                                                                                              
                                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd                                                                                                                                                                                      
                                 http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd                                                                                                                                                                    
                                 http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
    <route>
      <from uri="direct:a"/>
      <to uri="direct:b"/>
    </route>

    <route>
      <from uri="direct:b"/>
      <to uri="direct:c"/>
    </route>

    <route>
      <from uri="direct:c"/>
      <to uri="mock:direct:c"/>
    </route>
  </camelContext>                                                                                                                                                                                                                                                                                                              

</beans:beans>

在加载时我使用了以下代码片段(groovy代码):

def xmlBeans = applicationContext.getBeanFactory()
new XmlBeanDefinitionReader(xmlBeans).loadBeanDefinitions('test/integration/resources/applicationContext.xml')
def camelContext = applicationContext.getBean("camelContext")
camelContext.start();
println "Route definitions: " + camelContext.routeDefinitions
println "Routes: " + camelContext.routes

一切都加载正常,但routesrouteDefinitions为空;(
输出为:

Routes definitions: []
Routes: []

我做错了什么?或者我错过了什么?

更新1

经过研究,我发现了以下问题:如果我修改XML文件,将嵌套标记更改为无效值(例如,将<from>替换为<from1>),我会立即收到错误。但是,如果我将端点URI更改为无效值(例如,将mock:direct:c替换为mock_XXX:direct:c),则不会引发错误。
我相信Spring的模式验证可以运行,然而,路径的 Camel 验证却不能。

更新2

当我将camelContext.start()替换为applicationContext.start()时,没有任何变化。

更新3

在一些建议之后,我对代码进行了如下修改:

final Main main = new Main();
    main.setApplicationContextUri("test/integration/resources/applicationContext.xml");
    main.start();

    def ctx = main.applicationContext

    ctx.beanDefinitionNames.each { name ->
      if(name == 'camelContext'){
        def cctx = ctx.getBean(name);
        println "\nRD:" + cctx.routeDefinitions // <!-- this prints routes
      }
    }

    ctx.beanDefinitionNames.each { name ->                                                                                                                                                                                                                                                                                 
      def beanDef = ctx.beanFactory.getBeanDefinition(name);
      applicationContext.registerBeanDefinition(name, beanDef)
    }

    // TODO remove this snippets                                                                                                                                                                                                                                                                                           
    def camelContext = applicationContext.getBean("camelContext")
    applicationContext.start();
    camelContext.start();
    println "Route definitions: " + camelContext.routeDefinitions //<!-- this does not
    println "Routes: " + camelContext.routes //<!-- this does not

第一个输出按预期打印路线。第二个输出则不按预期打印路线。如下所示:

RD:[Route(a)[[From[direct:a]] -> [To[direct:b]]], Route(b)[[From[direct:b]] -> [To[direct:c]]], Route(c)[[From[direct:c]] -> [To[mock:direct:c]]]]
Route definitions: []
Routes: []

似乎路由定义不知何故没有初始化或加载或任何因此它只是空的。

w1jd8yoj

w1jd8yoj1#

你需要启动CamelContext来加载它的路由。它看起来就像你刚刚加载了spring bean定义一样。这不会导致CamelContext触发它的路由加载等等。所以这是预期的,它是空的。

rur96b6h

rur96b6h2#

实际上,我从未尝试过通过Groovy启动Camel Spring上下文。

final Main main = new Main();
 main.setApplicationContextUri("test/integration/resources/applicationContext.xml"); // use wildcards if needed
 main.start();

也许这是你解决问题的另一种选择。

hvvq6cgz

hvvq6cgz3#

虽然这并不能直接解决您的问题,但是camel调试输出通常会有所帮助。

logging.level.org.apache.camel=DEBUG

在您的application.properties档案中。An official alternative is

camel.springboot.tracing = true

相关问题