camel-如何使用javaconfig拥有多个上下文



我试图在不同的camel上下文中对几个camel路由进行分组,以避免组件名称冲突。我知道如何在相同的上下文中配置几个RouteBuilder类,从CamelConfiguration扩展到

   @Configuration
   public class CamelConfig extends CamelConfiguration {
   @Override
   public List<RouteBuilder> routes() {
       // here I create the RouteBuilder List and the return it
   }

但是,如何使用Java配置在一个camel上下文中拥有一些路由,在另一个camelcontext中拥有其他路由呢?

您可以在一个Camel上下文中添加外部XML文件中定义的许多Camel Routes如下例:

您可以在一个新的xml文件(即routes1-config.xml)中创建路由:

<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
   http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
">
  <!-- this is an included XML file where we only the the routeContext -->
  <routeContext id="routes1" xmlns="http://camel.apache.org/schema/spring">
    <!-- we can have a route -->
    <route id="cool">
        <from uri="direct:start"/>
        <to uri="mock:result"/>
    </route>
    <!-- and another route, you can have as many your like -->
    <route id="bar">
        <from uri="direct:bar"/>
        <to uri="mock:bar"/>
    </route>
  </routeContext>
</beans>

然后在主camel xml文件内的上下文中导入并引用它,如下所示:

<!-- import the routes from another XML file -->
<import resource="routes1-config.xml"/>
<camelContext xmlns="http://camel.apache.org/schema/spring">
  <!-- refer to a given route to be used -->
  <routeContextRef ref="routes1"/>
  <!-- we can of course still use routes inside camelContext -->
  <route id="inside">
    <from uri="direct:inside"/>
    <to uri="mock:inside"/>
  </route>
</camelContext>

有关更多详细信息,请查看Apache Camel的官方参考文档http://camel.apache.org/how-do-i-import-routes-from-other-xml-files.html

一个CamelConfiguration类创建一个CamelContext。解决方案是拥有多个这样的子类(或类似的子类)。

您可以使用NMR(标准化消息路由器),http://camel.apache.org/nmr.html并将每个项目骆驼配置中的路由公开为NMR路由,然后使用NMR通过统一的Java方法使用它:routename。

最新更新