I ServiceFactory的講解
一、首先我們實現一個繼承ServiceFactory接口聽服務Bean.如下:

public class HelloServiceFactory implementsServiceFactory{

       private int usageCounter = 0;

       public Object getService(Bundle bundle, ServiceRegistration registration) {

                   System.out.println("Create object of HelloService for " + bundle.getSymbolicName());

                   usageCounter++;

                   System.out.println("Number of bundles using service " + usageCounter);

                   HelloService helloService = new HelloServiceImpl();

                   return helloService;
       }

       public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {

                   System.out.println("Release object of HelloService for " + bundle.getSymbolicName());

                   usageCounter--;

                   System.out.println("Number of bundles using service " + usageCounter);
       }
}

注:這個ServiceFactory有兩個接口,

getService():     當某個Bundle首次通過BundleContext.getService(ServiceReference)形式請求服務的時候,此方法會自動呼叫。

ungetService(); 與上面的方法相反,當一個Bundle對象已經被發布了,並可能被消毁時,被調用。

二、改變客戶端的Activator程式,並改為如下內容:

public class HelloServiceActivator implements BundleActivator  {

          ServiceRegistration helloServiceRegistration;

          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();
          }
}

三、測試運行

通過以上編碼,我們即完成了相關代碼的開發工作,運行後從console台可以看到相關的輸出結果,讀者可根據實現情況Debug跟蹤。來加深理解。

II ServiceTracker的講解

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

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

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

源代码清单11.HelloServiceTracker.java
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
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()方法

posted on 2012-04-10 13:56  haoxx  阅读(650)  评论(0编辑  收藏  举报