[英]OSGi: How to detect when a bundle registers to a new service
I would like to know when a bundle in the environment registers a service using context.registerService(...). 我想知道环境中的捆绑包何时使用context.registerService(...)注册服务。
Is there a listener like FrameworkEvent.STARTED or something? 是否有诸如FrameworkEvent.STARTED之类的侦听器?
Thanks. 谢谢。
Listening to service changes is very common in OSGi. 侦听服务更改在OSGi中非常常见。 The plain API way is to use a ServiceTracker . 普通的API方式是使用ServiceTracker 。 You can specify which services you are interested in and will get callbacks when such a service is registered or unregistered. 您可以指定您感兴趣的服务,并在注册或取消注册该服务时获得回调。
The recommended way is to use frameworks like declarative services (DS) or blueprint which also offer ways to listen for services. 推荐的方法是使用声明性服务(DS)或蓝图之类的框架,这些框架也提供侦听服务的方式。
This is how to listen for all services by an interface using DS. 这是使用DS通过接口侦听所有服务的方法。 See also the javadoc of @Reference . 另请参阅@Reference的javadoc。
@Reference(unbind="unbind"
public bind(MyService my) {...}
public unbind(MyService my) {...}
You can register ServiceListener via BundleContext#addServiceListener. 您可以通过BundleContext#addServiceListener注册ServiceListener 。
For real-world example look how Gemini Blueprint framework works with service listeners: OsgiServiceCollection . 对于实际示例,请查看Gemini Blueprint框架如何与服务侦听器一起使用: OsgiServiceCollection 。 There is OsgiServiceCollection$BaseListener listener implementation. 有OsgiServiceCollection $ BaseListener侦听器实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.