在运行时加载apache camel路由

nom7f22z  于 11个月前  发布在  Apache
关注(0)|答案(1)|浏览(132)

在项目中,我一直在运行时动态加载ApacheCamel路由。当前环境包括Spring 2.7.4, Apache Camel 3.18.0, and JDK 17

private void fromFile(final Path filepath) throws Exception {
    final byte[] routeDefinitionBytes = Files.readAllBytes(filepath);
    final Resource resource = ResourceHelper.fromBytes(filepath.toString(), routeDefinitionBytes);
    final ExtendedCamelContext extendedCamelContext = camelContext.adapt(ExtendedCamelContext.class);
    extendedCamelContext.getRoutesLoader().loadRoutes(resource);
}

字符串
现在迁移项目以使用Apache Camel 4.0.0-RC1Spring Boot 3.1.1,并继续使用JDK 17。所面临的问题是adapt()方法在Apache Camel的新版本中不可用,从而导致现有代码无法正常工作。
我正在寻找一个可行的解决方案,它允许我继续从文件中阅读路由定义,并在Apache Camel 4.0.0-RC1的运行时将它们注入到Apache Camel上下文中。任何建议将不胜感激。

r7knjye2

r7knjye21#

对于这样的需求,您可以依赖实用程序方法PluginHelper#getRoutesLoader(CamelContext),但在幕后,新的逻辑是在camel上下文上调用getCamelContextExtension()以获取ExtendedCamelContext,然后通过方法ExtendedCamelContext#getContextPlugin(Class)获取您想要使用的插件。
所以你的代码应该是:

private void fromFile(final Path filepath) throws Exception {
    final byte[] routeDefinitionBytes = Files.readAllBytes(filepath);
    final Resource resource = ResourceHelper.fromBytes(
        filepath.toString(), routeDefinitionBytes
    );
    PluginHelper.getRoutesLoader(camelContext).loadRoutes(resource);
}

字符串

相关问题