samedi 30 janvier 2010

Provide a Service thank to OSGi

Hi everybody !

In this article I demonstrate to you, how to create and deploy a service thank to OSGi.

But for the moment, we just focus on OSGi acronym and explanations on the subject.

OSGi : Open Services Gateway initiative

OSGi is considered as a framework, it's a modular system for Java in order to implements a complete and dynamic component model, something that does not exist in standalone Java/VM environments, unfortunately.

OSGi can be represented as a set of stacking bricks, applications or components. These things in OSGi context as often called "bundle", the main purpose of OSGi is to management bundle life cycle which can be you Java packages, classes provided as "services". The management life cycle can be done remotely and regroup five states (installed, started, stopped, updated, uninstalled), but the real challenge for OSGi is to perform the changement status without requiring a reboot.

For this article I choose Felix the OSGi implementation of The Apache Software Foundation, but several implementations are also available like Equinox from Eclipse, Knopflerfish OSGI, OSCAR ...

In this article, I create a bundle that implements and provide a People Access Service. First we need to define the service interface :

This interface define a function that allow to check the person existence in the world, by providing the firstname and lastname.

Take attention to the package aruffie.osgi.tutorial.service because it will has a really importance afterward. I do this because I need to share the service interface with other bundles, it is better to separate service interface that need to be shared from code that doesn't need to be shared. This OSGi approach provides a high separation between interface and service implementation.

In order to respect this approach I create the service implementation in other package, aruffie.osgi.tutorial.implementation:

Now the for model:

Now, I need to create the bundle activator class of my service. you can find the source code in

The main purpose of this activator is as its name indicates, activate my people access service into the Felix OSGi Context.

To finish I need to provide the linked that contains the bundle meta-data for its package, deployement ... (its must be provided with the linked service into the jar archive)

I precise the activator for my bundle thank to "Bundle-Activator", and specify the shared package by using "Export-Package".
For my bundle, I need to import the org.osgi.framework bundle dependency. Why ? because I use an activator in order to activate my service in OSGi context (need org.osgi.framework.BundleActivator)

To finish I package the jar with all classes and the manifest file.
The next step is to launch Felix with this command, in my Felix environment "java -jar bin\felix.jar":

When Felix running, deploy the People Access Service packaged into "osgitutorial.jar" with the following command line "start file:[your jar path]"

But wait, "start" aggregate "install" and "start" the bundle ... (but you can use install command, and after start)

Now your People Access Service is running into the Felix OSGi Context ...


2 commentaires:

  1. Il dépend de quel version du standard OSGi? R4? R4.1? R4.2?

  2. I think it's R4 OSGI standard version, but not sure for current Felix release. You can check on: