spring - camel - 如何使用 java 配置拥有多个上下文

标签 spring apache-camel

我试图在不同的 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 上下文中有一些路由而在其他 camel 上下文中有其他路由?

最佳答案

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

您可以在新的 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

关于spring - camel - 如何使用 java 配置拥有多个上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26150197/

相关文章:

java - 手动触发一个@Scheduled 方法

java - 如何从构造函数访问@Value注释变量?

java - 将 DAO 注入(inject) CXF 服务

java - Camel 测试模板使用与我发送的交换器不同的交换器

java - Camel netty定时器必须指定错误

java - Java 上的 LRU 项目

Spring-boot LDAP 自定义 UserDetails

java - 如何判断camel中的交换Body属于哪种类型?

java - 组织.apache.camel.NoSuchBeanException : No bean could be found in the registry for:

java - 测试 Camel REST DSL 消费者模板