1

I have a DS-service consumer of the IFoo interface:

@Component
public class IFooListener {

    @Reference(bind = "bind",
               unbind = "unbind",
               referenceInterface = IFoo.class,
               cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE,
               policy = ReferencePolicy.DYNAMIC)
    public static final Map<String, IFoo> allServices = new ConcurrentHashMap<>();

    protected void bind(IFoo service, Map<String, String> properties) {
    ....
    }
    ....
}

I have the IFoo service registered like that:

BundleContext ctx = FrameworkUtil.getBundle(IFooListener.class).getBundleContext();
Properties properties = new Properties();
....
ServiceRegistration managementSrv = ctx.registerService(IFoo.class.getName(), iFooImpl, properties);

What I would like to know is, is it guaranteed that when the ctx.registerService(...) method returns, all DS consumers that were already available at that time would have been notified that an IFoo service has become registered? Is this osgi-implementation specific thing? Or is this part of the DS specification?

mdzh
  • 1,030
  • 2
  • 17
  • 34
  • You should indicate which annotations you use since there are several. These are definitely not the standard OSGi annotations. And as an answer, it is very bad practice in a system to rely in such guarantees. – Peter Kriens Sep 09 '16 at 19:55

1 Answers1

1

I could not find an exact answer in the specification. However, I have just checked the code of Felix SCR and I saw that it did not open a new thread. It implements ServiceListener interface (as it always has to be in the deepest level) and luckily the javadoc of ServiceListener says that the addingService is called synchronously.

In short: The bind method is called synchronously in Felix SCR.

Balazs Zsoldos
  • 6,036
  • 2
  • 23
  • 31
  • Ok, so SCR implements ServiceListener and according to the OSGi specification "When a ServiceEvent is fired, it is synchronously delivered to a ServiceListener. ". So in a certain way, we are sure that when ctx.registerService() returns, the SCR had been notified. But does that mean that SCR has notified all managed components? I didn't find this in the specification, so I guess this is implementation specific. You are correct, it is true for Felix at least for now. But maybe in the future Felix devs decide to notify components in an async matter? – mdzh Sep 09 '16 at 10:22
  • Yes. That is what I meant. Now Felix SCR does not start a new thread. If I were you, I would use a ServiceTracker. I have never felt the necessity of using multiple-dynamic references of DS as it is basically a ServiceTracker. That is why [ECM][1] does not have such reference type. [1]: http://www.everit.org/ecm/index.html – Balazs Zsoldos Sep 09 '16 at 13:43
  • 1
    In other words: NO IT IS NOT GUARANTEED. – Neil Bartlett Sep 09 '16 at 22:11