搜索

210.4. Eclipse Kura 组件

download PDF

从 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 捆绑包:

public class MyKuraRouter extends KuraRouter {

  @Override
  public void configure() throws Exception {
    from("timer:trigger").
      to("netty-http:http://app.mydatacenter.com/api");
  }

}

请记住,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

请记住,您不必导入要在路由中使用的每个 Camel 组件捆绑包,因为 Camel 组件会作为运行时级别的服务解析。

在部署路由器捆绑包前,请确定您部署了(并启动)以下 Camel 核心捆绑包(使用 Kura GoGo shell)…​

install file:///home/user/.m2/repository/org/apache/camel/camel-core/2.15.0/camel-core-2.15.0.jar
start <camel-core-bundle-id>
install file:///home/user/.m2/repository/org/apache/camel/camel-core-osgi/2.15.0/camel-core-osgi-2.15.0.jar
start <camel-core-osgi-bundle-id>
install file:///home/user/.m2/repository/org/apache/camel/camel-kura/2.15.0/camel-kura-2.15.0.jar
start <camel-kura-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>

210.4.3. KuraRouter 工具

 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 日志记录器实例。

public class MyKuraRouter extends KuraRouter {

    @Override
    public void configure() throws Exception {
        log.info("Configuring Camel routes!");
        ...
    }

}

210.4.3.2. BundleContext

受保护的成员 bundleContext 返回与给定 Kura 路由器关联的捆绑包上下文。

public class MyKuraRouter extends KuraRouter {

    @Override
    public void configure() throws Exception {
        ServiceReference<MyService> serviceRef = bundleContext.getServiceReference(LogService.class.getName());
        MyService myService = bundleContext.getService(serviceRef);
        ...
    }

}

210.4.3.3. CamelContext

保护的成员 camelContext 是与给定 Kura 路由器关联的 CamelContext

public class MyKuraRouter extends KuraRouter {

    @Override
    public void configure() throws Exception {
        camelContext.getStatus();
        ...
    }

}

210.4.3.4. ProducerTemplate

受保护的 member producerTemplate 是与给定 Camel 上下文关联的 ProducerTemplate 实例。

public class MyKuraRouter extends KuraRouter {

    @Override
    public void configure() throws Exception {
        producerTemplate.sendBody("jms:temperature", 22.0);
        ...
    }

}

210.4.3.5. ConsumerTemplate

受保护的成员 consumerTemplate 是与给定 Camel 上下文关联的 ConsumerTemplate 实例。

public class MyKuraRouter extends KuraRouter {

    @Override
    public void configure() throws Exception {
        double currentTemperature = producerTemplate.receiveBody("jms:temperature", Double.class);
        ...
    }

}

210.4.3.6. OSGi 服务解析器

OSGi 服务解析器(Class<T> serviceType)用于通过类型从 OSGi 捆绑包上下文轻松检索服务。

public class MyKuraRouter extends KuraRouter {

    @Override
    public void configure() throws Exception {
        MyService myService = service(MyService.class);
        ...
    }

}

如果没有找到 service,则返回 null 值。如果您希望应用程序在服务不可用时失败,请使用 requiredService (Class) 方法。如果找不到服务,则 requiredService 会抛出 IllegalStateException

public class MyKuraRouter extends KuraRouter {

    @Override
    public void configure() throws Exception {
        MyService myService = requiredService(MyService.class);
        ...
    }

}

210.4.4. KuraRouter activator 回调

Kura 路由器附带生命周期回调,可用于自定义 Camel 路由器的工作方式。例如,要在前配置与路由器关联的 CamelContext 实例,请覆盖 KuraRouter 类的 beforeStart 方法:

public class MyKuraRouter extends KuraRouter {

  ...

  protected void beforeStart(CamelContext camelContext) {
    OsgiDefaultCamelContext osgiContext = (OsgiCamelContext) camelContext;
    osgiContext.setName("NameOfTheRouter");
  }

}

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 路由文件,例如:

<routes xmlns="http://camel.apache.org/schema/spring">
    <route id="loaded">
        <from uri="direct:bar"/>
        <to uri="mock:bar"/>
    </route>
</routes>

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>

210.4.7. 另请参阅

  • 配置 Camel
  • 组件
  • 端点
  • 开始使用
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.