服务跟踪(ServiceTracker)

当多个Bundle使用同一接口名注册服务时,服务的获取策略如下:OSGi容器会返回排行最高的服务,即,返回注册时SERVICE_RANKING属性值最大的服务。如果有多个服务的排行值相等,那么OSGi容器将返回PID值最小的那个服务。

 

如果服务消费者需要对服务进行跟踪,比如服务何时被注册,何时取消注册等,可以使用ServiceTracker类。以下是该类的使用范例源码:

 

1、接口及其实现类:

Java代码   收藏代码
  1. public   interface  HelloService {  
  2.     public  String sayHello(String name);  
  3. }  

 

Java代码   收藏代码
  1. public   class  HelloServiceImpl  implements  HelloService {  
  2.     public  String sayHello(String name) {  
  3.         return   "Hello "  + name;  
  4.     }  
  5. }  

 

2、服务跟踪器子类

Java代码   收藏代码
  1. /**  
  2.  * 服务跟踪器:跟踪注册到HelloService接口下的所有服务  
  3.  */   
  4. public   class  HelloServiceTracker  extends  ServiceTracker {  
  5.     public  HelloServiceTracker(BundleContext context){  
  6.         super (context, HelloService. class .getName(),  null );  
  7.     }  
  8.       
  9.     @Override   
  10.     public  Object addingService(ServiceReference reference) {  
  11.         System.out.println("adding service: "  + reference.getBundle().getSymbolicName());  
  12.         return   super .addingService(reference);  
  13.     }  
  14.   
  15.     @Override   
  16.     public   void  removedService(ServiceReference reference, Object service) {  
  17.         System.out.println("removed service: "  + reference.getBundle().getSymbolicName());  
  18.         super .removedService(reference, service);  
  19.     }  
  20. }  

 

3、Bundle激活器类

Java代码   收藏代码
  1. public   class  Activator  implements  BundleActivator {  
  2.     HelloServiceTracker helloServiceTracker;  
  3.     ServiceRegistration serviceRegistration;  
  4.       
  5.     public   void  start(BundleContext context)  throws  Exception {  
  6.         System.out.println("start..." );  
  7.           
  8.         //启动服务跟踪器   
  9.         helloServiceTracker = new  HelloServiceTracker(context);  
  10.         helloServiceTracker.open();  
  11.           
  12.         //注册服务   
  13.         serviceRegistration = context.registerService(HelloService.class .getName(),  new  HelloServiceImpl(),  null );  
  14.           
  15.         //获取被跟踪的服务   
  16.         HelloService helloService =  (HelloService)helloServiceTracker.getService();  
  17.         if (helloService!= null ){  
  18.             System.out.println(helloService.sayHello("cjm" ));  
  19.         }  
  20.     }  
  21.   
  22.     public   void  stop(BundleContext context)  throws  Exception {  
  23.         System.out.println("stop" );  
  24.           
  25.         //关闭服务跟踪器   
  26.         helloServiceTracker.close();  
  27.     
  28.         serviceRegistration.unregister();  
  29.     }  
  30. }  

 

4、运行结果

osgi>
start...
adding service: p1
Hello cjm


osgi> stop 86
stop
removed service: p1

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值