第 12 章 OSGi 服务

摘要

OSGi 核心框架定义了 OSGi 服务层,它提供了一个简单机制,通过将 Java 对象注册为 OSGi 服务 registry 中的服务来进行交互。OSGi 服务模型的优势在于,任何 Java 对象都可以作为服务来提供:没有特定限制、继承规则或注解必须应用于服务类。本章论述了如何使用 OSGi Blueprint 容器部署 OSGi 服务。

12.1. Blueprint 容器

摘要

Blueprint 容器是一个依赖项注入框架,简化了与 OSGi 容器的交互。Blueprint 容器支持使用基于配置的方法,使用 OSGi 服务注册表(例如,提供标准的 XML 元素用于导入和导出 OSGi 服务)。

12.1.1. 蓝图配置

在 JAR 文件中的蓝图文件位置

相对于 bundle JAR 文件的根目录,Blueprint 配置文件的标准位置如下:

OSGI-INF/blueprint

此目录下带有后缀 .xml 的文件解释为 Blueprint 配置文件;换句话说,任何与模式匹配的文件 OSGI-INF/blueprint/*.xml

Maven 项目中的蓝图文件位置

在 Maven 项目 ProjectDir 上下文中,Blueprint 配置文件的标准位置如下:

ProjectDir/src/main/resources/OSGI-INF/blueprint

蓝图命名空间和 root 元素

蓝图配置元素与以下 XML 命名空间关联:

http://www.osgi.org/xmlns/blueprint/v1.0.0

蓝图配置的根元素是 蓝图,因此蓝图 XML 配置文件通常具有以下概述:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
  ...
</blueprint>
注意

蓝图 根元素中,不需要使用 xsi:schemaLocation 属性指定 Blueprint 模式的位置,因为 schema 位置已已知在 Blueprint 框架中。

蓝图清单配置

Blueprint 配置的一些方面由 JAR 清单文件中 META-INF/MANIFEST.MF 中的标头控制,如下所示:

自定义蓝图文件位置

如果您需要将 Blueprint 配置文件放在非标准位置(即 OSGI-INF/blueprint/*.xml以外的其他位置),您可以用逗号分隔在清单文件中 Bundle-Blueprint 标头中的其他位置列表:

Bundle-Blueprint: lib/account.xml, security.bp, cnf/*.xml

必需的依赖项

默认情况下,OSOS 服务的依赖项是强制的(虽然通过将 availability 属性设置为参考元素或一个 参考 列表 元素上的 可选 )可以进行更改。将依赖项声明为强制性意味着捆绑包无法正常运行,且这个依赖关系必须始终可用。

通常,当蓝图容器被初始化时,它会通过 宽限期,在这期间它尝试解析所有强制的依赖项时。如果在此时间内无法解决强制依赖关系(默认超时为 5 分钟),则容器初始化将中止且不会启动捆绑包。以下设置可附加到 Bundle-SymbolicName 清单标头中,以配置宽限期:

blueprint.graceperiod
如果为 true (默认),则启用了宽限期,并且 Blueprint 容器在初始化过程中会等待一个强制依赖项解析;如果为 false,则会跳过宽限期,容器不会检查是否已解决强制依赖关系。
blueprint.timeout
以毫秒为单位指定宽限期超时。默认值为 300000(5 分钟)。

例如,要启用 10 秒的宽限期,您可以在清单文件中定义以下 Bundle-SymbolicName 标头:

Bundle-SymbolicName: org.fusesource.example.osgi-client;
 blueprint.graceperiod:=true;
 blueprint.timeout:= 10000

Bundle-SymbolicName 标头的值是一个分号隔开的列表,其中第一个项目是实际捆绑包符号名称,第二个项目是 蓝图。graceperiod:=true 来启用宽限期,第三项,blueprint.timeout:= 10000 指定了 10 秒超时。

12.1.2. 定义服务 Bean

概述

Blueprint 容器允许您使用 bean 元素实例化 Java 类。您可以以这种方式创建所有主应用程序对象。特别是,您可以使用 bean 元素创建代表 OSGi 服务实例的 Java 对象。

蓝图 Bean 元素

Blueprint bean 元素在 Blueprint 架构命名空间 http://www.osgi.org/xmlns/blueprint/v1.0.0 中定义。

Bean 示例

以下示例演示了如何使用 Blueprint 的 Bean 来创建几种不同类型的 bean

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">

  <bean id="label" class="java.lang.String">
    <argument value="LABEL_VALUE"/>
  </bean>

  <bean id="myList" class="java.util.ArrayList">
    <argument type="int" value="10"/>
  </bean>

  <bean id="account" class="org.fusesource.example.Account">
    <property name="accountName" value="john.doe"/>
    <property name="balance" value="10000"/>
  </bean>

</blueprint>

其中 ,最后一个 bean 示例引用的帐户 类可以定义如下:

package org.fusesource.example;

public class Account
{
    private String accountName;
    private int balance;

    public Account () { }

    public void setAccountName(String name) {
        this.accountName = name;
    }

    public void setBalance(int bal) {
        this.balance = bal;
    }
    ...
}

参考信息

有关定义 Blueprint Bean 的详情,请参考以下参考:

12.1.3. 使用属性配置蓝图

概述

这部分论述了如何使用在 Camel 上下文以外的文件中保存的属性配置 Blueprint。

配置 Blueprint Bean

蓝图 Bean 可以通过使用可通过从外部文件使用属性放置的变量进行配置。您需要声明 ext 命名空间,并在 Blueprint xml 中添加 属性占位符 an。使用 Property-Placeholder bean 声明您的属性文件的位置到 Blueprint。

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.2.0">

    <ext:property-placeholder>
      <ext:location>file:etc/ldap.properties</ext:location>
    </ext:property-placeholder>
    ...
    <bean ...>
        <property name="myProperty" value="${myProperty}" />
    </bean>
</blueprint>

属性拥有者配置选项 的规格可在 http://aries.apache.org/schemas/blueprint-ext/blueprint-ext.xsd 找到。