Thursday, July 10, 2008

Set up Felix OSGi container with Maven and Spring in 20 mins

Recently I had a chance to try out the Apache Felix an open source implementation of OSGi R4 Service Platform and found there is not a lot of document regarding how to setup your development environment for OSGi, that's why I decided to record some of my finding and learning experience here, and hopefully will shed some light on this issue.

My goal, when I started this exercise, is firstly to use Maven 2 as the build management tool so I can setup an OSGi project just like any other Java project plus easy integration with any continuous integration tools out there. Second, I wanted to setup Spring as the micro container to manage all the wiring and all the neat aspect oriented programming stuff. Personally at the beginning I did not know exactly how well Felix and Spring will mix together, but roughly I had the idea to use OSGi for service level dynamic module management and Spring for lower level wiring. Ok, enough intro lets do some coding.

a. Download Felix binary from
Apache Felix

b. Start Felix by running 'java -jar bin/felix.jar', and just type any name for the profile name
Note: running java -jar in bin folder directly will not work

c. Type ps in Felix shell will show you a list of bundles that are already loaded. Now type shutdown to stop Felix.

d. After some research and trial-and-error, I found the Maven spring-osgi-bundle-archetype is the best fit I can find as a starting place for my little project. Type 'mvn archetype:generate' and pick 32 for spring-osgi-bundle-archetype.
Note: I am using maven 1.0.9

e. Run 'mvn clean install' will actually produce a valid OSGi bundle already without any coding. Try it out.

f. Restart Felix and in the shell type 'install file:path-to-your-bundle-file' should install the bundle you just created, yes its that simple, use 'ps' to check it out. Now you can start or stop the bundle.

g. Lets implement the Activator tutorial from Felix with our newly setup Maven project. See code sample and explanation here. Now if you do another 'mvn clean install' and run 'update #bundel-number' in Felix and expect to see something, you won't. Why? Because we haven't configure the Activator class as a bundle activator. To do that you need to first remove the auto generated Felix plug-in version number 1.0.0 from your pom.xml, since the old 1.0.0 plug-in does not support this configuration. After removing the version number, you need to add


your.package.Activator


under plug-in configuration.

h. Now run 'mvn clean install' again, then type 'update #bundle-number' in Felix shell, now whenever you start or stop the bundle you will see the log message got printed on the screen.


As you probably already noticed that the Maven project has already Spring configured for you, so you are pretty much all set at this point to start developing a OSGi application. Alright hopefully this setup did not take you more than 20 mins, unless you have a super slow internet connection ;-) Last but not least, you can also integrate Felix inside your Eclipse IDE for debugging and profiling purposes, see http://felix.apache.org/site/integration-of-felix-inside-eclipse.html for more details.

Have fun with OSGi and feel free to let me know your experience trying this setup out.

2 comments:

David Dossot said...

The trickiest thing I had to do so far with Felix was to access OSGI services from standard Java code. This thread helped me a lot achieving this.

I have found that using Knopflerfish to validate the behavior of my OSGI services was also very useful!

Nick Zhu said...

Thanks for the pointer David, that is some nasty class loading issue.