woensdag 9 juni 2010

Helios Bloghaton: Fun with Eclipse Remote Services - 1

In many hotels there is no 13th floor, let alone a room with number 13. Everyone knows that there are 13 steps leading up to the gallows and if you suffer from friggatriskaidekaphobia then you don't like Friday the 13th.

Nevertheless, the OSGi Alliance decided to assign number 13 to the Remote Services chapter. This is particularly creepy since the book starts with Chapter 1 and then jumps straight to chapter 13. After chapter 13 the next one is numbered 101. It is as if the Alliance almost wanted to proof that superstition is just that.


Eclipse Remote Services Implementation
And the proof sticks because Chapter 13 is a great chapter. ECF has implemented the remote services specification in February with the 3.2 release of ECF. Remote services come in two parts Discovery [1] and Distribution [2]. This article is about remote services in combination with the new Zookeeper based discovery provider.


To the fun
We have created a few Example projects to show you how remote services work and the user interface of this one is depicted above. It uses a cool new SWT Widget which I have named the OSGilloscope (watch out for a separate blog on this subject.) The other part of the UI consists of a tabbed folder. On one page a new quote is displayed every 15 seconds by a discovered remote service, and on the other page there is information about this remote service.

Get the sources
Please fire up a Helios with a new workspace and get the two bundles needed for this example from this project set** (copy this file in a project, give it the extension .psf, right click and choose Import Project Set...).

** Our recent move of non-epl code to github has rendered the psf file unusable for the org.eclipse.ecf.services.quotes project. Please get these projects from github and import it using "File/Import../Existing projects into workspace" and then point to the archive.

If this is a fresh Helios then you also have to download the ECF components. Point your p2 to http://download.eclipse.org/releases/helios and get the Communication Framework extensions from the EclipseRT project.

Run it
Inside the consumer bundle there is a file ending with .product. Open it and click on the link "launch an Eclipse application".

Yazafatutu.com
My server is named after somebody I once met in a dream. A long and strange story which I will tell you for a beer at Eclipse Summit Europe. This server is hosting the other side of this example and it uses ECF Zoodiscovery which in turn uses Apache Zookeeper from the Hadoop project. Zookeeper servers can be grouped to form a network of continuously synchronizing servers. Zookeeper is designed to replicate configuration data and it does that very fast and very reliable.

At Yazafatutu.com I have started an OSGi container and in there the ECF remote services and Zoodiscovery are running. Zoodiscovery is configured to run in StandAlone mode which means that there is no replication but only one central server. A so called hub-and-spoke configuration.

In this OSGi container there are three implementations of the QuoteService interface which is located in the other project you have just downloaded: org.eclipse.ecf.services.quotes. Each implementation resides in a separate bundle and the bundles are started and stopped one by one. Starting a bundle causes the Zoodiscovery software to broadcast a publication message to its clients.

Eclipse Twitter
Press the connect button on your user interface and your client will connect the the Zoodiscovery instance running on yazafatutu.com. After a few seconds, your OSGilloscope will come to live and quotes are shown every time a new bundle is started on the server. One of the three implementations of the QuoteService runs an Eclipse Twitter quote service. If you are on twitter then your quote could be there as well!


The Info tab
The Info tab shows some information about the discovered server. Take a look at the "component id" property because that conveniently counts the number of times a service was published. You can see from its count that the ECF Discovery service can handle a few publications. It needs about 25 weeks to hit a cool million. At the time of this writing it has run about 4 days.

How does it work
It is very easy once you know how. The application itself is a cross between a standalone Java application and an RCP, which is an interesting concept in itself. When the product is started, the Application class is given control. In the start method, the UI is setup and then a listener is installed to listen for service publications. This code is very easy to follow. For every service that comes in, it is checked to be a QuoteService. If this is the case then the UI is updated. The ECF remote services takes care of the remoting and your service is published as if it was a local service.

Please remember to place your code in a try block: You are working with remote services. If the remote server shuts down or decides to stop the service, the communication framework will throw an Exception. You always have to be aware of the fact that OSGi services are dynamic and remote services even more. Code in a defensive way suitable for network programming.

How is the framework started
Please look at the Configuration tab of the product configuration. A few of bundles are started to make sure that the underlying framework is initialized. The connect button of the UI does the manual work by creating a Container which is the ECF concept for an endpoint.
try {
zooContainer.connect(
zooContainer.getConnectNamespace().createInstance(
new String[] { "zoodiscovery.flavor.centralized="
+ getServers().getText() }), null);
Clients most of the time run in centralized mode and point to a server with configuration information and so does our client. We can remove the manual connect by setting the following Java options:

-Dzoodiscovery.autoStart=true
-Dzoodiscovery.flavor=zoodiscovery.flavor.centralized=yazafatutu.com

Conclusion
This is a small example on how to use the ECF Zoodiscovery from a consumer perspective. Various parts of Zookeeper can be configured to achieve an optimum for your specific needs and network. More information can be found on the Eclipse wiki about the configuration of Zoodiscovery.

Many thanks to the developers of OSGi, Equinox, Zookeeper and ECF for enabling us to glue this together to this very useful piece of software.

Need Help?
We can help you with professional services and training to implement this and other parts of Eclipse. Please drop a line to sales@industrial-tsi.com for more information.

Links