Search code examples
osgiapache-karafosgi-bundle

OSGI deployment Order


I have some bundles [Both OSGI and Non OSGI] in my karaf deploy directory. The start order if varies every-time when I start the karaf. I wanna have standard orders every-time. I'm seeing some ways set start level from OSGI app, But I need any way at karaf container level. Is there anyway to set it?

Not from code like,

Bundle bundle = framework.getBundleContext().installBundle(location);
BundleStartLevel bundleStartLevel = bundle.adapt(BundleStartLevel.class);
bundleStartLevel.setStartLevel(xxx);

Solution

  • You should try hard to avoid using start levels. There are very few cases where start levels actually make sense but they definitely should not be used to craft "standard orders every-time". If your application needs to start bundles in particular order it is (again with few exceptions) a sign of bad design.

    Keep in mind that even if you decide to use start levels, the order in which bundles within the same start level are started will still vary. So to get "fixed" start order you would have to assign each bundle to its own start level. While doable, that would be a nightmare to maintain.

    What you should do instead is

    • Use services as much as you can. Services have own lifecycle and can be started/stoped somewhat independently from the bundle lifecycle.
    • Use Requirements to express the fact that bundle needs something. Use Capabilities to express the fact that bundle provides something. This way a bundle will not be resolved unless the requirement is satisfied (by another bundle in this case) which effectively ensures one has to start before the other.

    If you decide to ignore all that and still go with start levels, probably the easiest thing to do in Karaf's case is to have a feature with all your bundles in it where you can provide start level for each bundle.