0

Consider the following class:

@Service(MyTest.class)
public class MyTest{
     @Reference
     ExternalService externalService;

     @Activate
     public void activate(){
        externalService.someMethod();
     } 
}

Does OSGi guarantee that externalService will already activated before invocation inside the activate() method of class MyTest?

seh
  • 14,999
  • 2
  • 48
  • 58
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

2 Answers2

0

ExternalService is not a component but an OSGi service. The OSGi service might have been registered by another component.

If the service is registered by the other component automatically (in your example with an @Service annotation), the activate method of the other component must have been called before the service was registered. However, if you take a breakpoint into your activate method, it might happen that you see the other component on the webconsole with unsatisfied state. The reason is that the activate method of MyTest is called synchronously and the state flag on the other component is not changed by DS yet. In short, the Activate method of the component finished when the Activate method of your component is called even if you see that the state of the other component is unsatisfied.

If the service is registered programmatically from the Activate method of the other component, it might happen that your activate method is called synchronously. In that case, if there is more logic in the activate method of the other component after registering the service, it is not yet initialized.

It is also possible that the OSGi service was registered by other technology or manually from the Activator of another bundle.

Balazs Zsoldos
  • 6,036
  • 2
  • 23
  • 31
0

It would help to see the component XML generated from the annotations. The Service annotation is not an OSGi Declarative Services standard annotation.

But in general, all non-optional policy="static" references must be set before the activate method is called. An optional reference may be unset.

BJ Hargrave
  • 9,324
  • 1
  • 19
  • 27