OSGi: How to detect when a bundle registers to a new service

401 views Asked by At

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.

2

There are 2 answers

0
Viktor On

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.

0
Christian Schneider On

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) {...}