Let’s say I have a Bundle A that has a component B, which is not part of the Export-Package. The component will schedule itself to run periodically once activated. The scheduler is provided by another bundle.
My question is, if I uninstall Bundle A while component B is being run by a thread in the thread pool of the scheduler, what will be the implication? Will the thread continue to run? or will the thread throw exceptions?
The bundle has a life-cycle. When a bundle is active it will be stopped before it will be uninstalled. If “component B” is a declarative service component it will be stopped as well. If not then at least the stop method in your bundle activator will be triggered.
During stop you (as the provider of component B) are responsible for un-scheduling the component from a schedule so that it will be removed from the thread pool. If the thread is currently running then you should implement a cancelling functionality so that it can be aborted.
If you don’t properly cleanup (i.e. un-schedule/cancel the thread) then it may happen that the thread will be executed again. If you access OSGi API from within that thread then you’ll get IllegaleStateException (for some API usage because the bundle’s BundleContext becomes invalid after being stopped). However, if not then you threads might continue to run forever. Objects and memory won’t be released. The classes and the bundle class loader won’t be garbage collected. Therefore, it’s important that you implement “stop” properly.