Introduction

The OSGi Alliance specifies a very simple model to expose arbitrary services within a local runtime. As of OSGi 4.2, however, also available is a specification of how to use this service model for remote services (Chapter 100 in the EEG specification)...services that are exported by a distribution provider, and allow remote access by multiple client/consumers.

This tutorial will guide the reader through

Defining and implementing an OSGi remote service

Exposing that service for remote access via OSGi Remote Services standard, using the implementation provided by the Eclipse Communication Project (ECF)

Common to Host and Consumer: 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 subsystems...to allow the subsystems to interact as needed, but only in clearly defined ways.

The purpose of this service is simply to provide the current time...in the form of a long value specifying the number of milliseconds since 1970 returned from a call to getCurrentTime(). Of course, declarations of much more complex services are possible, with multiple methods, multiple arguments for each method, and complex types as arguments and return values. In short, any java interface can be used as the service interface. OSGi also allows the use of multiple interfaces exposed by a single implementation.

With OSGi services it's a best practice to put the service interface (and any class referred to by the service interface) in a distinct bundle. This creates a clear, modular, framework-enforced separation between the service API, the service implementation, and any code that uses the service. This minimizes the coupling between subsystems. For Remote Services such separation and low-coupling is particularly important, since the host implementation is in a separate process from the service client/consumers...making it highly desirable to minimize the coupling and clarify the interface between systems.

Note also the version information associated with the package...OSGi Remote Services supports versioning of service interfaces. Being able to version service interfaces, and have the underlying framework automatically restrict the service binding to compatible versions is a very useful feature for enhancing and maintaining APIs over time.

Service Host: Implement the Service

Next, it's necessary to create a service 'host' implementing the ITimeService interface.

This implementation simply returns the local system's current time. To refer to the ITimeService service the host bundle manifest must import the com.mycorp.examples.timeservice package from the bundle defined above by adding a line like this to the host bundle manifest:

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

Service Host: 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 registering a service...programmatically java code, declaratively via Declarative Services (DS). For introductions about various approaches to register an OSGi Service see Vogella's OSGi Services Tutorial. For this tutorial we will register our TimeServiceImpl via a single line of java code:

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

With a local OSGi Service the above line of java is all that's necessary to register the service. For a Remote Service, only a little more is required to trigger the distribution provider to export the service:

Dictionary<String, String> props =new Hashtable<String, String>();// OSGi Standard Property - indicates which of the interfaces of the service will be exported. '*' means 'all'.
props.put("service.exported.interfaces", "*");// OSGi Standard Property - indicates which provider config(s) will be used to export the service
props.put("service.exported.configs","ecf.generic.server");// Register a new TimeServiceImpl with the above props
bundleContext.registerService(ITimeService.class, new TimeServiceImpl(), props);

With the ECF RS implementation present in the host runtime, adding these service properties will result in the following occurring before the registerService call returns:

The TimeServiceImpl will be exported via the distribution provider identified via config..i.e. "ecf.generic.server"

The exported service will be published for discovery, using any installed and available network discovery providers

Note that there are no references to additional OSGi classes...or ECF classes...but rather only classes exposed by the service interface (in this case ITimeService). For all of the remoting information, standardized service properties are used to qualify the ITimeService API as a remote service, and to select and configure a remote services provider. This provides a great deal of flexibility to the ITimeService implementer, and allows the dynamic selection and use of a variety of distribution providers...from ECF or not...and multiple configurations.

Consumer: Discover and Use the Service

For consumers to use a remote service they must first discover the service. With OSGi Remote Services this discovery can be accomplished several ways. The easiest and most automatic is to use a network discovery protocol, such that when the remote service is exported by a host and published via a network discovery protocol (as described above), a consumer can then automatically discover the remote service over the network.

For this tutorial, we'll assume that one ECF's supported LAN-based network discovery providers is present, meaning that if the remote service host and consumer are on the same LAN, the consumer will automatically discover the remote service via the network discovery protocol (e.g. Zeroconf). With network discovery, the consumer can use OSGi Declarative Services to automatically bind/inject the service into client code:

OSGi Declarative Services will automatically inject the ITimeService proxy by calling the bindTimeService method when the remote service is discovered. With the code above, the ITimeService.getCurrentTime() method will be called only when the binding occurs.

As with the host, to refer to the ITimeService the consumer must also import the com.mycorp.examples.timeservice package by importing the package via it's manifest:

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

The only other requirement for the consumer is to have standard DS markup so that DS can know to call bindTimeService when the ITimeService proxy is registered by the consumer's distribution provider. The required DS markup is available in this xml file.

The complete project for this consumer (TimeServiceComponent class, DS markup, and manifest.mf) is available here.

along with the ECF remote service implementation, we may now run the ITimeService host implementation. The consumer can then be run...to discovery, inject, and to use the ITimeService.

Starting the Host

In the com.mycorp.examples.timeservice.host project, in the launch directory is a launch config named TimeServiceHost.launch. In Eclipse, you can launch the host by right-clicking on this launch config and choosing Run As -> TimeServiceHost. In the console output, you should see something like this

This indicates that the service was registered and exported by ECF's remote service implementation.

Starting the Consumer to Execute the Remote Service

In the com.mycorp.examples.timeservice.consumer.ds project, in the launch directory is a launch config named TimeServiceConsumer DS.launch. In Eclipse, you may launch the consumer by right-clicking on this launch config and choosing Run As -> TimeServiceConsumer DS. In the console output, you should immediately see:

osgi>

But then after a few seconds (for the network discovery protocol to operate) the service will be discovered, the proxy created via the ECF distribution provier, and the proxy injected into the TimeServiceComponent via the call to bindTimeService, resulting in console output:

osgi> Discovered ITimeService via DS
Current time is: 1386354647672

As well as host console output resulting from the call to TimeServiceImpl.getCurrentTime():