服务追踪(ServiceTracker)
服务跟踪(ServiceTracker)public?class?Activator?implements?BundleActivator?{??????HelloServiceTracke
服务跟踪(ServiceTracker)
public?class?Activator?implements?BundleActivator?{??????HelloServiceTracker?helloServiceTracker;??????ServiceRegistration?serviceRegistration;????????????public?void?start(BundleContext?context)?throws?Exception?{??????????System.out.println("start...");????????????????????//启动服务跟踪器??????????helloServiceTracker?=?new?HelloServiceTracker(context);??????????helloServiceTracker.open();????????????????????//注册服务??????????serviceRegistration?=?context.registerService(HelloService.class.getName(),?new?HelloServiceImpl(),?null);????????????????????//获取被跟踪的服务??????????HelloService?helloService?=??(HelloService)helloServiceTracker.getService();??????????if(helloService!=null){??????????????System.out.println(helloService.sayHello("cjm"));??????????}??????}????????public?void?stop(BundleContext?context)?throws?Exception?{??????????System.out.println("stop");????????????????????//关闭服务跟踪器??????????helloServiceTracker.close();??????????????serviceRegistration.unregister();??????}??}??
?
4、运行结果
osgi>
start...
adding service: p1
Hello cjm
osgi> stop 86
stop
removed service: p1
?