Versions Compared

Key

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

...

It assumes that you have set up your DOSGi/ZooKeeper based Discovery system as outlined in the DOSGi Discovery page.
@@@ this is work in progress @@@

Demo design

This demo contains consists of a display controller node that writes messages on all the available display instances. Display instances are realized as remote OSGi services, which are registered with the ZooKeeper-based Discovery system. The discovery system informs the controller, which is a consumer of the DisplayServiceDisplayServices, of any available remote instances of this service.
@@@ add an image here @@@

...

The Display Service interface is like thisas follows:

Code Block
java
java
public interface DisplayService {
    boolean displayText(String text);
    String getID();
}

The Display

...

Controller (service consumer)

Let's start with the controller, which is a consumer to the the DisplayService. It's simply using an OSGi ServiceTracker to consume all DisplayService services. It also uses a Scheduled Executor to periodically send test messages to all registered displays. Here's the Activator:

Code Block
java
java
public class Activator implements BundleActivator {
    private ServiceTracker tracker;
    private Map<DisplayService, String> displays = new ConcurrentHashMap<DisplayService, String>();

    public void start(BundleContext bc) throws Exception {
        tracker = new ServiceTracker(bc, DisplayService.class.getName(), null) {
            public Object addingService(ServiceReference reference) {
                Object svc = super.addingService(reference);
                if (svc instanceof DisplayService) {
                    DisplayService d = (DisplayService) svc;
                    System.out.println("Adding display: " + d.getID() + " (" + d + ")");
                    displays.put(d, d.getID());
                }
                return svc;
            }

            public void removedService(ServiceReference reference, Object service) {
                String value = displays.remove(service);
                System.out.println("Removed display: " + value);
                super.removedService(reference, service);
            }            
        };        
        tracker.open();
        // ... scheduler stuff that sends messages to all registered displays ommitted
    }

    public void stop(BundleContext bc) throws Exception {
        tracker.close();
    }
}

...

As we are using Discovery here, there is no static remote service metadata required. So no remote-services.xml file. Discovery will dynamically inform the DOSGi implementation of the locations of available remote services. The DOSGi implemenation will in turn register proxies for the remote services in the local service registry. So as As a service consumer you simply depend on the service - e.g. by using a ServiceTracker as above, or through a component framework such as Blueprint or DS.

...

Running the

...

controller

...

Code Block
-> install @@@
-> start 7https://repository.apache.org/content/groups/snapshots/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-discovery-client/1.1-SNAPSHOT/cxf-dosgi-ri-samples-discovery-client-1.1-SNAPSHOT.jar
-> installstart @@@7
-> start 8
-> ps
START LEVEL 1
   install https://repository.apache.org/content/groups/snapshots/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-discovery-interface/1.1-SNAPSHOT/cxf-dosgi-ri-samples-discovery-interface-1.1-SNAPSHOT.jar
-> start 8
-> ps
START LEVEL 1
   ID   State         Level  Name
[   0] [Active     ] [    0] System Bundle (1.8.0)
[   1] [Active     ] [    1] Apache Felix Shell Service (1.2.0)
[   2] [Active     ] [    1] Apache Felix Shell TUI (1.2.0)
[   3] [Active     ] [    1] Apache Felix Bundle Repository (1.4.0)
[   4] [Resolved   ] [    1] OSGi R4 Compendium Bundle (4.1.0)
[   5] [Active     ] [    1] Distributed OSGi Distribution Software Single-Bundle Distribution
[   6] [Active     ] [    1] Distributed OSGi Zookeeper-Based Discovery Single-Bundle Distribution
[   7] [Active     ] [    1] Distributed OSGI Discovery Sample Client Bundle
[   8] [Active     ] [    1] Distributed OSGI Discovery Sample Interface Bundle

After a brief moment, you will see messages appearing on the controller side. These are the messages sent to all registered displays. Since there are none, they won't show up anywhere else just yet.

Code Block
Sending text to displays: some text 1
Sending text to displays: some text 2
Sending text to displays: some text 3
...

The

...

Remote Displays (service implementation)

Every Display in the system registers a Display Service implementation in the local Service Registry. This happens in the Activator of the provider side of the demo. service implementation bundle. It adds the properties to make the service available remotely as well:

...

Note that the port where the service is made available is dynamically set to be any free port in the system. So you can safely run any number of remote Display Services on the same machine. Because we're using Discovery here, a well-known port is not importantneeded. Registering the service as in the previous code snippet will make it available remotely and publish it to the installed Discovery system.

Once the provider side bundles are started:

system.

Once the provider side bundles are started:

Code Block
-> install https://repository.apache.org/content/groups/snapshots/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-discovery-interface/1.1-SNAPSHOT/cxf-dosgi-ri-samples-discovery-interface-1.1-SNAPSHOT.jar
-> start 7
-> install https://repository.apache.org/content/groups/snapshots/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-discovery-impl/1.1-SNAPSHOT/cxf-dosgi-ri-samples-discovery-impl-1.1-SNAPSHOT.jar
Code Block
-> install @@@
-> start 7
-> install @@@
-> start 8
-> ps
START LEVEL 1
   ID   State         Level  Name
[   0] [Active     ] [    0] System Bundle (1.8.0)
[   1] [Active     ] [    1] Apache Felix Shell Service (1.2.0)
[   2] [Active     ] [    1] Apache Felix Shell TUI (1.2.0)
[   3] [Active     ] [    1] Apache Felix Bundle Repository (1.4.0)
[   4] [Resolved   ] [    1] OSGi R4 Compendium Bundle (4.1.0)
[   5] [Active     ] [    1] Distributed OSGi Distribution Software Single-Bundle Distribution (1.1.0.SNAPSHOT)
[   6] [Active     ] [    1] Distributed OSGi Zookeeper-Based Discovery Single-Bundle Distribution (1.1.0.SNAPSHOT)
[   7] [Active     ] [    1] Distributed OSGI Discovery Sample Interface Bundle
[   8] [Active     ] [    1] Distributed OSGI Discovery Sample Implementation Bundle

You will start seeing the messages appear on the remote display:

Code Block
DisplayService [127.0.0.1 [myhost:1816]: some text 145
DisplayService [127.0.0.1myhost:1816]: some text 146
... 

...

The Discovery system uses standard OSGi mechanisms to store publish the metadata of the services that are exposed remotely in a centralized server, a ZooKeeper server. On the service consumer side, the services are looked up in Discovery through standard OSGi mechanisms as well. These mechanisms are independent of ZooKeeper, so they will work with any Distributed OSGi standards-compliant Dicovery implementation.

ZooKeeper comes with a client program (zkCli) that allows you to look in the ZooKeeper virtual filesystem. All of the OSGi-registered services are stored in a virtual directory under the node /osgi/service_registry. The virtual directory name is based on the fully qualified name of the interface that is implemented by the remote service. So in the case of the DisplayService the remote service metadata is stored under /osgi/service_registry/org/apache/cxf/dosgi/samples/discovery/DisplayService:

Code Block
>$ bin\/zkCli.cmd localhost:2181

-> ls /osgi/service_registry/org/apache/cxf/dosgi/samples/discovery/DisplayService
Processing ls
[127.0.0.1#1816##displaymyhost#1816##display]

Every instance of a remote service that implements the org.apache.cxf.dosgi.samples.discovery.DisplayService interface will get a virtual file in this location. The virtual file is only there for the lifetime of the service. If you stop the service, or kill the OSGi container that hosts it, its associated file in this directory will disappear.
You can obtain the information stored in the node, to get an idea of the metadata that's being communicated using the Discovery system:

Code Block
-> get /osgi/service_registry/org/apache/cxf/dosgi/samples/discovery/DisplayService/127.0.0.1#1816##displaymyhost#1816##display
Processing get
#
#Tue Jun 09 14:52:39 BST 2009
osgi.remote.endpoint.location=http\://127.0.0.1myhost\:1816/display
osgi.remote.interfaces=*
osgi.remote.configuration.pojo.address=http\://127.0.0.1myhost\:1816/display
osgi.remote.configuration.type=pojo
osgi.remote.endpoint.id=0ffe1fbf-c029-4096-bc14-3c7d77470dfd

cZxid = 511
ctime = Tue Jun 09 14:52:39 BST 2009
mZxid = 511
mtime = Tue Jun 09 14:52:39 BST 2009
pZxid = 511
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 81563124471365634
dataLength = 338
numChildren = 0