首页 > 解决方案 > 在运行时加载 apache 骆驼路由并添加到现有的 CamelContext

问题描述

我正在尝试在运行时加载 Apache Camel 路由并添加到现有的 CamelContext。我有如下定义的路线,

<?xml version="1.0" encoding="UTF-8" ?>

<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">

    <routeContext id="message" xmlns="http://camel.apache.org/schema/spring">
        <route id="abcRoute" autoStartup="false">
            <from uri="activemq:input"/>
            <delay>
                <constant>1000</constant>
            </delay>
            <to uri="activemq:output"/>
        </route>
    </routeContext>

</beans>

我明白了,在运行时使用 camel2 中的 loadRoutesDefinition 加载骆驼路线是可行的,如下所示,

InputStream inputStream = getClass().getResourceAsStream("MyRoute.xml");
    RoutesDefinition routesDefinition = camelContext.loadRoutesDefinition(is);
camelContext.addRouteDefinitions(routesDefinition.getRoutes());

我正在寻找在运行时在 camel3 中加载骆驼路线的可能方法。

标签: javaspring-bootapache-camel

解决方案


您可以使用ExtendedCamelContext从文件中加载路由定义,调整您的 Camel 上下文:

    ExtendedCamelContext ecc = camelContext.adapt(ExtendedCamelContext.class);
    Resource resource = ResourceHelper.fromBytes("resource.xml", bytes);
    ecc.getRoutesLoader().loadRoutes(resource);

其中bytes是存储文件内容的字节数组。


推荐阅读