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 and implements a simple OSGi Greeter Service and a consumer to that service with a trivial UI.
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 theGreeterService
interface which both other bundles depend on. This is the interface:Thepublic interface GreeterService { Map<GreetingPhrase, String> greetMe(String name) throws GreeterException; }
GreetingPhase
andGreeterException
classes are also defined by this bundle.
The Greeter Service bundle provides a trivial implementation of the GreeterService interface. Additionally, it has an Activator:
public class Activator implements BundleActivator { private ServiceRegistration registration; public void start(BundleContext bc) throws Exception { Dictionary props = new Hashtable(); props.put("osgi.remote.interfaces", "*"); props.put("osgi.remote.configuration.type", "pojo"); props.put("osgi.remote.configuration.pojo.address", "http://localhost:9090/greeter"); registration = bc.registerService(GreeterService.class.getName(), new GreeterServiceImpl(), props); } public void stop(BundleContext bc) throws Exception { registration.unregister(); } }
Besides creating the service instance, the activator sets the additional properties on the service that are required to make it available remotely:
- The
osgi.remote.interfaces
property is set to '*', which means that all the interfaces passes to registerService are good for remoting. In this case it's just theGreeterService
interface. - The
osgi.remote.configuration.type
is set topojo
, which is a CXF specific configuration type that can be used to set the URL at which the service is to be exposed. This is done via theosgi.remote.configuration.pojo.address
property.