从外部源加载 Apache Camel 路由

问题描述 投票:0回答:1

我正在尝试构建一个 Java 应用程序,从外部源(最好是数据库)加载 YAML DSL Camel 路由。这个想法是提供在外部编辑这些路由、刷新 CamelContext 以及在更新其中一个路由时重新加载路由的可能性。 我注意到当前的 Apache Camel 4 文档位于 https://camel.apache.org/docs/ 和数十个示例 没有展示如何将 YAML 定义的路由加载到 CamelContext。 我找到的唯一解决方案,这个https://stackoverflow.com/a/67758794/8740422,看起来有点像黑客,创建了某种假的

Resource

Apache Camel 不应该这样使用吗?还是我在文档中遗漏了一些东西?

java apache-camel
1个回答
0
投票

所以是的,这似乎是在java应用程序中加载从yaml到CamelContext的路由的唯一方法。

  1. 确保您已将此依赖项添加到您的

    pom.xml

     <dependency>
         <groupId>org.apache.camel</groupId>
         <artifactId>camel-yaml-dsl</artifactId>
         <version>4.3.0</version> <!-- your camel version -->
     </dependency>
    

2.使用

PluginHelper.getRoutesLoader(camelContext).loadRoutes(resource)
将路由加载到camel上下文。

  1. 通过java doc 资源是“描述资源,例如文件或类路径资源”。要从内存中创建资源,您可以使用
    ResourceHelper.fromBytes("fake-resource.yaml", byteArray)
    。这将用 InputStream 包装字节数组并从中构建资源。

完整示例:

byte[] targetArray = //load your yaml route from external source;
Resource resource = ResourceHelper.fromBytes("test.yaml", targetArray); 
PluginHelper.getRoutesLoader(camelContext).loadRoutes(resource);
© www.soinside.com 2019 - 2024. All rights reserved.