How do you handle service dependencies in OSGi if a required service is not available during activation? | Community
Skip to main content
Level 2
December 5, 2023
Solved

How do you handle service dependencies in OSGi if a required service is not available during activation?

  • December 5, 2023
  • 1 reply
  • 883 views

I was going through a video on OSGi Services, where someone explained that one of the benefits is "You can provide a service, if resource is avalilable".

They cited an example of Printer. That is Printer is available, then Service is also available.

Wondering how this is used in context of AEM services

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by aanchal-sikka

@aniketsi 

 

OSGi allows for the dynamic registration and consumption of services within the system, serving as a central registry for various services.

 

Static: A common way to access a service is through the use of the @Reference annotation. For example, consider the following code snippet:

 

 
@Reference
SlingRepository repo;

In this case, the code establishes a static reference to the SlingRepository service, granting access to the JCR. The service becomes active only when this reference can be resolved. 

 

Dynamic: there are scenarios where waiting for a reference to be resolved is not ideal, perhaps to provide additional value when a certain (optional) service is available. In such cases, a dynamic reference can be established:

 

@Reference(policy = ReferencePolicy.DYNAMIC)
PrintService printService

By setting the policy to ReferencePolicy.DYNAMIC, a service might become active before the referenced SlingRepository service is available.

 

This flexibility allows developers to manage dependencies more granularly and adapt their components' behavior based on the availability of optional services, providing a more dynamic and responsive architecture.

1 reply

aanchal-sikka
Community Advisor
aanchal-sikkaCommunity AdvisorAccepted solution
Community Advisor
December 5, 2023

@aniketsi 

 

OSGi allows for the dynamic registration and consumption of services within the system, serving as a central registry for various services.

 

Static: A common way to access a service is through the use of the @Reference annotation. For example, consider the following code snippet:

 

 
@Reference
SlingRepository repo;

In this case, the code establishes a static reference to the SlingRepository service, granting access to the JCR. The service becomes active only when this reference can be resolved. 

 

Dynamic: there are scenarios where waiting for a reference to be resolved is not ideal, perhaps to provide additional value when a certain (optional) service is available. In such cases, a dynamic reference can be established:

 

@Reference(policy = ReferencePolicy.DYNAMIC)
PrintService printService

By setting the policy to ReferencePolicy.DYNAMIC, a service might become active before the referenced SlingRepository service is available.

 

This flexibility allows developers to manage dependencies more granularly and adapt their components' behavior based on the availability of optional services, providing a more dynamic and responsive architecture.

Aanchal Sikka
joerghoh
Adobe Employee
Adobe Employee
December 5, 2023

This is not correct.

 

@3214626(policy = ReferencePolicy.DYNAMIC) PrintService printService;

will do a rebind if a better matching PrintService will get available (that means one with a higher service ranking). But the service which has that reference will only get active, if a PrintService can be bound.

 

To make your service start, irrespective if a PrintService is available or not, you need to make the reference optional. The easiest way (and my preferred one) looks like this:

@3214626 Optional<PrintService> printService;

 

(the annotation also supports a parameter "optional", but I prefer the support of the Java typesystem here.)