3
votes

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

1

1 Answers

4
votes

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.