Simple Message Service

David Winterfeldt


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.


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.


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'.

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,
Import-Bundle:;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, 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.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <bean id="messageService" 

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.

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns=""

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

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)

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)

public class MessageServiceImpl implements MessageService {

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

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