Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The Service Consumer side.

The Service Consumer Java code contains no specific Distribution-related elements. It's simply an Activator that creates a ServiceTracker listening for services that implement the GreeterService interface. When the service becomes available it opens a little GUI window that asks you for an argument that can be sent to the Greete service. See here for the actual code

Tip

Note that in many cases using an OSGi Component Framework such as Spring-DM, iPojo or OSGi DS is highly recommeded when writing OSGi Service Consumers. OSGi Services are highly dynamic in nature. They can come and go. Using a component framework will generally save you from writing ServiceTracker code as the framework will generally provide the service consumer with a reference to the service via injection.

So the remote service is simply looked up the normal way, via the OSGi Service Registry.
How does it get there? The fact that a lookup on a service is done internally triggers a Service Registry Hook. This will go out to any registered Distributed OSGi Discovery implementations and query them for any matching services.
However, in our setup we haven't yet registered a Discovery implementation. There is an alternative, more static way to provide discovery type information, in case this info is not available via discovery. It can be specified in a OSGI-INF/remote-service/*.xml file. This the content of the Greeter Service Consumer remote-services.xml file:

Code Block
<service-descriptions xmlns="http://www.osgi.org/xmlns/sd/v1.0.0">
  <service-description>
    <provide interface="org.apache.cxf.dosgi.samples.greeter.GreeterService" />
    <property name="osgi.remote.interfaces">*</property>
    <property name="osgi.remote.configuration.type">pojo</property>
    <property name="osgi.remote.configuration.pojo.address">http://localhost:9090/greeter</property>
  </service-description>
</service-descriptions>

The's run the consumer in Equinox, so that we have a bundle running in Equinox talking to a remoted service running in Felix!
As with Felix, we will have to load the bundle with the OSGi compendium interfaces. There's one that ships with Equinox.

Code Block

java -jar plugins/org.eclipse.osgi_3.5.0.v20081201-1815.jar -configuration conf -console
osgi> install file:plugins/org.eclipse.osgi.services_3.2.0.v20081205-1800.jar
Bundle id is 1
osgi> start 1

osgi> install file:/<dosgi-root>/distribution/single-bundle/target/cxf-dosgi-ri-singlebundle-distribution-1.0-SNAPSHOT.jar
Bundle id is 2
osgi> start 2

Some logging messages may appear. You can also automatically load the DOSGi bundles by appending the target/equinox.config.ini.append to you equinox config.ini file.

Code Block

osgi> install file:/O:/samples/greeter/interface/target/cxf-dosgi-ri-samples-greeter-interface-1.0-SNAPSHOT.jar
Bundle id is 3
osgi> install file:/O:/samples/greeter/client/target/cxf-dosgi-ri-samples-greeter-client-1.0-SNAPSHOT.jar
Bundle id is 4
osgi> ss

Framework is launched.

id      State       Bundle
0       ACTIVE      org.eclipse.osgi_3.5.0.v20081201-1815
1       ACTIVE      org.eclipse.osgi.services_3.2.0.v20081205-1800
2       ACTIVE      cxf-dosgi-ri-singlebundle-distribution_1.0.0.SNAPSHOT
3       INSTALLED   cxf-dosgi-ri-samples-greeter-interface_1.0.0.SNAPSHOT
4       INSTALLED   cxf-dosgi-ri-samples-greeter-client_1.0.0.SNAPSHOT

osgi> start 4

After a few moments the following window appears:

Image Added

This means that the ServiceTracker in the consumer has received a callback that the GreeterService has been found. The value entered will used to invoke the (remote) OSGi service, with a call like this:

Code Block
Map<GreetingPhrase, String> result = greeter.greetMe("foobar");

I can see that the invocation has been received by the service implementation as in the Felix window the following appears.

Code Block
-> Invoking: greetMe(foobar)