【问题标题】:CamelContext loads, but route definitions are emptyCamelContext 加载,但路由定义为空
【发布时间】:2014-03-07 20:11:49
【问题描述】:

我正在尝试从applicationContext.xml 文件手动加载CamelContext,如下所示:

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

加载时我正在使用这个 sn-ps(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 文件并将嵌套标记更改为某个无效值(例如,将 &lt;from&gt; 替换为 &lt;from1&gt;)我会立即收到错误消息。 但是,如果我将端点 URI 更改为某个无效值(例如,mock:direct:c 替换为 mock_XXX:direct:c) - 不会引发错误。

我相信 Spring 的模式验证可以运行,但是路线的骆驼验证不会。

更新 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: []

似乎路由定义不知何故没有初始化或加载,或者其他什么,因此它只是空的。

【问题讨论】:

标签: java spring groovy apache-camel applicationcontext


【解决方案1】:

你需要启动 CamelContext 让它加载它的路由。它出现在您刚刚加载 spring bean 定义时。这不会导致 CamelContext 触发其路由的加载等。因此,预计它是空的。

【讨论】:

  • camelContext.start() 没有帮助。我已经更改了代码 sn-ps,请检查
  • 你需要启动 Spring,例如 applicationContext.start() 也应该启动 Camel。
  • 我相信我的 applicationContext 是在其他地方启动的。但是,尝试了您的建议- 不起作用。我现在将更新我的问题。
  • 我再次更新了我的问题定义。请检查。如您所见,骆驼上下文已启动。 applicationContext 也可以。
【解决方案2】:

实际上,我从未尝试通过 Groovy 启动 Camel Spring 上下文。在 Java 中,我使用org.apache.camel.spring.Main 进行测试:

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

也许这可能是您解决问题的替代方法。

【讨论】:

  • 不,情况并非如此,因为我已经拥有 applicationContext 并且只想将 inject 我的 camelContext 放入其中。
【解决方案3】:

虽然这不能直接解决您的问题,但骆驼调试输出通常会有所帮助。在spring boot中,通过设置来启用它

logging.level.org.apache.camel=DEBUG

在您的 application.properties 文件中。 An official alternative is

camel.springboot.tracing = true

【讨论】:

    猜你喜欢
    • 2020-07-09
    • 2020-08-16
    • 2019-09-23
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多