Skip to main content

OSGi Deep Dive: Understanding BundleActivator Interface (Part 2)

Hello All! We dived into the basic concepts of OSGi in my previous post. Today we are going to look into more details and will work with the org.osgi.framework.BundleActivator interface.

We know that in OSGi, all service implementations are packaged into bundles. We can make a bundle lifecycle-aware by including a bundle activator to manage the lifecycle of the bundle. A bundle activator is nothing but a Java class that implements org.osgi.framework.BundleActivator interface and it is instantiated when a bundle is started.

In other words, a bundle gets the access of OSGi framework using a unique instance of BundleContext. But to get this unique instance, the class must implement the BundleActivator interface.

This interface has two methods start(BundleContext context) and stop(BundleContext context) and they are called when the bundle is started or stopped respectively.

Creating a Simple Bundle

Now, we will see the concepts in action via some code. We are going to create a bundle that will use the BundleContext to add itself as a listener for service events. Let's get started 😃
  • Create a simple AEM Multimodule project as described here.
  • Since we are going to create a bundle, we do not need any other modules apart from the core. Hence in the parent project's pom.xml, we can remove all modules except core.
  • Now create a new class Activator.java which will implement BundleActivator interface and implements start() and stop() methods.
  • You can see that we are also implementing the ServiceListener interface. This will have a method serviceChanged() which will listen to the various events in an OSGi service lifecycle.
  • The next important thing is to let the bundle know which is the bundle activator class. This can be done in the parent's pom.xml file as follows
Maven Bundle Plugin

  • After deploying, stop a bundle (so that the services get unregistered) and then restart it (so that the services get registered). I am stopping and starting the We.Retail bundle.
  • We will see following traces in the logs
  • Clearly you can see that services are getting registered and unregistered.

Conclusion

Congratulations!! 🙋 today we have created our first bundle using the BundleActivator interface and understood the concept of bundle's start() and stop() methods. I hope you enjoyed this post.

I have committed the code at GitHub in this commitFeel free to befriend me on FacebookTwitter or Linked In or say Hi by email.

Happy Coding 😊 and Happy Learning 😊

Comments

Popular posts from this blog

Day 00: AEM Developer Series

Hello everyone! Welcome to this AEM development series. We can all see the revolution of Digital Marketing today. Companies are dying to be a part of it and they have made this a war for the Digital Marketing tools.
Adobe is way ahead in this war and has gained a lot of market capture. They are leaders in the Digital Marketing platforms since 2014-15. One of the flagship product in Adobe's Digital Marketing suite is Adobe Experience Manager (AEM).
Since AEM is in huge demand, the people who know how to develop on AEM are also in huge demand. But developing on AEM is not easy as it is made up of various open-source technologies such as Apache Felix (OSGi), Apache Sling, Apache Oak and Adobe's own technologies like Granite, HTL etc. Learning all these technologies in conjunction can sometimes become confusing and frustrating 😫.
When I first started learning AEM in 2016, I was dumbfounded to see there is so much going on under the hood. I then spent months to gather all the res…

Day 01: Introduction to AEM

Day 04: Developing first OSGi bundle