CamelContext加载,但路由定义为空

时间:2014-03-07 20:11:49

标签: java spring groovy apache-camel applicationcontext

我正在尝试从CamelContext文件手动加载applicationContext.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对模式的验证运行,但是,路由的驼峰验证不会。

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

似乎路线定义在某种程度上没有被初始化或加载,或者因此它只是空的。

2 个答案:

答案 0 :(得分:0)

您需要启动CamelContext以使其加载其路由。看起来只是加载spring bean定义。并且这不会导致CamelContext触发其路由的加载等。所以这应该是空的。

答案 1 :(得分:0)

实际上,我从未试图通过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();

也许这可能是您解决问题的另一种选择。