Simple Message Service

David Winterfeldt

2008


There are three OSGi bundles in this example. Two are different versions of a bundle that exposes a message interface as an OSGi service. Each bundle depends on a different version of Commons Lang to demonstrate the side-by-side versioning capabilities of OSGi. The third bundle is a web module that displays the result from calling the OSGi message service. The example was made to be deployed on the Spring dm Server.

[Note]Note

Currently the examples are setup to only run from the Eclipse environment and to be deployed using the Spring dm Server's IDE integration. A Maven build will eventually be added.

1. Message Service Bundle 1.0

Manifest Configuration

The manifest defines the services name, description, it's symbolic name (will deployed under this name), version, and whatever libraries, bundles, and/or packages it imports. Also, whatever packages are exported are also defined.

[Note]Note

If when opening the manifest and viewing the Dependencies tab in the Eclipse IDE, there isn't an 'Import Bundle' section on the right and an 'Import Library' section underneath it the default Eclipse manifest editor may have been opened. To open the Spring manifest editor, right click, 'Open with'/'Other...', and then choose the 'Bundle Manifest Editor'.

src/main/resources/META-INF/MANIFEST.MF
                    
Manifest-Version: 1.0
Bundle-Name: Simple Service Bundle
Bundle-Description: Simple Service
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.springbyexample.sdms.message.messageService  1
Bundle-Version: 1.0.0  2
Import-Library: org.aspectj,
 org.springframework.spring
Import-Bundle: com.springsource.org.apache.commons.lang;version="[2.1.0,2.1.0]"  3
Export-Package: org.springbyexample.sdms.message.service;version="1.0.0"  4
                    
                
1 The symbolic name the bundle is deployed under and another bundle could use to reference it.
2 The version the bundle is deployed under.
3 The Import-Bundle property imports the package com.springsource.org.apache.commons.lang, version 2.1.0.
4 The Export-Package property exposes any packages for external use by other bundles. This exposes the package org.springbyexample.sdms.message.service which has the MessageService interface as version 1.0.0.

Spring Configuration

By default any spring configuration files in the META-INF/spring will be processed (this is configurable). Following the naming conventions setup by Spring Dynamic Modules for OSGi(tm) Service Platforms there are two Spring configuration files. One is called bundle-context.xml and the other is called bundle-context-osgi.xml. The first one is for standard Spring configuration and the latter is meant for Spring Dynamic Modules specific configuration. Like exposing OSGi services or defining services as bean references.

The MessageServiceImpl is defined as a messageService bean which will be exposed as a service in the bundle-context-osgi.xml configuration file. Spring's context:component-scan could have also been used, but wasn't just to keep the example a little more clear.

src/main/resources/META-INF/spring/bundle-context.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="messageService" 
          class="org.springbyexample.sdms.message.service.impl.MessageServiceImpl" 
          p:message="Greetings!"/>
    
</beans>
                    
                

The Spring Dynamic Modules' namespace is setup as the default namespace for the configuration file. The service element exposes the messageService bean as an OSGi service under the interface org.springbyexample.sdms.message.service.MessageService.

src/main/resources/META-INF/spring/bundle-context-osgi.xml
                    
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/osgi"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xsi:schemaLocation="http://www.springframework.org/schema/beans 
                                 http://www.springframework.org/schema/beans/spring-beans.xsd
                                 http://www.springframework.org/schema/osgi 
                                 http://www.springframework.org/schema/osgi/spring-osgi.xsd">

    <service  ref="messageService" interface="org.springbyexample.sdms.message.service.MessageService"/>
  
</beans:beans>
                    
                

Code Example

The code consists of the MessageService interface and the implementation MessageServiceImpl. It's just an interface to get a message and the implementation uses Commons Lang.

Example 1. Message Service MessageService (version 1.0)

sdms/message-service/service-version1_0/src/main/java/org/springbyexample/sdms/message/service/MessageService.java
                    
public interface MessageService {

    /**
     * Gets message.
     */
    public String getMessage();
    
}
                            
                        

This implementation uses FastDateFormat from Commons Lang 2.1.0 to display a formatted date after the message that will be injected from the Spring configuration.

Example 2. Message Service MessageServiceImpl (version 1.0)

sdms/message-service/service-version1_0/src/main/java/org/springbyexample/sdms/message/service/impl/MessageServiceImpl.java
                            
public class MessageServiceImpl implements MessageService {

    private String message = null;
    
    /**
     * Gets message.
     */
    public String getMessage() {
        StringBuffer sb = new StringBuffer();
        
        sb.append(message);
        sb.append("  [");
        sb.append(FastDateFormat.getInstance("MM/dd/yy").format(new Date()));
        sb.append("]");
        
        return sb.toString();
    }

    /**
     * Sets message.
     */
    public void setMessage(String message) {
        this.message = message;
    }
    
}