How Apache Karaf sorts bundles to install and star

2019-05-11 20:14发布

问题:

I have defined four bundles:

bundle 1 : export package x version 1
bundle 2 : import package x ver [1,2] and export package y;y uses x    
bundle 3 : export package x version 2
bundle 4 : import package y and also import package x version 2

Using apache felix (distribuable binary), I found that I should manually impose to not resolve (or start) the bundle 1 and then bundle 2 before starting the bundle 3 (otherwise a uses constraint problem appears because bundle 2 will use package x version 1 and in bundle 4 will appear package x version 1 and version 2 --> uses constraint violation).
Thanks to the authors of these posts:
http://njbartlett.name/2011/02/09/uses-constraints.html
http://blog.springsource.com/2008/10/20/understanding-the-osgi-uses-directive/
I don't like to impose order to my bundles, I need to copy all my bundles in the /bundle directory and then the instance of framework install and start them. I noticed that Apache felix sorts the bundles to be installed alphabetically (so bundle 1 will be installed and then started the first). I tried with Apache karaf, I copied my bundles into /deploy and I found that the problem disappears, so my question is:

Does Apache Karaf, (or felix file install) apply a strategy to impose any order for starting bundles in order to avoid these kind of problems ?

回答1:

You should try to use a Karaf feature for this kind of deployment. You create a feature file with one feature in it and add all the bundles to this feature. Karaf will then load all the bundles and resolve and start them in one pass. So the resolver should bee able to correctly resolve all your bundles.



回答2:

Apache Karaf does automatically add a startlevel to the bundles in the deploy folder this is configurable. The default of it is 50. So all your custom bundles are installed as StartLevel 50. This also makes sure the basic bundles of karaf itself are already up and running, especially the file-installer bundle.



回答3:

Felix FileInstall does not have any ordering capability.

Start order isn't really important (bundles should be able to be started in any order), but a good Management Agent should be able to install and resolve a batch of bundles as a single operation. However FileInstall installs/resolves/starts bundles whenever it happens to poll the filesystem directory. Therefore FileInstall is not really usable for production deployment.

I don't know anything about Karaf, but any management agent that simply polls a directory is likely to have the same problem.