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 "Getting Started with ECF's RFC119 Implementation"

(Service Interface)
(Service Interface)
Line 19: Line 19:
 
modules: '''org.eclipse.ecf/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello''', '''org.eclipse.ecf/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello.host''', '''org.eclipse.ecf/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello.consumer'''
 
modules: '''org.eclipse.ecf/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello''', '''org.eclipse.ecf/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello.host''', '''org.eclipse.ecf/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello.consumer'''
  
Here is a [[Media:Hello.psf|project set file]] for this project.
+
Here is a [[Media:Hello.psf|project set file]] for these projects.
 +
 
 +
The service interface above is contained in the o.e.e.e.remoteservices.hello bundle, along with a trivial implementation (in org.eclipse.ecf.examples.remoteservices.hello.impl.Hello).
 +
 
 +
===Registering the Service Host===
 +
 
 +
Here is the code in org.eclipse.ecf.internal.examples.remoteservices.hello.host.Activator that registers a remote service with the ECF RFC119 implementation:
 +
 
 +
<source lang="java>
 +
public void start(BundleContext context) throws Exception {
 +
this.context = context;
 +
// Create R-OSGi Container
 +
IContainerManager containerManager = getContainerManagerService();
 +
containerManager.getContainerFactory().createContainer("ecf.r_osgi.peer");
 +
 +
Properties props = new Properties();
 +
// add OSGi service property indicating this
 +
props.put(REMOTE_INTERFACES, REMOTE_INTERFACES_WILDCARD);
 +
// register remote service
 +
helloRegistration = context.registerService(IHello.class.getName(), new Hello(), props);
 +
System.out.println("Host: Hello Service Registered");
 +
}
 +
</source>
 +
 
 +
This code does the following:
 +
 
 +
# Creates an r-osgi ECF container to do the distribution
 +
# Sets the RFC119-specified REMOTE_INTERFACES service property (with REMOTE_INTERFACES_WILDCARD...i.e. '*' value)
 +
# Creates a new Hello instance and registers it as a remote service (via the service props).
 +
 
 +
This is sufficient to publish this hello remote service, and make it available for access by service consumers.
 +
 
 +
===The Service Consumer===
 +
 
 +
The service consumer first discovers the published service, and then when discovered it creates a proxy and registers the proxy in the local service registry. 
 +
 
 +
Here is code from org.eclipse.ecf.internal.examples.remoteservices.hello.consumer.Activator
 +
 
 +
<source lang="java">
 +
public void start(BundleContext context) throws Exception {
 +
this.context = context;
 +
// Create R-OSGi Container
 +
IContainerManager containerManager = getContainerManagerService();
 +
containerManager.getContainerFactory().createContainer("ecf.r_osgi.peer");
 +
// Create service tracker to track IHello instances that are REMOTE
 +
helloServiceTracker = new ServiceTracker(context,createRemoteFilter(),this);
 +
helloServiceTracker.open();
 +
}
 +
</source>
 +
 
 +
Like the service host, this code also must create an R-OSGi peer container instance.  Note that to switch to another/different provider (than R-OSGi), all that must be changed is the string "ecf.r_osgi.peer" in both the host and consumer.
 +
 
 +
After creating the container, a ServiceTracker is created to track the discovery and registration of remotely published services.  This allows discovery to happen asynchronously, and when the remote service matching the filter returned from createRemoteFilter(), the 'serviceAdded' method will be called by the service tracker.  Here is the serviceAdded method:
 +
 
 +
<source lang="java">
 +
public Object addingService(ServiceReference reference) {
 +
System.out.println("IHello service proxy being added");
 +
IHello hello = (IHello) context.getService(reference);
 +
// Call it
 +
hello.hello();
 +
System.out.println("Called hello() on proxy successfully");
 +
return hello;
 +
}
 +
</source>
 +
 
 +
This method:
 +
 
 +
# Gets the hello instance for the reference (which is actually a proxy)
 +
# Calls hello() on the hello instance, just like it was a local service.

Revision as of 00:37, 28 May 2009

Install ECF 3.0. See ecf download.

Service Interface

As with any OSGi service, you need to define your service interface. Here is a trivial 'hello' service interface:

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

This service is defined, along with a simple implementation, in this project in CVS

cvs: :pserver:anonymous@dev.eclipse.org:/cvsroot/rt
modules: org.eclipse.ecf/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello, org.eclipse.ecf/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello.host, org.eclipse.ecf/examples/bundles/org.eclipse.ecf.examples.remoteservices.hello.consumer

Here is a project set file for these projects.

The service interface above is contained in the o.e.e.e.remoteservices.hello bundle, along with a trivial implementation (in org.eclipse.ecf.examples.remoteservices.hello.impl.Hello).

Registering the Service Host

Here is the code in org.eclipse.ecf.internal.examples.remoteservices.hello.host.Activator that registers a remote service with the ECF RFC119 implementation:

	public void start(BundleContext context) throws Exception {
		this.context = context;
		// Create R-OSGi Container
		IContainerManager containerManager = getContainerManagerService();
		containerManager.getContainerFactory().createContainer("ecf.r_osgi.peer");
 
		Properties props = new Properties();
		// add OSGi service property indicating this
		props.put(REMOTE_INTERFACES, REMOTE_INTERFACES_WILDCARD);
		// register remote service
		helloRegistration = context.registerService(IHello.class.getName(), new Hello(), props);
		System.out.println("Host: Hello Service Registered");
	}

This code does the following:

  1. Creates an r-osgi ECF container to do the distribution
  2. Sets the RFC119-specified REMOTE_INTERFACES service property (with REMOTE_INTERFACES_WILDCARD...i.e. '*' value)
  3. Creates a new Hello instance and registers it as a remote service (via the service props).

This is sufficient to publish this hello remote service, and make it available for access by service consumers.

The Service Consumer

The service consumer first discovers the published service, and then when discovered it creates a proxy and registers the proxy in the local service registry.

Here is code from org.eclipse.ecf.internal.examples.remoteservices.hello.consumer.Activator

	public void start(BundleContext context) throws Exception {
		this.context = context;
		// Create R-OSGi Container
		IContainerManager containerManager = getContainerManagerService();
		containerManager.getContainerFactory().createContainer("ecf.r_osgi.peer");
		// Create service tracker to track IHello instances that are REMOTE
		helloServiceTracker = new ServiceTracker(context,createRemoteFilter(),this);
		helloServiceTracker.open();
	}

Like the service host, this code also must create an R-OSGi peer container instance. Note that to switch to another/different provider (than R-OSGi), all that must be changed is the string "ecf.r_osgi.peer" in both the host and consumer.

After creating the container, a ServiceTracker is created to track the discovery and registration of remotely published services. This allows discovery to happen asynchronously, and when the remote service matching the filter returned from createRemoteFilter(), the 'serviceAdded' method will be called by the service tracker. Here is the serviceAdded method:

	public Object addingService(ServiceReference reference) {
		System.out.println("IHello service proxy being added");
		IHello hello = (IHello) context.getService(reference);
		// Call it
		hello.hello();
		System.out.println("Called hello() on proxy successfully");
		return hello;
	}

This method:

  1. Gets the hello instance for the reference (which is actually a proxy)
  2. Calls hello() on the hello instance, just like it was a local service.

Back to the top