简体   繁体   中英

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(...).

Is there a listener like FrameworkEvent.STARTED or something?

Thanks.

Listening to service changes is very common in OSGi. The plain API way is to use a 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.

This is how to listen for all services by an interface using DS. See also the javadoc of @Reference .

@Reference(unbind="unbind"
public bind(MyService my) {...}

public unbind(MyService my) {...}

You can register ServiceListener via BundleContext#addServiceListener.

For real-world example look how Gemini Blueprint framework works with service listeners: OsgiServiceCollection . There is OsgiServiceCollection$BaseListener listener implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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