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 4 of 6  >>

Advertisement

A Local Logging Proxy Implementation

Using the general architecture shown in Figure 1, one simple implementation would be to write the logging information on the local console or a local disk. In that way, the information is recorded locally to each client making calls on the space proxy.

When a call is made to the logging proxy's API, the logging proxy constructs a SpaceOperation object for the associated JavaSpaces API call and then forwards the call to DelegateProxy.

For simplicity's sake, the logOperation() method simply uses the system's output stream to write the formatted SpaceOperation object to the local console, but we could implement it to write to a local disk file.

Here are the highlights of the LocalLoggingProxy implementation:

package jsip.space;

public class LocalLoggingProxy
        implements LoggingSpace, Externalizable {
    private JavaSpace _delegateSpace;

    public void readExternal(ObjectInput in)
            throws IOException, ClassNotFoundException {
        //Nothing to read
        Debug.message("readExternal() finding space");
        try {
            _delegateSpace = SpaceLocator.getSpace();
        }
        catch (Exception exp) {
            throw new IOException(
                    "Can't find JavaSpace " + exp.toString());
        }
    }

    public void writeExternal(ObjectOutput out)
            throws IOException {
        //Nothing to write
    }

    public Lease write(Entry entry,
                       Transaction txn, long lease)
            throws TransactionException,
            RemoteException {
        logOperation(
                new SpaceOperation("write", entry, txn, lease));
        return _delegateSpace.write(entry, txn, lease);
    }

    // all of the other JavaSpaces API methods 
    public Entry snapshot(Entry entry)
            throws RemoteException {
        logOperation(
                new SpaceOperation("snapshot", entry, null, 0));
        return _delegateSpace.snapshot(entry);
    }

    private void logOperation(SpaceOperation op) {
        System.out.println(op.toString());
    }
}

In the previous code example, we have skipped most of the methods that exist in the JavaSpaces API, because they essentially follow the same pattern as the write method. In the snapshot case, we have included default information for some of the parameters.

The most interesting thing to note in the LocalLoggingProxy class is how it dynamically embeds the delegate space proxy. Because we know that Java's serialization mechanism invokes the readExternal() method when a client deserializes the proxy, we can insert the code to find the delegate space and assign its proxy to the field _delegateSpace. The LocalLoggingProxy uses that field to invoke the matching methods on the JavaSpace.

We can use the readExternal() technique safely to locate the space because we know that the proxy gets serialized to the Jini Lookup Service as a MarshalledObject. This means that the proxy itself isn't deserialized in the lookup service. If this wasn't the case, using this technique would be a bad idea because the readExternal() method would also get invoked in the lookup service.

Why do we defer the discovery of the JavaSpace until the LocalLoggingProxy is deserialized in the client, rather than discovering the space and embedding it in the proxy when it is registered with a Jini Lookup Service? Because that way we can stop and restart the space without having to stop and re-register the LocalLoggingProxy.

This type of adapter or smart Jini proxy has many interesting uses, for example, we could write a fault tolerant JavaSpace proxy by adding exception handling around the delegated method calls to detect if the space became unavailable. If the space became unavailable, the smart proxy could then reinitiate discovery and connect to another space or attempt to reconnect to the failed space.

There are more examples and variations of smart Jini proxies, like the LocalLoggingProxy presented in our book JavaSpaces In Practice (see Resources).

<<  Page 4 of 6  >>


Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2017 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us