Skip to main content
Jump to: navigation, search

Tutorial: Building your first OSGi Remote Service

Revision as of 23:56, 5 December 2013 by (Talk | contribs) (Implement the Service)


The OSGi Framework provides a very simple model to expose services within a local runtime. Also available, however, is a specification of Remote that are exported by a distribution provider to allow remote access. The ECF project implements a specification-compliant distribution provider.

This tutorial will show how to

  1. define and implement a simple OSGi service
  2. expose that service for remote access via ECF's implementation of the OSGi Remote Services standard

Define the Service

The key to building a system with low coupling and high cohesion is to create clear and coherent boundaries between different parts of your system. Central to this is defining simple, clear interfaces between allow to interact, but only in clearly defined ways.

For example, here's a simple time service:

package com.mycorp.examples.timeservice;
public interface ITimeService {
	public Long getCurrentTime();

The purpose of this service is to provide the current the form of a long value specifying the number of milliseconds since 1970. Of course, declarations of much more complex services (and remote services) are possible, with multiple methods, multiple arguments for each method, and complex types as arguments and return values. In essence, any semantics that can be represented as a java interface can be used as the service interface.

With OSGi Services Interfaces (and Remote Services) it's a best practice to put the service interface (and any referred to by the service interface) in a distinct bundle. This creates a clear, modular separation between the service interface API and both the service implementation and any code that uses the service. For Remote Services this separation is particularly important, since the client/consumer of the remote service only references the service interface...the implementation doesn't even need to be present in the framework.

Here is a complete bundle with only the ITimeService class declared. Note that the package containing this class is exported via the manifest:

Export-Package: com.mycorp.examples.timeservice;version="1.0.0"

Note also the version information associated with the package.

Host: Implement the Service

Next, it's necessary to create a service 'host' implementing the ITimeService interface. Here's an implementation of our time service

import com.mycorp.examples.timeservice.ITimeService;
public class TimeServiceImpl implements ITimeService {
	public Long getCurrentTime() {
		return new Long(System.currentTimeMillis());

This implementation simply returns the local system's current time.

Register the Service

In the OSGi Service Model it's necessary to register the service with the OSGi service registry. There are multiple ways of doing this...via java code, or declaratively via Declarative Services (DS). For details about various approaches to register an OSGi Service see Vogella's OSGi Services Tutorial. For this tutorial we will register our TimeServiceImpl via java code:

bundleContext.registerService(ITimeService.class, new TimeServiceImpl(),null);

With a normal/local OSGi Service this is all that's necessary to register the service. For a Remote Service, a little more is required to trigger the remote services provider:

Dictionary<String, String> props = new Hashtable<String, String>();
props.put("service.exported.interfaces", "*");
bundleContext.registerService(ITimeService.class, new TimeServiceImpl(), props);  // NOTE use of props rather than null

With an OSGi RS distribution provider present, adding these service properties will trigger the export of the service. Here's an explanation of each of the service props:


// OSGi Standard Property - indicates which of the interfaces of the service will be exported. '*' means 'all'.


// OSGi Standard Property - indicates which provider config(s) will be used to export the service

With the ECF RS implementation present, adding these two service properties will result in the following occurring before the registerService call returns to the caller thread:

  1. The TimeServiceImpl will be exported via the distribution provider identified via config "ecf.generic.server"
  2. The exported service will be published for discovery, using any installed and available discovery providers

Back to the top