Reputation: 1676
I'm experimenting with OSGi to build a modular IoT unit. It seems like the perfect technology if one would like to update parts of the system ( e.g. sensor code ) without sacrificing communication code and vice-versa.
The question is though :
Why do I need to restart all dependent bundles when updating a service (inside a bundle )?
private void updateBundle(String f) {
BundleContext mainBc = felix.getBundleContext();
Bundle bundle = mainBc.getBundle(f);
try {
bundle.update();
// if I don't do this, the services inside 'bundle' will NOT be updated
PackageAdmin pa = mainBc.getService(mainBc.getServiceReference(PackageAdmin.class));
pa.refreshPackages(mainBc.getBundles());
// at this point all bundles were restarted
} catch (BundleException e) {
System.err.println("Error while updating " + f);
e.printStackTrace();
}
}
Judging by the fact that all my bundles were restarted ( or at least dependent ones ), what is the point of updating the class definitions in a bundle since all app bundles will be stopped
and started
resetting all state and active connections ?
It's exactly like restarting my app from the command line, so why would I need osgi?
Upvotes: 1
Views: 81
Reputation: 9374
It would seem the package holding the service interface class is exported by your service implementation bundle. So when the service implementation bundle is updated, so is the exported package. Thus all bundles consuming the service are using the old revision of the exported package unlike your service implementation bundle. Thus, you must refresh all those service consuming bundles to make sure they use the same revision of the exported package as your service implementation bundle.
This is why you want to export the package containing the service interface class from a different bundle than the service implementation bundle. Then all service consuming bundles and the service implementation bundle import the package from the exporting bundle. Then when you update the service implementation bundle, you do not need to refresh the service consuming bundles.
So, generally, you do not want a bundle which may be updated frequently to be exporting packages which must be imported by other bundles that care about the function of the updating bundle, such as a service.
Upvotes: 1