OSGi Applications that run in an enterprise environment are a great alternative to deploying the traditional EAR file. This series is going to run through an introduction to OSGi Applications for an enterprise developer, if you don’t know why you might want to do this then Ian Robinson has done an excellent explanation of the advantages of using OSGi Applications.

Each part of this series will build on the last to create an OSGi application that prints a Date from a servlet using OSGi services and dependency injection through Blueprint. Throughout I will be using the WebSphere Application Server Liberty Profile runtime and the OSGi application tools within Eclipse that come the WebSphere Developer Tools on the downloads page.

In this first part we will look at one of the features that running in an OSGi environment gives us: a managed lifecycle for an OSGi bundle. We’ll use this to print “Hello, World!” to the console within Liberty.

First though we need to create a bundle and an OSGi Application to contain it:

  1. Select the menu “File” -> “New” -> “OSGi Bundle Project”
  2. Set the properties:
    • Project name: “DateProvider”
    • Target runtime: “WebSphere Application Server V8.5 Liberty Profile”
    • Add bundle to application: checked
    • Application project: DatePrintingApp
  3. Click “Next” twice
  4. Set the properties:
    • Generate an activator: checked
    • Activator: com.ibm.example.Activator
  5. Click “Finish”

This will have created two projects for you, an OSGi bundle project named DateProvider (this is deployed as a JAR file) and an OSGi Application project named DatePrintingApp (this is deployed as an Enterprise Bundle Archive – EBA – file). The OSGi Application project just contains a single APPLICATION.MF file that stores information about the application such as it’s name and which bundles it contains but for now it is the bundle project we created that we are interested in.

When creating the bundle project we told the Eclipse tools to also create an activator class, this is a special OSGi class that informs the bundle about lifecycle events such as when the bundle is being started or stopped. More importantly for us, the Activator class also allows the bundle to interact with the framework when start and stop events happen. We are going to use this class to print “Hello, World!” when the bundle is started by doing the following:

  1. Open the com.ibm.example.Activator class
  2. Change the implementation to:

    package com.ibm.example;
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;

    public class Activator implements BundleActivator {

    public void start(BundleContext context) throws Exception {
    System.out.println(“Hello, World!”);

    public void stop(BundleContext context) throws Exception {


We are now ready to deploy the application into the Liberty Runtime. If you don’t already have a Liberty Server setup in this Eclipse Workspace (i.e. you do not have one in the “Servers” view of Eclipse) do the following:

  1. In the “Servers” view, right click and select “New” -> “Server”
  2. Select IBM -> WebSphere Application Server V8.5 Liberty Profile
  3. If you already have the Liberty Server runtime registered in Eclipse click Finish to add the default server to the list of Servers, otherwise click “Next”
  4. Click “Browse” and browse to where the runtime was extracted to selecting the “was4d” folder
  5. Click “OK” then “Finish”

Now we need to add the OSGi application to the server and start the server by doing the following:

  1. Drag the DatePrintingApp onto the server in the “Servers” view.  If you expand the “Server Configuration” under the Servers view you
    notice that the tools have noticed that you are running an OSGi
    Application and added support for it into the server by enabling the “blueprint-1.0” feature
  2. Click the green arrow in the “Servers” view to start the server

In the Console view you should see “Hello, World!” printed indicating that your bundle has started.

We’ve now created a very simple OSGi Application that uses the bundle lifecycle to print a message when the Liberty Runtime starts the application.

4 comments on"Getting Started with OSGi Applications: Bundle Lifecycle (Part 1)"

  1. How to set the rumtime to a JVM instance running in CICS instead of the liberty server?

  2. Yes very nice demo simple and to the point. I agree

  3. Very nice article, simple, concise and clear.

Join The Discussion

Your email address will not be published. Required fields are marked *