210.4. Eclipse Kura 组件
从 Camel 2.15 开始提供
本文档页面涵盖了使用 Eclipse Kura M2M 网关 Camel 的集成选项。将 Camel 路由部署到 Eclipse Kura 的常见原因是向消息传递 M2M 网关提供企业集成模式和 Camel 组件。例如,您可能想要在 Raspberry PI 上安装 Kura,然后使用 Kura 服务读取与 Raspberry PI 附加到 Raspberry PI 的温度值,最后使用 Camel EIP 和组件将当前的温度值转发到您的数据中心服务。
210.4.1. KuraRouter activator 复制链接链接已复制到粘贴板!
部署到 Eclipse Kura 的捆绑包通常作为捆绑包激活器进行开发。因此,将 Apache Camel 路由部署到 Kura 的最简单方法是创建一个包含扩展 org.apache.camel.kura.KuraRouter
类的 OSGi 捆绑包:
请记住,K uraRouter
实现 org.osgi.framework.BundleActivator
接口,因此您需要 在创建 Kura 捆绑包组件类时 注册其
生命周期方法。
启动和停止
Kura 路由器启动其自己的 OSGi 感知型 CamelContext
。这意味着,对于每个类扩展 KuraRouter
,将有一个专用的 CamelContext
实例。理想情况下,我们建议为每个 OSGi 捆绑部署一个 KuraRouter
。
210.4.2. 部署 KuraRouter 复制链接链接已复制到粘贴板!
包含 Kura 路由器类的捆绑包应在 OSGi 清单中导入以下软件包:
Import-Package: org.osgi.framework;version="1.3.0", org.slf4j;version="1.6.4", org.apache.camel,org.apache.camel.impl,org.apache.camel.core.osgi,org.apache.camel.builder,org.apache.camel.model, org.apache.camel.component.kura
Import-Package: org.osgi.framework;version="1.3.0",
org.slf4j;version="1.6.4",
org.apache.camel,org.apache.camel.impl,org.apache.camel.core.osgi,org.apache.camel.builder,org.apache.camel.model,
org.apache.camel.component.kura
请记住,您不必导入要在路由中使用的每个 Camel 组件捆绑包,因为 Camel 组件会作为运行时级别的服务解析。
在部署路由器捆绑包前,请确定您部署了(并启动)以下 Camel 核心捆绑包(使用 Kura GoGo shell)…
… 以及您要在路由中使用的所有组件:
install file:///home/user/.m2/repository/org/apache/camel/camel-stream/2.15.0/camel-stream-2.15.0.jar start <camel-stream-bundle-id>
install file:///home/user/.m2/repository/org/apache/camel/camel-stream/2.15.0/camel-stream-2.15.0.jar
start <camel-stream-bundle-id>
最后部署路由器捆绑包:
install file:///home/user/.m2/repository/com/example/myrouter/1.0/myrouter-1.0.jar start <your-bundle-id>
install file:///home/user/.m2/repository/com/example/myrouter/1.0/myrouter-1.0.jar
start <your-bundle-id>
210.4.3. KuraRouter 工具 复制链接链接已复制到粘贴板!
Kura router base class provides many useful utilities. This section explores each of them.
Kura router base class provides many useful utilities. This section
explores each of them.
210.4.3.1. SLF4J logger 复制链接链接已复制到粘贴板!
Kura 使用 SLF4J facade 进行日志记录目的。受保护的成员 日志
返回与给定 Kura 路由器关联的 SLF4J 日志记录器实例。
210.4.3.2. BundleContext 复制链接链接已复制到粘贴板!
受保护的成员 bundleContext
返回与给定 Kura 路由器关联的捆绑包上下文。
210.4.3.3. CamelContext 复制链接链接已复制到粘贴板!
保护的成员 camelContext
是与给定 Kura 路由器关联的 CamelContext
。
210.4.3.4. ProducerTemplate 复制链接链接已复制到粘贴板!
受保护的 member producerTemplate
是与给定 Camel 上下文关联的 ProducerTemplate
实例。
210.4.3.5. ConsumerTemplate 复制链接链接已复制到粘贴板!
受保护的成员 consumerTemplate
是与给定 Camel 上下文关联的 ConsumerTemplate
实例。
210.4.3.6. OSGi 服务解析器 复制链接链接已复制到粘贴板!
OSGi 服务解析器(Class<T> serviceType)
用于通过类型从 OSGi 捆绑包上下文轻松检索服务。
如果没有找到 service,则返回 null
值。如果您希望应用程序在服务不可用时失败,请使用 requiredService (Class)
方法。如果找不到服务,则 requiredService
会抛出 IllegalStateException
。
210.4.4. KuraRouter activator 回调 复制链接链接已复制到粘贴板!
Kura 路由器附带生命周期回调,可用于自定义 Camel 路由器的工作方式。例如,要在前配置与路由器关联的 CamelContext
实例,请覆盖 KuraRouter
类的 beforeStart
方法:
210.4.5. 从 ConfigurationAdmin 加载 XML 路由 复制链接链接已复制到粘贴板!
有时需要从服务器配置中读取路由的 XML 定义。对于参与式重新部署成本的 IoT 网关来说,这种常见方案可能是很大的。为了满足此要求,每个 KuraRouter
使用 OSGi ConfigurationAdmin 从 kura.camel.BUNDLE-SYMBOLIC-SYMBOLIC-NAME.route
属性中查找 kura.camel
PID。这个方法允许您为每个部署的 KuraRouter
定义 Camel XML 路由文件。要更新路由,只需编辑适当的配置属性并重启与其关联的捆绑包。kura.camel.BUNDLE-SYMBOLIC-NAME.route
属性的内容应该是 Camel XML 路由文件,例如:
210.4.6. 将 Kura 路由器部署为声明 OSGi 服务 复制链接链接已复制到粘贴板!
如果要将 Kura 路由器部署为声明 OSGi 服务,您可以使用 KuraRouter
提供的
方法。
激活和停用
<scr:component name="org.eclipse.kura.example.camel.MyKuraRouter" activate="activate" deactivate="deactivate" enabled="true" immediate="true"> <implementation class="org.eclipse.kura.example.camel.MyKuraRouter"/> </scr:component>
<scr:component name="org.eclipse.kura.example.camel.MyKuraRouter" activate="activate" deactivate="deactivate" enabled="true" immediate="true">
<implementation class="org.eclipse.kura.example.camel.MyKuraRouter"/>
</scr:component>
210.4.7. 另请参阅 复制链接链接已复制到粘贴板!
- 配置 Camel
- 组件
- 端点
- 开始使用