In this article, the second in series 3 discussing dynamic class loading, Ivan takes a look at the many benefits of OSGi, as well as exploring declarative services and OSGi Services in more detail. Be sure to read the previous blog to learn about what dynamic class loading is, and its advantages and disadvantages.
- The many benefits of using OSGi.
- Use OSGi services for a better dynamic component model.
2) Enterprise OSGi Applications with dynamism – OSGi services and declarative services
There are many benefits to OSGi, including:
- efficient and quicker class loading
- componentisation of the application
- the ability to run multiple versions of a package/class within the same JVM
- component update without JVM restart
- missing dependencies are detected at install time thus avoiding runtime failures
Dynamic class loading however, is not on its list of strengths.
The crux of the problem for OSGi is that when dependencies (classes/packages) are not known at application deployment time, and are instead loaded dynamically – the OSGi framework cannot know to wire between the relevant class loaders, and so
The good news is that OSGi provides a far better alternative with its dynamic service model. Backed by the OSGi service repository, OSGi service implementations declared with this model can come and go – giving you the same benefits of dynamic class loading, without the pain, and without playing class loader roulette. Not only will you reap the aforementioned benefits, but updates and improvements will be bound to your application dynamically. Indeed, the use of OSGi services is generally encouraged because it provides flexible and genuinely dynamic application updates. It is a common misconception of OSGi that bundle-wiring provides the dynamic update capability – it does not. The OSGi service model does.
If that is not enough to persuade you to use OSGi services, then let’s talk declarative services. Declarative Services is a way to define your services and the components that use them; and to have those services automatically injected/removed as they become available. Declarative services removes the need to write service tracking code yourself. This is a very powerful, convenient, way to architect your OSGi applications for the provision of dynamic updates. Ultimately OSGi services provide a far cleaner solution and offer a wealth of benefits beyond any class loader based approach.
Despite all the benefits of OSGi, there are some real world reasons why you may not always be able to exploit OSGi services. Read the next blog in the series to discover what these are.