在Eclipse中开发OSGi(2)开发一个OSGi应用

来源:互联网 发布:淘宝复古女装店铺 编辑:程序博客网 时间:2024/04/30 17:08

发布和使用服务

由于 OSGi 框架能够方便的隐藏实现类,所以对外提供接口是很自然的事情,OSGi 框架提供了服务的注册和查询功能。好的,那么我们实际操作一下,就在 Hello world 工程的基础上进行。

我们需要进行下列的步骤:

  1. 定义一个服务接口,并且 export 出去供其它 bundle 使用;
  2. 定义一个缺省的服务实现,并且隐藏它的实现;
  3. Bundle 启动后,需要将服务注册到 Equinox 框架;
  4. 从框架查询这个服务,并且测试可用性。

好的,为了达到上述要求,我们实际操作如下:

1. 定义一个新的包 osgi.test.helloworld.service ,用来存放接口。单独一个 package 的好处是,您可以仅仅 export 这个 package 给其它 bundle 而隐藏所有的实现类 


 2. 在上述的包中新建接口 IHello,提供一个简单的字符串服务,代码如下:


IHello

package osgi.test.helloworld.service; public interface IHello {     /**      * 得到 hello 信息的接口 .      * @return the hello string.      */     String getHello(); }

3. 再新建一个新的包 osgi.test.helloworld.impl,用来存放实现类。


4. 在上述包中新建 DefaultHelloServiceImpl 类,实现上述接口: 


IHello 接口实现

public class DefaultHelloServiceImpl implements IHello {     @Override     public String getHello() {         return "Hello osgi,service";     }  }

5. 注册服务,OSGi 框架提供了两种注册方式,都是通过 BundleContext 类实现的:

    1)registerService(String,Object,Dictionary) 注册服务对象 object 到接口名 String 下,可以携带一个属性字典Dictionary

    2)registerService(String[],Object,Dictionary) 注册服务对象 object 到接口名数组 String[] 下,可以携带一个属性字典 Dictionary,即一个服务对象可以按照多个接口名字注册,因为类可以实现多个接口;

我们使用第一种注册方式,修改 Activator 类的 start 方法,加入注册代码:

public void start(BundleContext context) throws Exception {             System.out.println("hello world");     context.registerService(         IHello.class.getName(),         new DefaultHelloServiceImpl(),         null);         }

6.为了让我们的服务能够被其它 bundle 使用,必须在 MANIFEST.MF 中对其进行导出声明,双击 MANIFEST.MF,找到runtime > exported packages > 点击 add,如图,选择 service 包即可: 


选择导出的服务包


7.另外新建一个类似于 hello world 的 bundle 叫:osgi.test.helloworld2,用于测试 osgi.test.helloworld bundle 提供的服务的可用性;


8.添加 import package:在第二个 bundle 的 MANIFEST.MF 文件中,找到 dependencies > Imported packages > Add …,选择我们刚才 export 出去的 osgi.test.helloworld.service 包: 


选择刚才 export 出去的 osgi.test.helloworld.service 包


9. 查询服务:同样,OSGi 框架提供了两种查询服务的引用 ServiceReference 的方法:

    1)getServiceReference(String):根据接口的名字得到服务的引用;

    2)getServiceReferences(String,String):根据接口名和另外一个过滤器名字对应的过滤器得到服务的引用;


10. 这里我们使用第一种查询的方法,在 osgi.test.helloworld2 bundle 的 Activator 的 start 方法加入查询和测试语句:

加入查询和测试语句

public void start(BundleContext context) throws Exception {     System.out.println("hello world2");             /**         * Test hello service from bundle1.     */     IHello hello1 =         (IHello) context.getService(         context.getServiceReference(IHello.class.getName()));         System.out.println(hello1.getHello()); }

11. 修改运行环境,因为我们增加了一个 bundle,所以说也需要在运行配置中加入对新的 bundle 的配置信息,如下图所示:

12. 执行,得到下列结果:



转自:http://www.cnblogs.com/lw900320/archive/2012/06/26/2563221.html


0 0
原创粉丝点击