Difference between revisions of "EIG:Getting Started with OSGi Remote Services"

From Eclipsepedia

Jump to: navigation, search
(Steps to Create the Hello Example)
Line 84: Line 84:
At this point the service can be remotely discovered by consumers, accessed, and used.
At this point the service can be remotely discovered by consumers, accessed, and used.
===Running the Example Host Application===
===Service Consumer:  Discovering and Accessing the Remote Service===
===Service Consumer:  Discovering and Accessing the Remote Service===

Revision as of 23:50, 26 January 2010


Adding ECF 3.2 to Your Target Platform

When ECF 3.2 is released (expected release date: Feb 19, 2010) it will be available on the ecf download page. Until the release, however, to work with ECF 3.2 you will need to get one of the recent daily builds of the ECF SDK (available here...see the C-HEAD-sdk_feature or the N-HEAD-sdk_feature), OR get the ECF source code and build/run from your local workspace.

Note that you only need the ECF SDK in your target platform.

You will also need Eclipse 3.6M5 in your target platform. See here to download Eclipse 3.6M5 (or newer).

Getting the Example Code

Click here for instructions on retrieving all the Hello Example source into your local workspace

Steps to Create a Remote Service

Below are the steps to define and build the Hello Example Remote Service.

Define a Service Interface

As with any OSGi service, local or remote, you must first define your service interface. Here is the 'hello' service interface defined in the org.eclipse.ecf.examples.remoteservices.hello bundle:

package org.eclipse.ecf.examples.remoteservices.hello;
public interface IHello {
	public void hello(String from);

Create the Service Implementation

There is a trivial implementation of the IHello interface in this class (which is in the org.eclipse.ecf.examples.remoteservices.hello bundle):


Service Host: Register Remote Service

All that's required to distribute as a remote service is to register the service implementation via the OSGi service registry...*with* some OSGi standard service properties set. The OSGi 4.2 remote services specfication defines several standard service properties. When these properties are present when registering via the OSGi service registry, any present distribution system (e.g. ECF's remote services API + a provider implementation) automatically kicks in to distribute the service.

Here, for example, is the code in the hello example host (class: org.eclipse.ecf.internal.examples.remoteservices.hello.host.HelloHostApplication in org.eclipse.ecf.examples.remoteservices.host bundle):

// Setup properties for remote service distribution, as per OSGi 4.2 remote services
// specification (chap 13 in compendium spec)
Properties props = new Properties();
// add OSGi service property indicated export of all interfaces exposed by service (wildcard)
// add OSGi service property specifying config
props.put(IDistributionConstants.SERVICE_EXPORTED_CONFIGS, containerType);
// register remote service
helloRegistration = bundleContext.registerService(IHello.class.getName(), new Hello(), props);

The IDistributionConstants are OSGi 4.2 specification-defined constants. The first one, i.e.:


is a required property that says that all the interfaces that are registered should be exported.

The second service property set...i.e.:

props.put(IDistributionConstants.SERVICE_EXPORTED_CONFIGS, containerType);

uses the OSGi configuration type property ("service.exported.configs") set to the value of the containerType variable. This variable is the ECF container type/container factory name. For example, for the ECF generic provider, this variable is set to 'ecf.generic.server'. For the ECF r-OSGi provider it is set to 'ecf.r_osgi.peer'. To use *any* other ECF remote services provider, all that's needed is to change this variable to the appropriate value.

Finally, the remote service is actually registered (and automatically distributed by the distribution system) by this line.

// register remote service
helloRegistration = bundleContext.registerService(IHello.class.getName(), new Hello(), props);

This OSGi service registration is precisely the same whether a local or a remote service is being register. Some things to note:

  1. The registration uses a new instance of the hello implementation class (i.e. new Hello()). As per the OSGi registerService contract, the IHello interface must be implemented by the object given in the second parameter.
  2. As described above service properties (props) result in
    1. An ECF container of the given containerType is created
    2. The container instance is used to register and distribute the remote service. This is done via the ECF Remote Services API
    3. The remote service is published for network discovery by the ECF Discovery API

At this point the service can be remotely discovered by consumers, accessed, and used.

Running the Example Host Application

Service Consumer: Discovering and Accessing the Remote Service