The Artima Developer Community
Sponsored Link

Observing JavaSpace-Based Systems, Part I
How to Log and Analyze a Space-Based Distributed System
by Philip Bishop & Nigel Warren
October 7, 2002

<<  Page 5 of 6  >>


The LoggingSpace Service

To use the LocalLoggingSpace, you need a server that registers the proxy with the Jini lookup service. In the code to register the proxy, it's important to note that since the proxy is a simple serializable object and not an RMI (remote method invocation) object, such as a UnicastRemoteObject, we must have a loop at the end of main() to stop the VM from exiting. If the VM did exit, the JoinManager wouldn't be able to renew the proxy's lease in the lookup service, resulting in the proxy being removed (from the lookup service) when the lease expired.

import jsip.debug.Debug;

public class RegisterProxy {
    private static JoinManager _joinManager;

    public static void main(String[] args) {
        try {
                    new java.rmi.RMISecurityManager());

            //Create a proxy to put into Reggie
            LocalLoggingProxy proxy = new LocalLoggingProxy();

            //Join stuff, ignoring persistent service id for now.
            ServiceIDListener listener = new ServiceIDListener() {
                public void serviceIDNotify(ServiceID serviceID) {
                    //Not storing for now

            Entry[] attribs = new Entry[]{
                new Name("Local Logging Space")

            _joinManager =
                    new JoinManager(proxy, attribs, listener, null, null);

            //Keep the VM alive, as we're not using
            //a UnicastRemoteObject.
            //If the VM exits, then the proxy's
            //lease with the LUS would expire.
            while (true) {
        catch (Exception ex) {

A second point to note in the RegisterProxy example is that we don't store and reuse the ServiceID the lookup service provides. However, this is to keep the example simple; you should always store and reuse ServiceIDs. The simplest way to do this is to serialize ServiceIDs to the local file system; that way, each time you want to register a proxy the code can check for the existence of the file and use the stored ServiceID if one exists. Failure to reuse ServiceIDs results in dead proxies residing in the lookup service until their leases expire. If you reuse a ServiceID, by contrast, the lookup service replaces the matching proxy with the new one.

The LoggingSpace Client

Now that we have our first logging model complete let's take a quick look at how this affects the client code.

To use the LocalLoggingProxy you simply change the name of the class used for discovery from to

We would suggest using something like a system property that your client code can pick up on to allow logging to be enabled/disabled at start up. A simple way of doing this is to set a system property to the appropriate class name as follows:

java MyClient

Then in your client code do something like this

//get the name of the class, either 
// LoggingProxy or JavaSpace
String cname=System.getProperty("spaceClass");

//Load the class object
Class spaceClass=Class.forName(cname); 

//Create the ServiceTemplate
Class [] types=new Class[]{spaceClass};
ServiceTemplate tmpl=new ServiceTemplate(null,types,null);

<<  Page 5 of 6  >>

Sponsored Links

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use