3

If I have a service A which is referenced by some other component (called B), at which point will B's reference to A become null?

-Before the deactivate method of A is called?

-After the deactivate method of A is called, finished?

-After the instance of the object is destroyed?

I went through the compendium spec but I could not find details about this. I would be thankful if someone could clarify it for me.

Also, when a service A is activated when is it injected as reference to B? before or after the activate method?

santiagozky
  • 2,519
  • 22
  • 31

2 Answers2

6

There are two answers to this question, depending on whether you are using the static or dynamic policy for references. I'll cover static policy first.

During activation, the static policy guarantees that all bind methods that are going to be invoked are invoked before the activate method. Note that if any references are optional then the bind might not be activated at all. But DS enforces a happens-before relationship (in the terms of the Java Memory Model JSR133) so that the activate method can safely rely on the values of any fields set during the bind methods of static policy references, without explicit synchronization.

With the static policy, the component must be deactivated if any of the bound services becomes unavailable. Again DS guarantees that the deactivate method is invoked and completed before any unbind methods are called. (Note that there is rarely any need to implement unbind methods for static references... if your component has any cleaning up to do then it's easier to do it all in the deactivate method).

Now, things get more complex with the dynamic policy. For dynamic+mandatory references there is still a guarantee that the bind method has been called before the activate method starts, however after that all bets are off. It's actually possible for the bind/unbind to be invoked (even many times!) during the execution of the activate method.

Also be sure that you refer to section 112.5.10 ("bound service replacement") which describes the order in which bind/unbind are invoked. It can be unintuitive at first — the bind of the new service is called before the unbind of the old service — but when you think about it makes perfect sense since it guarantees that a mandatory reference is never bound to null.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • thanks Neil. that is quite useful. Just one question to be clear. It seems you talk about references of services in the component being activated instead of references *to* the component being activated. ie. if B has a dynamic and optional reference to A, when will B get or loose access to A? – santiagozky May 05 '15 at 08:31
  • If B has a dynamic/optional ref to A, then B will "get" (bind) to A when the A service is published, and unbind when the A service is unpublished. From B's point of view, there is no actual necessity for A to be another DS component, it's just an OSGi Service. However if A *is* implemented as a DS component, then its service will be published after it is activated. – Neil Bartlett May 05 '15 at 13:40
1

As far as I know the guarantee you get is that the references are removed after the deactivate method is finished.

Christian Schneider
  • 19,420
  • 2
  • 39
  • 64