This is a walkthrough of the Distributed OSGi Greeter Demo. It should help users of the Distributed OSGi get started with it.
The greeter demo can be found in the samples/greeter directory of the SVN code base and implements a simple OSGi Greeter Service and a consumer to that service with a trivial UI.
In this walkthrough all the required bundles are installed straight from the maven release repository, so no need to check out SVN and build anything to get started with the Greeter Demo.
The Greeter demo design
The demo is composed of 3 bundles:
- The Greeter Interface bundle
- The Greeter Service Implementation bundle
- The Greeter Service Consumer bundle
The Greeter Interface bundle exports the
GreeterService interface which both other bundles depend on. This is the interface:
GreeterException classes are custom types defined in the Greeter Interface bundle.
In this walkthrough, the following setup will be used:
The client side bundles will be running in Equinox, invoking on a Distributed OSGi Service running in Felix. Note that the choice of containers is completely trivial. Any OSGi container that implements the Service Registry Hooks (a new feature in OSGi 4.2) can be used on either side.
The Server Side
The Greeter Service implementation bundle provides a trivial implementation of the GreeterService interface. Additionally, it has an Activator:
Besides creating the service instance, the activator sets the additional properties on the service that are required to make it available remotely:
service.exported.interfacesproperty is set to
*, which means that all the interfaces passes to registerService should be made accessible remotely. In this case it's just the
service.exported.configsis set to
org.apache.cxf.ws, which is a CXF specific configuration type that can be used to expose the OSGi Service as a Web Service over SOAP/HTTP. The address of the service is specified via the
Let's run the server in Felix 3.0.1. As a prerequisite it requires some of the OSGi Compendium Specification interfaces. These don't come with the Felix download, but you can install a bundle that contains these interfaces straight from Maven.
In this walkthrough I'm using the single-bundle distribution of CXF/DOSGi which can be installed straight from the Maven release repository.
To set up my Felix environment, I'm running the following commands:
Some log messages may come up now.
Now let's start up the server-side greeter bundles. Like with the DOSGi bundle itself, I'm installing these straight from the Maven release repository.
At the end of the log messages you will see one appear that says:
INFO: TopologyManager: export sucessful Endpoints: Endpoint Description for ServiceReference [org.apache.cxf.dosgi.samples.greeter.GreeterService]
This means that the service is successfully exposed remotely, and you can verify this by requesting the WSDL:
This WSDL was dynamically generated from the exposed
GreeterService Java interface. Under the hood, the Aegis data binding is used for this.
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 Greeter service. See here for the actual code
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:
Let's run the consumer in Equinox, so that we have the bundles 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.
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.
After a few moments the following window appears:
The window appears once 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:
I can see that the invocation has been received by the service implementation as in the Felix window the following appears.
In the Equinox window I can see the response: