java - 在运行时加载 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 中加载骆驼路线的可能方法。
解决方案
您可以使用ExtendedCamelContext从文件中加载路由定义,调整您的 Camel 上下文:
ExtendedCamelContext ecc = camelContext.adapt(ExtendedCamelContext.class);
Resource resource = ResourceHelper.fromBytes("resource.xml", bytes);
ecc.getRoutesLoader().loadRoutes(resource);
其中bytes是存储文件内容的字节数组。
推荐阅读
- python - C++ 类:如何使用 dict["hello"]=20; 像 Python
- javascript - 具有输入值的 JSON 对象的索引
- python - 为什么默认情况下没有将 pip 包安装到 $PATH 到?
- r - R:混合多列并根据字符串条件从一列中选择行值
- django - 为什么要使用 Django 模型管理器?
- python - 元音列表:奇怪的 lambda 表达式
- c# - 使用 VST.net 和 Unity3d 创建一个简单的 VST 主机
- python - 即使保留副本,Python 嵌套字典也会被覆盖
- r - 在 [R] 中绘制类(预测)的时间序列列表
- javascript - “字符”之后的画面