I need to call a method with bundle activator instance after every bundle is started. I have five bundles. How do I get the bundle activator instance? Requirement: Call a method only with the activator instance.
Asked
Active
Viewed 83 times
1 Answers
0
Look into using EventHandler (or perhaps BundleTracker depending on your use case). OSGi defines a FrameworkEvent that an EventHandler will receive. You could configure your second stage to trigger based on the 'STARTED' event.
- org.osgi.service.event.EventHandler
- org.osgi.framework.FrameworkEvent
alternatively: org.osgi.util.tracker.BundleTracker

Matt Pavlovich
- 4,087
- 1
- 9
- 17
-
Thanks! I need some clarification- I need to call a method with bundle activator instance after the BundleListener starts listening to the bundle. Is there any way to get the bundle activator instance after bundle listener starts. – Thalaimalai Pandiyan T May 25 '22 at 04:07
-
Does it *need* to be a Bundle Activator? Perhaps more predictable and easier to test if your Bundle exposes a service that has a second stage boot methods (ie.. processStartup()). That same service can be an EventHandler and invoke the processStartup() when the FrameworkEvent.STARTED arrives. – Matt Pavlovich May 25 '22 at 15:08
-
Hey Man! Thanks I got it but now i need another case. – Thalaimalai Pandiyan T May 26 '22 at 11:34
-
Hey Man! Thanks, I got it but now I need another one. In my project I have seven bundles in two different modules. I need to invoke a method after all bundle activator started and BundleListener finish listening all bundles. For ex: I have bundle with name S, P and Q so it is bundle S, bundle P and bundle Q. If bundle S to start its prerequisite bundle like bundle P, bundle Q activator also started and finish listening the bundle. How to invoke a method after every bundle started in my project. – Thalaimalai Pandiyan T May 26 '22 at 11:43
-
Exposing services instead of using Activators solves most of this. OSGi services are registered (and available to other services) once they have been instantiated (and optionally a successful call to an 'activate()' method). The services from other bundles can depend on each other. Those can get set by OSGi: serviceFromBundleP.setServiceFromBundleS(S service). You control state / lifecycle one the set/unset methods of the various services. This approach is much more dynamic and flexible. – Matt Pavlovich May 26 '22 at 13:23
-
Thanks, Man ! but in my use case, I am strictly advised not to use OSGi services(activate() method). Is there any other way to invoke a method after all the bundles started and the bundellistener finishes listening all the bundles – Thalaimalai Pandiyan T May 26 '22 at 16:12
-
Probably? Design questions like this goes outside the scope of reasonable ability to address as a question on SO. Would need more details of the scenario, the bundles, the interactions, etc. – Matt Pavlovich May 26 '22 at 18:10
-
In my project, all the tables and adding a new row in the tables in the database are done with the meta.xml file. The XML file is located in Bundle S. After bundle S started all the table definitions, add a new row, insert a new record in table are done. After that, they need to store the database fields in Cache but cache is in Bundle P. if we try to load cache in bundle P activator, it results in empty because all the tables are loaded in bundle S. So after all bundlelistener listens the bundle need to invoke a method to load cache in bundle P. – Thalaimalai Pandiyan T May 27 '22 at 04:34
-
This sounds more like a use case for event-driven architecture vs timing during bootstrapping. Look at using embedded JMS (ie ActiveMQ) or OSGi's EventAdmin. – Matt Pavlovich May 27 '22 at 17:27
-
I'm on it. Thank you for sharing your expertise. You have my gratitude. – Thalaimalai Pandiyan T May 28 '22 at 04:19