Expand my Community achievements bar.

Ranking Multiple Dynamic References in OSGi | AEM Community Blog Seeding

Avatar

Administrator

BlogImage.jpg

Ranking Multiple Dynamic References in OSGi by Juan Ayala

Abstract

In AEM we tend to develop custom OSGi services that do small units of work. For example, an OSGi service to send emails. Or a service that executes search queries. Sometimes we have to deal with many service instances at once. Factory configurations can create many instances of the same class. Or an interface can have many implementations.


Open the services console at http://localhost:4502/system/console/services. Use the filter (service.factoryPid=org.apache.sling.jcr.repoinit.RepositoryInitializer). There are several instances of this service each bound to a different configuration.


Use the filter (&(objectClass=javax.servlet.Servlet)(sling.servlet.paths=/bin/wcm/*)). These are all the Servlet interface implementations configured with a path prefix. See any you like?


What I See Most of the Time

This is typical of the sort of code I run into most of the time

final var context = FrameworkUtil.getBundle(this.getClass())
.getBundleContext();
final var serviceReferences = context.getServiceReferences(MyService.class, null);

for (final var serviceReference : serviceReferences) {

final var service = context.getService(serviceReference);
final var serviceId = serviceReference.getProperty("service.id");
// do something...
log.debug("{} ({})", service.getSomeProperty(), serviceId);
context.ungetService(serviceReference);
}
There is a lot of overhead to get the services. Unit testing may be a challenge. And I'm not sure it's thread-safe. But hey, it works!


Additionally, it will be up to you to sort the services. Usually by the service.ranking property.

Read Full Blog

Ranking Multiple Dynamic References in OSGi

Q&A

Please use this thread to ask the related questions.



Kautuk Sahni
0 Replies