Accessing an OSGi service from a non-OSGi client

June 10, 2008

I warmed up to the idea of OSGi after reading an introduction like : “Use OSGi to service enable your JVM”. For background, we at MindTree have created a SOA based J2EE delivery platform built on open source technologies – primarily Spring. In this framework, we expose POJOs as services using Spring remoting and access them via the Mule ESB. The idea of being able to expose POJOs as services without having to remote enable them made OSGi an interesting proposition.

I chose Spring Dynamic Modules(Spring DM) for three reasons:

  • I hoped to leverage all of Spring’s capabilities while writing my service
  • Ability to manage my service POJOs as Spring beans
  • Easy migration path for our framework

My test was to write a POJO, declare it as a Spring bean and an OSGi service (using Spring DM) and eventually access the service from a standalone application. This completes a typical usage scenario. I used the “simple-service” sample code that comes with the Spring DM distribution (version 1.1.0).

I faced issues either because I was not able to find the right documentation or because it was simply not there. I encountered and eventually overcame the following issues:

  • Ability to launch an OSGi container in standalone mode and without the prompts – most samples on the internet showed me how to bring up Equinox with the “osgi>” prompt. This was easily solved though.
  • Ability to load all the required OSGi bundles *before* I could load my bundle
  • Look up a published service from the bundle I deployed and invoke it from my standalone code

A few lessons learned from this exercise are:

  • OSGi is very strict when it comes to class visibility and loading. Parent class loader delegation does not apply – something we are very used to in J2SE and J2EE applications
  • It is easy to share services and packages between bundles. Not all that easy if you want to share between a bundle and a non-OSGi client application. For e.g, you cannot simply type-cast a service you have looked up to an interface that is packaged in the bundle and available on your local classpath.
  • Boot path delegation (creating fragment bundles i.e extensions to the system bundle) can help address the above need but must be used selectively and carefully in order to preserve the benefits of OSGi.
  • All bundles are loaded asynchronously. You need to account for this before you look up any service.

I have replicated below the manifest contents from my fragment bundle (one that exports the common interfaces & classes) and the service bundle (one that imports the common interfaces and implements the service):

Fragment manifest:

——————————————————–

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: m1000350
Build-Jdk: 1.5.0
Bundle-Version: 1.0
Bundle-SymbolicName: org.springframework.osgi.samples.simpleservice.boot
Bundle-Name: Simple-Service-Sample-boot
Bundle-Vendor: Spring Framework
Fragment-Host: system.bundle <— note this extension
Export-Package: org.springframework.osgi.samples.simpleservice <— note this export
Bundle-ManifestVersion: 2

———————————————————

The manifest for my service bundle looks like:

——————————————————–

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: m1000350
Build-Jdk: 1.5.0
Bundle-Version: 1.0
Spring-Context: *;create-asynchronously=false <— Spring DM integration
Bundle-SymbolicName: org.springframework.osgi.samples.simpleservice
Bundle-Name: Simple-Service-Sample
Bundle-Vendor: Spring Framework
Bundle-ManifestVersion: 2
Import-Package: org.springframework.osgi.samples.simpleservice <— note this import

——————————————————–

I have attached the code for my sample application. Unfortunately, the complete Spring DM integration (the use of OsgiBundleXmlApplicationContext) does not work and I have posted a question on the Spring support forum :Spring DM issue.

I was however able to invoke the OSGi service from my non-OSGi client.

Disclaimer: The code is just a sample and is not validated in its approach or manner of using the OSGi platform.

OSGiTest – Rename to .java and open in your favorite editor

9 Responses to “Accessing an OSGi service from a non-OSGi client”

  1. praveena Says:

    Hi Regu,

    Nice one. It would be great if you can explain the technical/business benefits of OSGI to different stake holders.
    1. Developers (In terms of code)
    2. Project Managers (In terms of productivity maintainence)
    3. Business Executives (In terms of $)
    finally
    4. What will happen to our lives if we don’t understand/use OSGi. what we will miss?

    in am manner you explained ESB myth. I see OSGI literature out there explains how OSgi helps to solve deployment (effecient class loading, versioning), this definately doesn’t match the current hype.

    I also guess this is not another EJB stuff (Forcing people to un-necessarily thining about distrubution, transaction etc… with hell lot of interface, OSGi API looks similar btw :)) as Rod Johnosn (Spring author) predicted OSGi to be hot in 2008 & investing huge time along app server vendors (IBM & BEA)

    Anyway it’s really nice to read your informative blogs.


  2. Hi Praveena,

    Thanks for the comments and glad that you liked my other posts🙂

    I am a newbie to OSGi and still debating within myself on the benefits that it can bring to a regular J2EE application. I could see its immediate application in a project where we need incremental download of binaries to address bandwidth issues. Other than that I am unable to get myself to justify the effort that goes into breaking up an application into many dynamic components – I’ll not even mention packaging, which is not an easy task today.

    I guess that explains why I haven’t formed opinions on OSGi and its relevance to the different audience you mention above. I however agree that it must make sense to each of the above in order to make an impact.

    For now, it is definitely an area to watch closely from a technical person’s(developer, lead) perspective and of course to try using it to address some of one’s own immediate needs – thats the way I view any new technology.


  3. Hi Regu
    I figured I’ll share some of he knowledge I have regarding Spring-DM and OSGi that might help you in your ventures🙂. Recently, I’ve written a “Getting Started” tutorial which I plan to expend (with community suggestions).
    http://springosgi.googlepages.com/
    But in its current state it already addresses some of the concerns you currently have:
    – Ability to load all the required OSGi bundles *before* I could load my bundle
    – Look up a published service from the bundle I deployed and invoke it from my standalone code

    Although in its current state it mainly concentrates on how to develop and work with Spring-DM using Eclipse, the same principles could be applied on standalone environment.

    Section 1.4 give a few pointers on the order of the bundle startup
    The last section is about developing Spring-MVC application which will access the “sample service” deployed earlier as and outside service. And although these are both Spring-DM bundles, the communication between them is pure OSGi.
    Let me know if you have any comments
    Cheers


  4. Hi Oleg,

    Thanks for the pointers. You are definitely doing the community a favour by writing the tutorial.
    I shall take a look and get back to you in due course.

    Regu

  5. Vikash Anand Says:

    Hi,

    I am new to OSGI. Using reference from serverside.com and javaworld.com I have created a hello world kind of plugin. I have exported it as deployable plugin and fragments…..Now I want to use this plugin for a test application….please send me some URL for reference as I tried searching but could not get one or some help on it.

    Thanks and Regards,
    Vikash Anand.


    • Vikash,
      The sample I have on this blog entry includes a standalone Java class that you may use to invoke a OSGi service. Iam not sure what else you are looking for. Note that this is a dated post and now you must have easier ways of doing the same – using Spring OSGi or alike solutions.
      Regu

  6. Astha Says:

    Hi, I am unable to find the code link in your page. Please give me the link to the code

  7. Raj Says:

    hi regu
    I have a requirement to access osgi service from a j2ee application. Can you elaborate on the steps needs to achieve this.

    Thanks

  8. Regu Says:

    @Raj,

    This post is quite dated. OSGi support in J2EE changed so much in the meanwhile. You may need to look elsewhere to get answers to your question.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: