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)
(Service Host: Register Remote Service)
Line 54: Line 54:
 
The IDistributionConstants are OSGi 4.2 specification-defined constants.  The first one, i.e.:
 
The IDistributionConstants are OSGi 4.2 specification-defined constants.  The first one, i.e.:
  
 +
<source lang="java">
 
props.put(IDistributionConstants.SERVICE_EXPORTED_INTERFACES,IDistributionConstants.SERVICE_EXPORTED_INTERFACES_WILDCARD);
 
props.put(IDistributionConstants.SERVICE_EXPORTED_INTERFACES,IDistributionConstants.SERVICE_EXPORTED_INTERFACES_WILDCARD);
 +
</source>
  
 
is a required property that says that all the interfaces that are registered should be exported.
 
is a required property that says that all the interfaces that are registered should be exported.
Line 60: Line 62:
 
The second service property set...i.e.:
 
The second service property set...i.e.:
  
 +
<source lang="java">
 
props.put(IDistributionConstants.SERVICE_EXPORTED_CONFIGS, containerType);
 
props.put(IDistributionConstants.SERVICE_EXPORTED_CONFIGS, containerType);
 +
</source>
  
 
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.
 
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.
Line 66: Line 70:
 
Finally, the remote service is actually registered (and automatically distributed by the distribution system) by this line.
 
Finally, the remote service is actually registered (and automatically distributed by the distribution system) by this line.
  
 +
<source lang="java">
 
// 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>
  
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.
+
This OSGi service registration is precisely the same whether a local or a remote service is being register.  Some things to note:
 +
 
 +
#The registration uses a new instance of the hello implemenation class (new Hello())As per the OSGi registerService contract, the IHello interface must be implemented by the object given in the second parameter.
 +
# The service properties (props) result in  
 +
## An ECF container of the given containerType being automatically created
 +
## This container instance being used to distribute the service
 +
## The remote service being published by the ECF discovery provider(s)

Revision as of 21:59, 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 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 implemenation class (new Hello()). As per the OSGi registerService contract, the IHello interface must be implemented by the object given in the second parameter.
  2. The service properties (props) result in
    1. An ECF container of the given containerType being automatically created
    2. This container instance being used to distribute the service
    3. The remote service being published by the ECF discovery provider(s)

Back to the top