How to refresh OSGI R6 components on bundle activation

Recently, I had the need to wrap the default implementation for AEM’s mail service in a new service called CustomMailService where I wanted to transform certain emails and let other emails be handled by the default service.

My main interest was for the AccountManagementService to bind my implementation of the MailService, instead the default implementation, you can achieve this by adding the service.ranking property and giving it an integer higher than the default implementation:

But I hit a problem

The problem is, the AccountManagementService refuses to bind my CustomMailService unless I refreshed AccountManagementService. The reason is that the AccountManagementService has a static reference to MailService which means that adding a new MailService in OSGI would not cause AccountManagementService to look for a “better” service (higher ranking). See the OSGI spec for more details

I realize this is version 7 of the OSGI spec, but the same information is true for OSGI 6, you could download version 6 of the spec here

Now what?

Well, we could use a Bundle Activator and write some code to stop, then start the AccountManagementService. And do that we will!

First, we write a general util class to enable/disable components by providing the service PID, the bundle context and the ServiceComponentRuntime service.

and voila! the AccountManagementService is now refreshed every time this bundle is deployed! You could also write some code to check if the custom mail service really got bond to the AccountManagementService.

Partners

Industries

Follow Us

Contributors

Perficient Digital

We architect systems of customer delight. We move at the speed of culture. We exist to delight our clients, by relentlessly delighting their customers. We are what happens when more than 200 of the most talented digital strategists, design pros, brand shapers, digital marketers, data scientists, and creative coders all believe one thing to be true – exceptional customer experiences always translate to positive ROI for our clients.