向 OSGi 声明式服务声明服务

可以使用单独的 XML 文件在捆绑软件中声明每项服务。

关于此任务

声明式服务 (DS) 支持对已声明的组件执行操作,每个组件由捆绑软件中的 XML 文件定义。将包含组件声明的捆绑软件添加至框架时,DS 将读取每个组件声明,并在服务注册表中注册所提供的服务。DS 随后会管理组件的生命周期:根据声明式属性和满足依赖性的组合来控制它的生命周期。

组件的 XML 描述可让 DS 解析服务依赖性,而不要求实例化组件,也不要求装入其实现类。这便于延迟和懒惰装入资源,从而有助于改进服务器的启动以及减少运行时内存占用量。

在捆绑软件的 MANIFEST.MF 文件中使用 Service-Component 头列示了用于描述组件的 XML 文件,这些 XML 文件通常位于该捆绑软件的 /OSGI-INF 目录中。

有许多工具可用来生成必需的 XML;下列示例显示了 XML 本身。

本主题描述了一个使用 XML 向 DS 声明其组件的简单 OSGi 捆绑软件。

过程

  1. 通过组件的实现类名来标识组件。
    <component>
      <implementation class="com.example.bundle.HelloComponent"/>
    </component>
  2. 通过引用服务提供的接口的名称来声明该服务。这是捆绑软件启动时将由 DS 发布到服务注册表的名称。
    <component>
      <implementation class="com.example.bundle.HelloComponent"/>
      <service>
         <provide interface="com.example.HelloService"/>
      		</service> 
    </component>
  3. 组件的名称。组件名称还充当服务“持久身份”(即,PID),它用来使配置属性与该服务相关联。在激活时以及每次更新属性时,具有相匹配的 PID 的配置属性将插入到组件中。
    <component name="HelloService">
      <implementation class="com.example.bundle.HelloComponent"/>
      <service>
        <provide interface="com.example.HelloService"/>
      		</service> 
    </component>
    注: 在 Liberty 中,用户可以将以下元素添加至 server.xml 配置文件,并且属性将注入到 HelloComponent 类中。
    <HelloService firstKey="firstValue" secondKey="secondValue" />
  4. 将此 XML 文件打包到捆绑软件中。
    例如,此 XML 文件位于 OSGI-INF/HelloService.xml,您向捆绑软件清单 MANIFEST.MF 文件添加一个头,以便 DS 可以找到该文件:
    Service-Component: OSGI-INF/HelloService.xml
    如果有多个组件打包在同一捆绑软件中,那么必须以逗号分隔的列表形式输入相应的 XML 文件。例如:
    Service-Component: OSGI-INF/HelloService.xml, OSGI-INF/GoodbyeService
  5. HelloService 组件的 Java™ 实现如下所示:
    package com.example.bundle;
    
    import com.example;
    import org.osgi.service.component.ComponentContext;
    
    /*
     * This class must be public and have a public default constructor for it to be 
     * usable by DS. This class is not required to be exported from the bundle.
     */
    public class HelloComponent implements HelloService {
    	/**
    	 	 * Optional: DS method to activate this component. If this method exists, it
    	 	 * will be invoked when the component is activated. Best practice: this
    	 	 * should be a protected method, not public or private
    	 * 
    	 * @param properties
    	 	 *            : Map containing service & config properties
    	 	 *            populated/provided by config admin
    	 */
    	protected void activate(ComponentContext cContext, Map<String, Object> properties) {
    		modified(properties);
    	}
    
    	/**
    	 	 * Optional: DS method to deactivate this component. If this method exists,
    	 	 * it will be invoked when the component is deactivated. Best practice: this
    	 	 * should be a protected method, not public or private
    	 * 
    	 	 * @param reason
    	 	 *            int representation of reason the component is stopping
    	 */
    	protected void deactivate(ComponentContext cContext, int reason) {
    	}
    
    	/**
    	 	 * Optional: DS method to modify the configuration properties. This may be
    	 	 * called by multiple threads: configuration admin updates may be processed
    	 	 * asynchronously. This is called by the activate method, and otherwise when
    	 	 * the configuration properties are modified while the component is
    	 	 * activated.
    	 * 
    	 * @param properties
    	 */
    	public synchronized void modified(Map<String, Object> properties) {
    				// process configuration properties here
    	}
    
    	/**
    	 	 * Service method defined by com.example.HelloService interface
    	 */
    		public void sayHello() {
    		System.out.println("Hello");
    	}
    }

用于指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: Monday, 5 December 2016
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-libcore-mp&topic=twlp_declare_services_ds
文件名:twlp_declare_services_ds.html