317.3. 如何从其他 XML 文件导入路由
可作为 Camel 2.3 提供
当使用 Xml 配置 在 Camel 中定义路由时,您可能需要在其他 XML 文件中定义一些路由。例如,您可能有多个路由,如果某些路由位于单独的 XML 文件中,则可能有助于维护应用程序。您可能还想将通用和可重复使用的路由存储在其他 XML 文件中,您可以在需要时轻松导入。
在 Camel 2.3 中,现在可以定义您在新 < ;
外部的路由。
routeContext/> tag 中进行的 <camel
Context/>
注意: 当您使用 <routeContext> 时,它们会被分隔,且无法重复使用现有的 <onException>、<intercept>、<dataFormats> 和类似在 <camelContext> 中定义的跨域功能。换句话说,<routeContext> 当前是隔离的。这可能会改变 Camel 3.x。
例如,我们可以有一个名为 myCoolRoutes.xml
的文件,其中包含几个路由,如下所示:
myCoolRoutes.xml
然后,在包含 CamelContext 的 XML 文件中,您可以使用 Spring 导入 myCoolRoute.xml
文件。
然后,在 <camelContext/&
gt; 内,您可以通过其 ID 引用 <routeContext
/>,如下所示:
另请注意,您可以混合和匹配 CamelContext 中的路由,并在 RouteContext 中进行外部化。
您可以像这样一样拥有数量的 <routeContextRef
/>。
可重复使用路由
< routeContext/&
gt; 中定义的路由可以被多个 < camelContext/>
重复使用。但是,它只有重复使用的定义。在运行时,每个 CamelContext 将根据定义创建自己的路由实例。
317.3.1. 测试时间线索. 复制链接链接已复制到粘贴板!
在测试时,通常需要能够选择性地将匹配路由从不能供测试场景使用或有用的路由。例如,您可能在 'org.example.routes' 软件包中有一个 spring 上下文文件 routes-context.xml 和三个 Route builder RouteA、RouteB 和 RouteC。packageScan 定义将发现所有这些路由中的所有三个路由,并初始化它们。
称 RouteC 不适用于我们的测试方案,并在测试过程中生成大量 noise。在这个特定测试中可以排除此路由会很方便。SpringTestSupport 类已被修改以允许执行此操作。它提供了两个方法(excludedRoute 和 excludeRoutes),可覆盖这些方法,以排除单个类或一组类。
为了通过 spring 初始化 camelContext 初始化 hook,排除了 MyExcludedRouteBuilder.class,我们需要截获 spring 上下文创建。当覆盖 createApplicationContext 以创建 spring 上下文时,我们会调用 getRouteExcludingApplicationContext ()方法提供处理排除的特殊父 spring 上下文。
@Override protected AbstractXmlApplicationContext createApplicationContext() { return new ClassPathXmlApplicationContext(new String[] {"routes-context.xml"}, getRouteExcludingApplicationContext()); }
@Override
protected AbstractXmlApplicationContext createApplicationContext() {
return new ClassPathXmlApplicationContext(new String[] {"routes-context.xml"}, getRouteExcludingApplicationContext());
}
现在初始化中排除 RouteC。同样,在仅测试 RouteC 的另一个测试中,我们可以通过覆盖来排除 RouteB 和 RouteA
@Override protected Class[] excludeRoutes() { return new Class[]{RouteA.class, RouteB.class}; }
@Override
protected Class[] excludeRoutes() {
return new Class[]{RouteA.class, RouteB.class};
}