简体   繁体   English

如何注销OSGi服务

[英]how to unregister an OSGi service

I have the following test code, which should register and than unregister a test service. 我有以下测试代码,该代码应注册然后注销一个测试服务。 I tested it in an Eclipse 4.5.2 Mail-Demo appliation. 我在Eclipse 4.5.2 Mail-Demo应用程序中对其进行了测试。 The problem is that the service is not unregisted, the second call for the service is not null. 问题在于该服务不是未注册的,对该服务的第二次调用也不为空。

BundleContext bundleContext = Activator.getDefault().getBundle().getBundleContext();

ServiceRegistration<ITestService> registration = bundleContext.registerService(ITestService.class, new TestService(), null);

ServiceReference<ITestService> serviceReference = bundleContext.getServiceReference(ITestService.class);
bundleContext.ungetService(serviceReference);

serviceReference = bundleContext.getServiceReference(ITestService.class);
System.out.println("should be null but is not: " + serviceReference);

This is the output I get: 这是我得到的输出:

should be null but is not: {unregister.osgiservice.ITestService}={service.id=172, service.bundleid=8, service.scope=singleton}

How do I unregister the service correctly? 如何正确注销服务?


EDIT: 编辑:
Ok I found ServiceRegistration.unregister() , if I add the code below the service is unregisted. 好的,如果我在服务下方注册了代码,则找到ServiceRegistration.unregister() Which brings up the next question, how do I get the ServiceRegistration from another place where the service is registered? 哪个提出了下一个问题,如何从另一个注册服务的地方获取ServiceRegistration

registration.unregister();

serviceReference = bundleContext.getServiceReference(ITestService.class);
System.out.println("now the service is null: " + serviceReference);

Output: 输出:

should be null but is not: {unregister.osgiservice.ITestService}= service.id=174, service.bundleid=8, service.scope=singleton}
now the service is null: null

ungetService just drops the particular service reference, it does not unregister the service. ungetService只会删除特定的服务引用,不会取消注册服务。

registerService returns a ServiceRegistration which you can use to unregister the service: registerService返回一个ServiceRegistration ,您可以用来注销该服务:

ServiceRegistration<ITestService> reg = bundleContext.registerService(ITestService.class, new TestService(), null);

...

reg.unregister();

It is up to you to track the service registrations. 由您来跟踪服务注册。 Many plugins store them in the plugin Activator. 许多插件将它们存储在插件激活器中。 It is also common to register the service in the Activator start method and unregister in the stop method. 通常也可以在Activator start方法中注册服务,然后在stop方法中注销。

Despite the answer of greg above, I found this nasty workaround using internal classes: 尽管上面有greg的回答,我还是使用内部类发现了这种讨厌的解决方法:

((org.eclipse.osgi.internal.serviceregistry.ServiceReferenceImpl) serviceReference).getRegistration().unregister();

Sure this is bad, but I need a solution to get the ServiceRegistration . 当然这很糟糕,但是我需要一个解决方案来获取ServiceRegistration I can't store the ServiceRegistration on startup since I'm using Eclipse Gemini Blueprint (former Spring DM) to register my services. 由于我使用Eclipse Gemini Blueprint (以前的Spring DM)注册我的服务,因此无法在启动时存储ServiceRegistration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM