简体   繁体   English

OSGi:如何检测捆绑包何时注册到新服务

[英]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.

相关问题 如何检测bundle是否在osgi容器中完全加载? - how to detect if a bundle is completely loaded in osgi container? 如果OSGi捆绑包使用类加载器,如何检测它的运行时库? - How can I detect runtime libraries of an OSGi bundle if it uses classloader? OSGi:是否有处理服务和注册的“最佳方式”? - OSGi : Is there a 'best way' to handle service and registers? 如何使用OSGi Apache Felix在应用程序中调用Service Bundle - How to call service Bundle in your application with OSGi apache felix 如何在没有ClassCastException的情况下从另一个OSGi捆绑包公开服务 - How to expose a service from one OSGi bundle in another without ClassCastException 如何创建OSGI包? - How to create OSGI bundle? OSGi-如何获取新的捆绑包以通过现有的bndrun文件运行 - OSGi - How to get a new bundle to run via an existing bndrun file 使用Maven进行构建时如何在OSGI捆绑包中导入javax.smartcardio? - How to import javax.smartcardio in a osgi bundle when building with maven? 仅当满足特定条件时,如何激活 OSGI 包? - How to activate an OSGI bundle only when the certain condition is satisfied? 在Netbeans和Karaf中开发OSGI捆绑包时如何管理依赖项? - How to manage dependencies when developing OSGI bundle in Netbeans and Karaf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM