Hello, OSGi, Part 2: Introduction to Spring Dynamic Modules

Build a service-oriented application using Spring and OSGi

1 2 3 4 5 6 7 Page 3
Page 3 of 7

Hello, Spring World!

In this section we will develop a simple Hello World application using Spring DM. This very simple application will print "Hello Spring World!!" in your console at the time of starting, and will print "Goodbye Spring World!!" at the time of shutting down. The first step is to create a simple HelloWorld.java class, like this one:

Listing 1. HelloWorld.java

package com.javaworld.osgi.spring;
public class HelloWorld {
    public void start() throws Exception {
        System.out.println("Hello Spring World!! "  );
    }
    public void stop() throws Exception {
        System.out.println("Goodbye Spring World!!");
    }
}


As you can see, HelloWorld.java is a simple class that does not implement any interfaces or extend any superclasses. It has two methods: start() and stop(). The start() method is called at the time of application startup; the stop() method is called at the time of application shutdown.

Your next step is to create a helloworld.xml file in a META-INF/spring folder:

Listing 2. helloworld.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean name="hello" class="com.javaworld.osgi.spring.HelloWorld"
        init-method="start" destroy-method="stop" />
</beans>

In this file you declare only one bean, hello, pointing to the com.javaworld.osgi.spring.HelloWorld class. The value of init-method for this bean is start. The value of the destroy-method attribute is stop.

When you execute your application as an Eclipse OSGi application, you should see the "Hello Spring World!!" message in your console. How could it be so easy? Well, you set it up that way when you created the META-INF/spring folder to contain your helloworld.xml file.

When the extender bundle is started, it sees that your HelloWorld bundle is Spring powered. It then reads helloworld.xml and uses it to create an application context for the bundle. The helloworld.xml file defines a hello bean pointing to your HelloWorld class. The hello bean has defined start() as its initialization method, so the Spring framework will call the start() method of the HelloWorld class. Similarly, when you try to shut down the HelloWorld bundle, the Spring framework will call HelloWorld.java's stop() method.

Service-oriented applications in Spring DM

OSGi's applicability to service-oriented application development is one of its most compelling features. The OSGi service platform allows you build service-oriented applications using OSGi services. A service is nothing but a simple Java object that is published under one or more interface names. The concept of the OSGi service platform is that a source bundle exports services in a common registry. A consumer bundle then searches for a service implementing a particular interface; once found it binds to that service and consumes it.

The OSGi service platform can be very useful when building complex enterprise applications. For example, let's say you are building a Web application. In an OSGi implementation, you might break up the application into two bundles. The data access bundle would interact with databases and export data access services into a common registry. The Web layer bundle would import data access services and use them to display data to the end user.

Building your application based on this modular architecture (executed as a series of bundles) makes it easy to update your data access service from, for instance, a slow service to a faster one. For that matter, you could create a different data access service to talk to a Web services implementation, then replace the RDMBS access service with this new service without restarting your application.

In the next section we'll build a service-oriented contact management application. The application will demonstrate how Spring beans can be exported as OSGi services, as well as how OSGi services can be imported as Spring beans.

1 2 3 4 5 6 7 Page 3
Page 3 of 7