Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

(Service Host: Register Remote Service)
Line 36: Line 36:
 
===Service Host: Register Remote Service===
 
===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.
+
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 standard 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):
 
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):
Line 46: Line 46:
 
// add OSGi service property indicated export of all interfaces exposed by service (wildcard)
 
// add OSGi service property indicated export of all interfaces exposed by service (wildcard)
 
props.put(IDistributionConstants.SERVICE_EXPORTED_INTERFACES,IDistributionConstants.SERVICE_EXPORTED_INTERFACES_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);
+
// add OSGi service property specifying config
 +
props.put(IDistributionConstants.SERVICE_EXPORTED_CONFIGS, containerType);
 
// register remote service
 
// register remote service
 
helloRegistration = bundleContext.registerService(IHello.class.getName(), new Hello(), props);
 
helloRegistration = bundleContext.registerService(IHello.class.getName(), new Hello(), props);
 
</source>
 
</source>
 +
 +
The IDistributionConstants are OSGi 4.2 specification-defined constants.  The first one, i.e.:
 +
 +
props.put(IDistributionConstants.SERVICE_EXPORTED_INTERFACES,IDistributionConstants.SERVICE_EXPORTED_INTERFACES_WILDCARD);
 +
 +
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 is a normal OSGi service registration, using a new instance of the hello implemenation class.  The service properties (props) result in the appropriate ECF provider detecting this registration, and automatically distributing the remote service using the given ECF remote service provider.

Revision as of 21:48, 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 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 standard 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);

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

props.put(IDistributionConstants.SERVICE_EXPORTED_INTERFACES,IDistributionConstants.SERVICE_EXPORTED_INTERFACES_WILDCARD);

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 is a normal OSGi service registration, using a new instance of the hello implemenation class. The service properties (props) result in the appropriate ECF provider detecting this registration, and automatically distributing the remote service using the given ECF remote service provider.

Back to the top