Posts tagged ‘Spring’
Costin Leau is a Java enterprise developer with extensive J2EE and Spring framework skills, working as a software engineer for SpringSource (formerly known as Interface21). Involved in Spring framework development, Pitchfork, Spring JPA, project lead for Spring Dynamic Modules (Spring OSGi), Spring Modules and Spring JavaConfig.
Speaker at conferences on Java/Spring related topics.
Since the first milestones of Spring Dynamic Modules, requests for running web applications in OSGi started to come in. It has been probably one of the most requested features and no wonder, once 1.0 final was released, web support has been the main focus of the 1.1 branch. I am pleased to report that, with the just released M2, as already hinted by Juergen, Spring-DM supports not just vanilla wars (available since 1.1.0 M1) but also Spring-MVC applications running inside OSGi. In this entry, I would like to briefly discuss the typical OSGi web scenario and Spring-DM’s approach. But first,
Why deploy WARs in OSGi?
Easy question: OSGi natively provides versioning, package wiring and hot reloading. Imagine taking advantage of these features within your applications: you can stop embedding libraries under WEB-INF/lib and start sharing them between your web apps, avoid taglibs duplications (while keeping multiple versions running) and update, at runtime, only certain parts of your application. This is especially useful as web applications tend to be tiered and thus subject to a significant number of changes during their life cycle.
Why are web applications in OSGi problematic?
The Servlet specification revolves around the idea of a web container: a runtime environment for web components that provides standard services such as life cycle management (object creation and disposal, thread allocation), concurrency, HTTP request handling and so on. The OSGi platform on the other hand, acts also as a managed environment with its Service Registry, package wiring and versioning (to name just a few). To deal with this problem, the OSGi committee designed, part of the compendium specification, the Http Service.
The Spring Dynamic Modules for OSGi(tm) Service Platforms project makes it easy to build Spring applications that run in an OSGi framework. A Spring application written in this way provides better separation of modules, the ability to dynamically add, remove, and update modules in a running system, the ability to deploy multiple versions of a module simultaneously (and have clients automatically bind to the appropriate one), and a dynamic service model.
OSGi is a registered trademark of the OSGi Alliance. Project name is used pending approval from the OSGi Alliance.
How to create a Spring bundle project in maven (and eclipse…)
The Spring-OSGi project supplies a maven archetype that will create a Spring bundle project for you all set up and ready to go.
To create a new project simply type the following:
mvn archetype:create \ -DarchetypeGroupId=org.springframework.osgi \ -DarchetypeArtifactId=spring-osgi-bundle-archetype \ -DarchetypeVersion=1.0 \ -DgroupId=<your-project-groupId> \ -DartifactId=<your-project-artifactId> \ -Dversion=<your-project-version>
The result of this is a maven project that defines a simple bean, configures it using src/main/resources/META-INF/spring/bundle-context.xml and src/main/resources/META-INF/spring/bundle-context-osgi.xml, and provides unit and (out of container) integration tests. The project is packaged as an OSGi bundle.
There project does not contain a META-INF manifest yet – to generate one invoke the following command:
mvn package mvn org.apache.felix:maven-bundle-plugin:manifest
You should now see the MANIFEST under target/classes/META-INF/ folder (the upcoming 1.0.1 version will properly generate this folder under META-INF).
Note: if you haven’t already downloaded and installed the spring-osgi artifacts (by running ‘mvn install’ on the spring-osgi tree) you’ll need to add an additional parameter to the above command: -DremoteRepositories=http://s3.amazonaws.com/maven.springframework.org/milestone. If this parameter does not work for you first time, see the additional instructions here.