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.
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 commit. Feel free to befriend me on Facebook, Twitter or Linked In or say Hi by email.
Happy Coding 😊 and Happy Learning 😊
Comments
Post a Comment