DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. OSGi: How to Handle the (Wrong) Bundle's Startup Order

OSGi: How to Handle the (Wrong) Bundle's Startup Order

Let's take a look at how to handle the (wrong) bundle's startup order as well as look at two solutions, such as modifying the bundle's start level.

Adrian CITU user avatar by
Adrian CITU
·
Aug. 23, 18 · Tutorial
Like (1)
Save
Tweet
Share
8.24K Views

Join the DZone community and get the full member experience.

Join For Free

Context

In some situations, it is needed that some of the OSGi (formerly known as the Open Services Gateway initiative) bundles are started in a specific order.

A concrete case is when the Apache Camel is used in the context of the OSGi. If one of the OSGi bundles is a user-defined Apache Camel component and another bundle uses this user-defined Camel component into a (Camel) route, then the bundle containing the Camel component should be started before the bundle that is using the Camel component.

Solution: Modify the Bundle's Start Level

The first solution would be to modify the start level of the bundle that you want to start later.

Apache Felix offers the "bundlelevel" command:

bundlelevel - set bundle start level or initial bundle start level
scope.
flags:
-i, --setinitial set the initial bundle start level
-s, --setlevel set the bundle's start level

So something like:

 bundlelevel -s newStartLevel bundleId

will do the trick.

The advantage of this solution is that you do not need any programming skills to do it and you can apply it on any bundle (even on the bundles that you are not controlling the content).

The drawback of this solution is that it is totally manual (at least in case of Apache Felix server).

The OSGi specification also defines the OSGi Start Level API, which provides the following functions:

  • Controls the beginning start level of the OSGi Framework.
  • Is used to modify the active start level of the Framework.
  • Can be used to assign a specific start level to a bundle.
  • Can set the initial start level for newly installed bundles.

Using the OSGi Start Level API it is possible to programmatically set the start level:

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

Solution: Use a BundleListener

The basic idea is that the bundle B (then needs the bundle A to be active) will wait until the bundle A is marked as started. This can be achieved by implementing a BundleListener to the level of bundle B.

The implementation of the "bundleChanged" method of the listener will look like this:

public void bundleChanged(BundleEvent bundleEvent) {
    String symbolicName = bundleEvent.getBundle().getSymbolicName();
    int eventType = bundleEvent.getType();

    if ("The Bundle A Symbolic Name".equals(symbolicName)
        && BundleEvent.STARTED == eventType) {
        //here we know that bundle A is started
        //so can do something that will need
        //bundle A
     }
}

The advantage of this approach is that the bundle developer is in control of the behavior. On the other side, this approach will not work if you do not own the code of the bundle that you want to start later.

Apache Felix Apache Camel Advantage (cryptography) Framework API Initiative Command (computing) Implementation

Published at DZone with permission of Adrian CITU, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Data Mesh vs. Data Fabric: A Tale of Two New Data Paradigms
  • Why You Should Automate Code Reviews
  • How To Generate Code Coverage Report Using JaCoCo-Maven Plugin
  • Using the PostgreSQL Pager With MariaDB Xpand

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: