3

Assuming I use OSGi Declarative Services and I have a service that have a number references with policy = dynamic...

A - Mandatory unary.

B - Mandatory unary.

C - Mandatory multiple.

D - Optional unary.

E - Optional multiple.

All references are available when my service starts. Is there any way to control what order bind is called in?

I'd like to have B bind first and do something to each E that comes in, but I have no way of ensuring that B is bound before E.

Yes, the more logical approach would be to let the service that represents B also bind to E and do whatever it should do, but I can't modify B, I can only use it. If I make a new service that just binds to B and E I'll have the same problem still.

I could do whatever I need to do in the activate method when everything is bound, and then do it as additional (dynamic) E's are bound, but I was wondering if there is another way...

MarcB
  • 549
  • 6
  • 14

1 Answers1

4

Make reference B use the "static" policy, which ensures it will be bound before the activate method is invoked.

If reference E is declared as multiple/optional with the "dynamic" policy -- which is pretty much the only reasonable choice when you have a multiple reference -- then it will be bound/unbound whenever the services are published/unpublished. This can happen from any thread, and can even happen (multiple times!) during the invocation of the activate method.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • Ah, that would work. Thanks! I'm guessing mandatory_multiple in combination with policy static is one of those "do not use, ever" things then? – MarcB Jul 11 '13 at 06:49
  • Essentially yes. With static policy, DS will have to destroy and recreate your component each time a service comes or goes away. In fact for this reason it will never tell you about new services arriving later, unless you use the new "greedy" policy option added in DS 1.2. – Neil Bartlett Jul 11 '13 at 06:53
  • 1
    My observation over felix is that even with B using "static" policy and E using "multiple/optional with dynamic policy", the order in which the @Reference annotations are placed in the class (and the tags in the service descriptor) - matters. If reference for E appears before B, E will get binded first – mdzh Dec 15 '16 at 14:55
  • @mdzh That doesn't contradict what I said: the static policy fields are bound before the activate is invoked. – Neil Bartlett Dec 15 '16 at 19:58