I am experiencing problems with the order components are loaded when using OSGi declaratives services through Karaf.
I have this situation:
@Component
public class A implements IA
{
doSomething() {...}
}
@Component
public class B implements IB
{}
@Component
public class C implements IC
{
@Reference
IA a
@Reference
(cardinality = ReferenceCardinality.MULTIPLE,
policyOption = ReferencePolicyOption.GREEDY,
unbind = "doUnRegister" )
void doRegister(IB b)
{
a.doSomething()
}
void doUnregister(IB b)
{
...
}
}
A, B, and C are three distinct bundles.
When firing up Karaf, a B is registered and doRegister is called. However: service A is not ready (a is null).
I tried the following:
- set the start level of A to something lower than B... Did not work
- to pickup the registrations of B in a work-list and actually use A later when C was activated. Did not work AND the code was cluttered.
- searched for a way to write this requirement through the annotation on doRegister - NOT possible.
- I tried to use a service locator and get the context through an activate method on C - DID NOT WORK, it crashed Karaf.
I must clearly be missing something, is there anybody that have experienced similar problems and found a solution?
UPDATE: Reference A a changed into IA a. Added forgotten information on Reference B().