OSGi服务:非常适合SOA的架构

本文是《你 好,OSGi 》系列的第四部分。下面讲述OSGi服务。对OSGi不是很了解的读者可以先阅读OSGi是什么 一文。

OSGi服务

前面我们提到,OSGi架构非常适合我们实现面向服务的应用(SOA)。它可以让Bundles导出服务,而其它的Bundles可以在不必了解源 Bundles任何信息的情况下消费这些导出的服务。由于OSGi具有隐藏真实的服务实现类的能力,所以它为面向服务的应用提供了良好的类与接口的组合。

在OSGi框架中,源Bundle在OSGi容器中注册POJO对象,该对象不必实现任何接口,也不用继承任何超类,但它可以注册在一个或多个接口 下,并对外提供服务。目标Bundle可以向OSGi容器请求注册在某一接口下的服务,一旦它发现该服务,目标Bundle就会将该服务绑定到这个接口, 并能调用该接口中的方法。下面我们举个例子,以便我们能更好理解与OSGi相关的这些概念。

5.1. 导出服务

在本小节中,我们将更新HelloService Bundle,以便它能把HelloServiceImpl类的对象导出为服务,具体步骤如下:

1) 修改com.javaworld.sample.HelloService Bundle中的MANIFEST.MF文件,让它导入org.osgi.framework包(译者注,这一步我们已经完成);

2) 新建Java类com.javaworld.sample.impl.HelloServiceActivator.java,其源代码如清单7所示;

源代码清单7. HelloServiceActivator.java

public class HelloServiceActivator implements BundleActivator {
  ServiceRegistrationhelloServiceRegistration;
  public void start(BundleContext context)throws Exception {
    HelloService helloService = newHelloServiceImpl();
    helloServiceRegistration=context.registerService(HelloService.class.getName(), helloService, null);
  }
  public void stop(BundleContext context)throws Exception {
    helloServiceRegistration.unregister();
  }
}

请注意,在源Bundle中,我们应使用BundleContext.registerService()方法导出服务,这个方法带三个参数:

a) 该方法第一个参数为您要注册的服务的接口名称。如果您想把您的服务注册到多个接口下,您需要新建一个String数组存放这些接口名,然后把这个数组作为 第一个参数传给registerService()方法。在示例代码中,我们想把我们的服务导出到HelloServer接口名下;

b) 第二个参数是您要注册的服务的实际Java对象。在示例代码中,我们导出HelloServiceImpl类的对象,并将其作为服务;

c) 第三个参数为服务的属性,它是一个Dictionary对象。如果多个Bundle导出服务的接口名相同,目标Bundle就可以使用这些属性对源 Bundle进行过滤,找到它感兴趣的服务。

3) 最后,请修改HelloServiceBundle中的MANIFEST.MF文件,将Bundle-Activator属性头的值改为 com.javaworld.sample.service.impl.HelloServiceActivator。

现在HelloService Bundle就可以导出HelloServiceImpl对象了。当OSGi容器启动HelloServiceBundle时,它会将控制权交给 HelloServiceActivator.java类,HelloServiceActivator将HelloServiceImpl对象注册为服 务。下面,我们开始创建该服务的消费者。

5.2. 导入服务

在本小节中,我们将修改上面开发的HelloWorld Bundle,以便让它成为HelloService服务的消费者。您主要需要修改HelloWorldBundle中的Activator.java代 码,修改后的代码如源代码清单8所示:

源代码清单8. HelloWorld Bundle中的Activator.java

packagecom.javaworld.sample.helloworld; 
 
importorg.osgi.framework.BundleActivator; 
importorg.osgi.framework.BundleContext; 
importorg.osgi.framework.ServiceReference; 
importcom.javaworld.sample.service.HelloService; 
 
publicclass Activator implements BundleActivator { 
  ServiceReference helloServiceReference; 
  public void start(BundleContext context)throws Exception { 
    System.out.println("HelloWorld!!"); 
    helloServiceReference=context.getServiceReference(HelloService.class.getName()); 
    HelloService helloService=(HelloService)context.getService(helloServiceReference); 
    System.out.println(helloService.sayHello()); 
  } 
  public void stop(BundleContext context)throws Exception { 
    System.out.println("Goodbye World!!"); 
    context.ungetService(helloServiceReference); 
  } 
}

在上面的代码中,BundleContext.getServiceReference()方法将为注册在HelloService接口下的服务返 回一个ServiceReference对象。如果存在多个HelloService服务,该方法会返回排行最高的服务(服务的排行是通过 Constants.SERVICE_RANKING属性指定的)。您一旦获得ServiceReference对象,您就可以调用其 BundleContext.getService()方法获取真实的服务对象。

您可以参照运行Bundle的方法运行上面的示例应用,请点击“RunàRun…”菜单,并确保HelloWorld和HelloService这 两个Bundle被选中。当您启动HelloServiceBundle时,您会在控制台上看到 “InsideHelloServiceImple.sayHello()”,这个消息是由HelloServiceImpl.sayHello()方法 打印出来的。

5.3. 创建服务工厂

在上节中,我们学会了如何使用OSGi框架新建一个Java对象,并把它注册为一个服务,然后让其它的Bundle去消费这个服务。如果您看一下 HelloServiceActivator.start()方法,您会注意到我们在start()方法中新建了HelloServiceImpl类对 象,然后将它注册到HelloService接口名下。这样注册后,任何其它的Bundle在请求HelloService服务时,OSGi容器将返回同 一对象。

在大多数情况下,这样的实现方法没有问题。但是,比如说我们要为每一个Bundle消费者返回不同的HelloServiceImpl对象,再比如 说,您的服务对象要提供的服务为打开一个数据库连接,但并不是马上就打开它,而是在真正需要的时候才打开这个数据库连接。

对这两种情况,我们的解决方法是,新建一个类实现ServiceFactory接口,并把该类的对象注册为服务,但并不是注册实际的服务对象。一旦 您完成这一步,其它Bundle在请求该服务时,您的ServiceFactory实现类将接管该请求,ServiceFactory会为每个 Bundle新建一个服务对象,并将真实服务的创建时间延迟到有人真正需要该服务的时候。

下面我们将使用ServiceFactory更新我们上面开发的 com.javaworld.sample.HelloServiceBundle,具体步骤如下:

1) 新建工厂 类HelloServiceFactory.java,源代码如清单9所示。

源代码清单9 . HelloServiceFactory.java

public class HelloServiceFactory implements ServiceFactory{ 
  private int usageCounter = 0; 
  public Object getService(Bundle bundle,ServiceRegistration registration) { 
    System.out.println("Create objectof HelloService for " + bundle.getSymbolicName()); 
    usageCounter++; 
    System.out.println("Number ofbundles using service " + usageCounter); 
    HelloService helloService = newHelloServiceImpl(); 
    return helloService; 
  } 
  public void ungetService(Bundle bundle,ServiceRegistration registration, Object service) { 
    System.out.println("Release objectof HelloService for " + bundle.getSymbolicName()); 
    usageCounter--; 
    System.out.println("Number ofbundles using service " + usageCounter); 
  } 
}

从上面的代码中,我们可以看到,ServiceFactory接口定义了两个方法:

a) getService()方法:当某个Bundle第一次使用BundleContext.getService(ServiceReference)方 法请求一个服务对象时,OSGi框架会调用该方法。在源代码清单9中,我们用这个方法为每个Bundle新建并返回不同的 HelloServiceImpl对象,如果这个对象不是null,OSGi框架会缓存这个对象。如果同一个Bundle再次调用 BundleContext.getService(ServiceReference)方法,OSGi将返回同一个服务对象。

b) ungetService()方法:当Bundle释放服务时,OSGi容器可以调用该方法销毁服务对象。在源代码清单9中,我们使用 usageCounter变量来跟踪服务的使用数目,并打印出该服务的客户端数量。

2) 修改HelloService Bundle中的HelloServiceActivator.java的start()方法,让它注册到ServiceFactory接口名下,而不是 注册到HelloService接口。详细代码如清单10所示:

源代码清单10. 修改后的HelloServiceBundle中的HelloServiceActivator.java

package com.javaworld.sample.service.impl; 
importorg.osgi.framework.BundleActivator; 
importorg.osgi.framework.BundleContext; 
importorg.osgi.framework.ServiceRegistration; 
 
importcom.javaworld.sample.helloservice.HelloServiceFactory; 
importcom.javaworld.sample.service.HelloService; 
 
publicclass HelloServiceActivator implements BundleActivator { 
  ServiceRegistrationhelloServiceRegistration; 
  public void start(BundleContext context)throws Exception { 
    HelloServiceFactory helloServiceFactory= new HelloServiceFactory(); 
    helloServiceRegistration=context.registerService(HelloService.class.getName(), helloServiceFactory,null); 
  } 
  public void stop(BundleContext context)throws Exception { 
    helloServiceRegistration.unregister(); 
  } 
}

现在,您可以试运行示例代码。您会注意到,当HelloWorld Bundle启动时,服务计数器变为1;当HelloWorldBundle停止时,服务计数器的数目将变为0。

5.4. 跟踪服务

在“OSGi服务”小节,您学会了如何使用服务的接口名搜索服务。但如果有多个Bundle使用同一接口名注册服务,那会发生什么呢?这 时,OSGi容器将返回排行最高的服务,即,返回注册时那个SERVICE_RANKING属性值最大的服务。如果有多个服务的排行值相等,那么OSGi 容器将返回PID值最小的那个服务。

但是,如果您的服务消费者需要了解某一接口下的服务对象何时注册、何时取消注册,这时,您应使用ServiceTracker类。下面,我们看看如 何使用服务跟踪器来修改我们的示例代码,具体步骤如下。

1) 修改HelloWorldBundle的MANIFEST.MF文件,让它导入org.osgi.util.tracker包;

2) 新建类HelloServiceTracker.java,其源代码参见清单11。

源代码清单11.HelloServiceTracker.java

public class HelloServiceTracker extends ServiceTracker { 
  public HelloServiceTracker(BundleContext context) { 
    super(context, HelloService.class.getName(),null); 
  } 
  public Object addingService(ServiceReference reference) { 
    System.out.println("Inside HelloServiceTracker.addingService " + reference.getBundle()); 
    return super.addingService(reference); 
  } 
  public void removedService(ServiceReference reference, Object service) { 
    System.out.println("Inside HelloServiceTracker.removedService " + reference.getBundle()); 
    super.removedService(reference, service); 
  } 
}

在上面的HelloSerivceTracker类的构造函数中,您可以看到,我们把HelloService接口名传入其父类中,这相当于 说,HelloServiceTracker应跟踪注册到HelloService接口名下的所有服务,HelloServiceTracker继承自 ServiceTracker类,实现了下面两个方法:

a) addingService()方法:当Bundle使用接口名注册服务时,该方法将会被调用;

b)removedService()方法:当Bundle取消注册某个接口名下的服务时,该方法将会被调用。

3) 用HelloServiceTracker类更新我们的Activator.java类,以便让它来管理服务,而不是直接去查找它们,源代码请参见清单 12。

源代码清单12. 使用了HelloServiceTracker的Activator.java

public class Activator implements BundleActivator { 
  HelloServiceTracker helloServiceTracker; 
  public void start(BundleContext context) throws Exception { 
    System.out.println("Hello World!!"); 
    helloServiceTracker= new HelloServiceTracker(context); 
    helloServiceTracker.open(); 
    HelloService helloService = (HelloService)helloServiceTracker.getService(); 
    System.out.println(helloService.sayHello()); 
  } 

  public void stop(BundleContext context) throws Exception { 
    System.out.println("Goodbye World!!"); 
    helloServiceTracker.close(); 
  } 
}

我们看到,在初始的start()方法中,我们首先新建一个HelloServiceTracker对象,然后要求这个对象跟踪 HelloService接口下的服务。这时,我们可以调用getService()方法获得HelloService对象。

如果您试运行上面的示例代码,您会注意到,在启动或停止HelloSerivceBundle时,OSGi容器都会调用 HelloServiceTracker对象的addingService()方法或removedService()方法。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值