读书人

服务追踪(ServiceTracker)

发布时间: 2012-11-07 09:56:10 作者: rapoo

服务跟踪(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

?

读书人网 >软件架构设计

热点推荐