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
Solved! Go to Solution.
Views
Replies
Total Likes
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.
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.
This is not correct.
@reference(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:
@reference
Optional<PrintService> printService;
(the annotation also supports a parameter "optional", but I prefer the support of the Java typesystem here.)
Views
Replies
Total Likes