Monday, December 8, 2008

OSGi example = Spring Dynamic Modules + Equinox Declarative Services

Well, it took me a while to came up with that example.
As I am working on design of some fancy spring osgi equipped application some research was needed to check how and if it works as described ;).
But why not to make this more interesting and add some integration to this deliberations.


In this post I am going to show how to integrate Spring DM and OSGi DS (Equinox implementation). The only purpose for the code is to show that it is possible. It is only OSGi powered version of helloWorld software :D

What can be useful ?

Main idea

As it was late in the night usability of the example is not catchy :D "hello" here "hello there" but the main idea is to show that it works :D.
Ok so we have 3 bundles. Two of them are using declarative services and the last one is using spring to provide it services :D

Programmer notes

First thing to mention. To build spring bean I am using maven. As you all know there is something called maven project structure. The best place to store META-INF is resources directory in src catalog. Don't do it this way. To make bundle visible for PDE tooling we need to store META-INF in the root of the project. Of course this requires changes in pom.xml project description.

As we have it visible as a bundle let me add some content.
First our service:

package nugae.spring.example;
public interface IHelloService {
  void sayHello();

It has its implementation in nugae.spring.example.impl package.
Next we need to define our service definition. It is recommended to split osgi and beans definition.

I assume that you may know standard bean definition so I will only focus on the osgi definition (component-osgi.xml):

Basically, saying that we deliver some service can be done with this declaration:

<osgi:service id="springService" ref="helloService" interface="nugae.spring.example.IHelloService" />

At the moment (in theory) we have our service (and bundle) ready to deployment.

Lets move then to our declarative services. As I've mention we'll add consumer and another provider bundles.
It is worth to mention that in eclipse 3.5M3 we have DS tooling available in PDE.

Declaring service using DS is nothing more complicated than using spring approach.
Neither specifying services required.

So what we have here ?
We just specified that our bundle requires two services (one delivered by other DS bundle and second provided by Spring bundle.

So let bring it to life :D

To bring the DS functionality to our target platform we need some bundles (can be checked out from

The simplest way to run spring osgi support is to use target provided by spring framework.

It contains all required bundles.


It works. Well I am surprised cause sometimes not everything work as described ;).
I think that OSGi brought new quality to developing java application. The level of freedom we've using different approaches make it even more attractive.
If you want to run this example (hopefully you will be able following this post)
source codes can be downloaded from here.


Frederic Conrotte said...

Good tutorial. I'm about to commit as well this kind of framework within the Elcipse OFMP project, including traditional JEE services like transaction, logging and security.


Chris Aniszczyk (zx) said...

Cool, shows you the power of having one service registry that all these things interact with ;)

Bartek Michalik said...

indeed :D