Monday, May 4, 2009

Why developers don't just jump at OSGi

On paper, the choice to use OSGi should be an easy one. After all, OSGi offers an escape from the "jar classpath hell" that Java developers have been living with for years. The ability to compose systems through modules that can be composed and loaded dynamically promises a solutions to a range of important problems that enterprise developers have been grappling with, unsuccessfully, for years.

Yet the takeup of OSGi has been quite slow. I was curious enough the other day to take a look on Jobserve on job postings requiring OSGi, and I found only a handful of positions available. While there seems to inexorable movement towards OSGi driven in particular by application server vendors (who, remember, also drove the adoption of the now infamous EJB 1 and 2), and a few evangelists, we are yet to see a groundswell of enthusiasm from the mainstream developer community, in the same way as, for example, with technologies like Grails and, before it, Spring.

This is a shame, because I believe the ideas that underpin OSGi are fundamentally important to writing flexible systems which can remain manageable as they grow in size and complexity.

I'd like to comment on some of the reasons why OSGi has still not taken off in a way which cements it's role as the foundation for Enterprise Java applications, and also to explain why I haven't used OSGi as the basis of Impala. My intention is not to spread FUD, but to identify some of the perceptions (and potentially misconceptions) held on OSGi and to give my interpretation on to what extent they are justified.

Some people just don't "get it"
Not everybody thinks that the idea of partitioning an application into modules is a good one. Some developers are happier just to lump all classes together under a single source directory, and don't see how an application can benefit from modules. Maybe they haven't worked on projects that really require this kind of partitioning, or have suffered from a botched attempt to modularise an application. Clearly, these developers are not going to be early adopters of OSGi or, for that matter, a technology like Impala.

OSGi won't really help the productivity of my development
Clearly, there is more work involved in setting up an OSGi application than a regular Java application. You need to ensure that all your jars, both for your application and for the third party libraries, are OSGi compliant. For your application's jars, you'll be responsible for the bundle manifest yourself, making sure that its content fits in with the structure and organisation of your application. You'll definitely want some tool to make this job easier. Also, you'll have to source third party libraries which are OSGi compliant, or, in the worse case, add the necesary OSGi metadata yourself.

The productivity advantages of dynamically updatable modules will probably kick in at some point, but not until you have a smooth running development environment set up. You can accelerate this process with the help of an OSGi-based framework such as SpringSource's dm Server, or ModuleFusion.

While OSGi will undoubtedly help you write better and more flexible applications, you don't get many wild claims that OSGi will allow you to build your applications dramatically faster. Developers who come to OSGi with those kinds of expectations will probably be disappointed.

OSGi requires a complex environment
Enterprise Java has a reputation for being complex. Not only do you need to know the Java language, you need to know Spring, Hibernate, web technologies, relational databases, etc. etc.. You need to know all sorts of related technologies, test frameworks, ANT or Maven, and more. And this is just to write traditional Java applications.

To write OSGi-based Enterprise applications, there is much more to know. You'll need a good conceptual understanding of how OSGi works - both in the way that it manages class loaders and the way services are exported and consumed. Not Java 101 stuff. You'll also need a practical understanding of the idiosyncracies of your own OSGi environment. There will be differences in the way you build and deploy applications, and the way you manage the tools and runtime, depending on which OSGi containers and helper frameworks you use. You won't need to be a rocket scientist to figure this all out, but you will need some time, patience and experience. The wave of books coming out on OSGi will definitely help, but don't expect the junior members of your team to be able to jump straight into an OSGi project and hit the ground running.

How do I know it will all work?
Some people might be put off OSGi because of lingering thoughts that they will run into difficulties getting their applications to work in an OSGi environment, especially those with large existing code bases.

Some of the most commonly used frameworks out there are not very OSGi-friendly, typically either because they are designed and packaged in a not very modular way, or because they use class loaders in a way which does not align with the OSGi specification, for example, by using the thread context class loader to load classes. Naive use of these libraries in an OSGi environment will lead to unexpected problems.

You'll need to find a way to work around these issues. The hard way will be to try to do it yourself. The easy way will be to rely on a packaged OSGi solution, again such as dm Server or ModuleFusion. But remember, even here, there are trade-offs. In the case of the dm Server, you'll be very closely tied in to SpringSource as a vendor, and with ModuleFusion, you may need to accept a technology stack which does not include your favourite frameworks.

OSGi applications are difficult to test
This, in my opinion, is a real achiles heel of OSGi. Because OSGi applications need to run in an OSGi container with class loading managed in a very specific way, you cannot run low level integration tests without the tests themselves running in a container. This makes testing OSGi applications particularly challenging.

The only serious attempt I am aware of to address this problem is the Spring Dynamic Modules test framework, which dynamically creates a test bundle using your application code, launches an OSGi container, deploys the test bundle to the OSGi container (as well as the bundles you need to test plus some infrastructure bundles), and runs your test code. It's not especially pretty, but there's no substitute for real integration tests as opposed to unit tests or tests using mock objects.

For me, ease of testing is of fundamental importance in choosing technologies - it certainly is a large part of the reason for the emergence of Spring. I certainly have no appetite for a return to the days of EJB 1 and 2 when applications could only be tested on a container.

Some concluding remarks

Let me make my position clear. I am not an OSGi evangelist. I prefer to think of myself as OSGi-neutral. I have deliberately chosen not to base Impala on OSGi, but I have designed it in a way which accomodates OSGi - indeed I even have a working example of Impala running on OSGi. As OSGi gains traction - and if users demand it - Impala will provide much better support for OSGi and even offer a simple migration route to OSGi which users can choose to adopt on a per project basis.


mcculls said...

[disclaimer: I'm involved in a number of open-source OSGi projects and I'm writing a book on OSGi... so this might be biased!]

Another benefit of modularizing your application is you can give small modules to junior developers and they shouldn't need to know about the legacy issues, especially if you program to interfaces. So in that respect, I don't think OSGi has to be a complex environment - of course if you have to deal with some of the low-level APIs it can get hairy, but there are plenty of frameworks that abstract this (like DS, Spring-DM, iPOJO, peaberry). In fact you can often use OSGi services to shield other developers from the pain of legacy code (I admit at the moment this isn't easy, but as patterns develop it should become easier).

You can also decide to partially modularize your app - ie. just turn your business code into bundles, put the library/legacy jars on the main classpath, and use the "org.osgi.framework.bootdelegation" setting to give the business bundles access to the legacy code. That way you get classic classloading for the legacy bits, while keeping your application nicely modularized - like in Impala.

You'll also to pleased to know that a number of OSGi testing solutions have been unveiled over the last month or so: testing and tooling will get a lot of focus in 2009, which should make it much easier for developers to pick it up :)

[ps. all the best with Impala, it's good to hear about other approaches to modularity]

mcculls said...

Blogger seems to have squashed the URLs together, here they are again:

秦贤 said...

Hey, Your hint hit me a bit!
Take a remember, I forgot EJB, Agent,
Maybe I should forgot OSGi, and maybe OSGi like jade or cougaar..
OK, let me impala a bit.

Eric Newcomer said...

Nice article.

One thing to add about the complexity issue is that while setting up the OSGi development environment may be complex, as Stuart is saying it should also be possible to shield module developers from this complexity, although doing so would of course increase complexity for whoever was creating and overseeing the overall modular design

Peter Kriens said...

Thanks for this informative article. I basically agree with all your statements: OSGi is no silver bullet and there is no gain without pain. The bigger your code base, the more pain (but exponentially more gain fortunately).

However, I think you miss one crucial advantage. OSGi is a standard implemented by a surprisingly large number of companies and open source groups, it has a backing specification of high quality, and is shepherded by an organization that has the key players in this market as members. Hard to beat in my eyes ...

One nit, OSGi is not an application programming model for app developers, it enables collaborating programming models. And in contrast with your premise, I actually regard it as quite well adopted for what it offers: Eclipse, Spring, all major app servers, several Apache projects, and an amazing number of embedded applications.

Kind regards,

Peter Kriens