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

From Eclipsepedia

Jump to: navigation, search
(Define a Service Interface)
Line 15: Line 15:
 
Below are the steps to define and build the Hello Example Remote Service.
 
Below are the steps to define and build the Hello Example Remote Service.
  
==Define a Service Interface==
+
===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:
 
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:
Line 26: Line 26:
 
 
 
}
 
}
 +
</source>
 +
 +
===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):
 +
 +
'''org.eclipse.ecf.examples.remoteservices.hello.impl.Hello'''
 +
 +
===Service Host: Register Remote Service===
 +
 +
To actually register (and distribute) the remote service all that's required is to register the service via the OSGi service registry...*with* some OSGi service properties defined.  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):
 +
 +
<source lang="java">
 +
// 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)
 +
props.put(IDistributionConstants.SERVICE_EXPORTED_INTERFACES,IDistributionConstants.SERVICE_EXPORTED_INTERFACES_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);
 
</source>
 
</source>

Revision as of 21:35, 26 January 2010

Contents

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 the Hello Example

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):

org.eclipse.ecf.examples.remoteservices.hello.impl.Hello

Service Host: Register Remote Service

To actually register (and distribute) the remote service all that's required is to register the service via the OSGi service registry...*with* some OSGi service properties defined. 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)
props.put(IDistributionConstants.SERVICE_EXPORTED_INTERFACES,IDistributionConstants.SERVICE_EXPORTED_INTERFACES_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);