Вы находитесь на странице: 1из 10

OSGI Tutorial - HelloWorld Program

Open Source Gateway Interface (OSGI) framework is meant for java programming language to build the
application in the form of several modules and to control them dynamically. It maintains the whole
lifecycle (start, stop, destroy etc) of those components. It has good integration with Spring framework
called Spring-OSGI, where you can define the OSGI services as the spring services in the context
configuration file. Eclipse, could be the best example for OSGI implementation. In 2004, Eclipse 3.0
released with the OSGI integration. In OSGI, you have to define your services and also need to take care
about the connection between those services and your application. You can install, uninstall any module
at the runtime and it allows containing more than one version of a module.
It provides two basic features one is service platform and deployment platform. Service platform
usually allows building all your services and registering them in the registry. These services can be
accessed across the modules depending upon the source module configurations. Bundles contain the
service provider and service requesters. The deployment platform allows you to install, uninstall,
activate, deactivate, start and stop a service. These deployment functionalities can be accessed via osgi
command prompt. Each bundle usually has a manifest file which has the description of the activator of
the bundle. This activator control the starting and stopping the service.
In this tutorial, I will explain how to create the OSGI bundles and how one service defined in one bundle
can be used in other one. It includes importation and exportation of services in OSGI framework.

First of all, Open the eclipse and create a new Project by File -> New -> Project. You will get a wizard to
select the type of project.

1st July 2011

Amit Raj

araj@cs.tcd.ie

Now select the Plug-in project and press next. Enter the project name as com.osgi.amitraj.HelloWorld
and Select the Target Platform as an OSGI Framework - Quinox. We will use the quinox framework for
osgi development.

1st July 2011

Amit Raj

araj@cs.tcd.ie

Then press next button. You will get the following screen to edit some more properties if you want.

1st July 2011

Amit Raj

araj@cs.tcd.ie

Then press next. Then you will see a screen to select the template to create an OSGI bundle. Select the
Hello OSGI Bundle and press finish button.

1st July 2011

Amit Raj

araj@cs.tcd.ie

After pressing the finish button, the osgi bundle will be ready and available in project explorer. Double
click on the Manifest.mf file will look like this:

1st July 2011

Amit Raj

araj@cs.tcd.ie

You can see this manifest file in the text format as well as shown below:

1st July 2011

Amit Raj

araj@cs.tcd.ie

Here, I will explain some of the basic terms of this manifest file.
Bundle-ManifestVersion: It tell to use the OSGI version.
Bundle-Name: Some human readable name for the bundle.
Bundle-SymbolicName: Fully qualified name of your bundle.
Bundle-Activator: Activator class of the bundle which has the method like start and stop.
Bundle-Vendor: Human Readable vendor name.
Import-Package: The package imported by this bundle.

1st July 2011

Amit Raj

araj@cs.tcd.ie

This is the bundle activator class which is instantiate on the OSGI framework. It contains two method
to control the life cycle. Start method is called when the bundle is started and stop method is called
when the bundle is stopped. These methods can be used in the scenarios like at the start of the
bundle, keep some open connections of a database and when the bundle stop, flush all the database
connections.
Now I will show how to run this simple example. Go to the Run and select the Run configurations.
Run-> Run Configurations. Double click on OSGI Framework will create a new OSGI Framework Run
Configuration. Then select your bundle in workspace and org.eclipse.osgi in the Target Platform like
below:

1st July 2011

Amit Raj

araj@cs.tcd.ie

Then press Apply and Run button. It will start the osgi console and you can see the bundle starting
message as shown in the below snapshot.

1st July 2011

Amit Raj

araj@cs.tcd.ie

And your first OSGI application is ready.


This was very easy but the complications starts when you have many bundles and you want to access
other bundles services. In that case, you have to import the services from other bundles.

1st July 2011

Amit Raj

araj@cs.tcd.ie

Вам также может понравиться