8.2. Hello World Using notify

For a simple example we are going to return to the “Hello World” application from Chapter 1. You will recall that we wrote a message entry into a space and then read it and printed its content, which happens to be the String “Hello World”. Now we are going to adapt that program to use the notify method instead of read.

To do so, our application will first register a listener that is interested in Message entries arriving in the space. Then, as in the original version, we will write a Message entry into the space. When the new entry arrives in the space, the space notifies the listener by calling the listener's notify method. The listener responds to the event by reading the Message entry from the space and printing its content field. In effect, the application behaves exactly as it did in Chapter 1, but instead of using a blocking read to retrieve the Message entry, it retrieves the entry as a result of being notified that one has been written into the space.

Here is the full code for our new reactive “Hello World” application:

public class HelloWorldNotify {
  public static void main(String[] args) {
    JavaSpace space = SpaceAccessor.getSpace();

    try {
       Listener listener = new Listener(space);
       Message template = new Message();
       space.notify(template, null, listener,
         Lease.FOREVER, null);

       Message msg = new Message();
       msg.content = "Hello World";
       space.write(msg, null, Lease.FOREVER);
    } catch (Exception e) {
       e.printStackTrace();
    }
  }
}

Let's step through the code in detail. As in the original code, the application first obtains a handle to a space. Then we instantiate a Listener object that will act as the listener for remote events from the space. Next we create a Message template, leaving the content field null to serve as a wildcard.

Next, the application calls the space's notify method:

space.notify(template, null, listener, Lease.FOREVER, null);

The first four parameters of the space's notify method are the template, a transaction (in this case a null transaction), the object that will receive notifications (in this case our listener object), and a lease time for the notify. We'll discuss the last parameter in Section 8.3.1; here we supply a null value. When called, the notify method registers the listener for notification whenever an entry is placed in the space that matches the template. This registration will remain in effect until the granted lease expires.

Finally, the application creates and writes a Message entry (with its content field set to “Hello World”) to the space.

Now let's look at the details of the Listener inner class, which is defined as follows:

public class Listener implements RemoteEventListener  {
  private JavaSpace space;

  public Listener(JavaSpace space) throws RemoteException {
    this.space = space;
    UnicastRemoteObject.exportObject(this);
  }

  public void notify(RemoteEvent ev) {
    Message template = new Message();

    try {
       Message result =
        (Message)space.read(template, null, Long.MAX_VALUE);
       System.out.println(result.content);
    } catch (Exception e) {
       e.printStackTrace();
    }
  }
}

The Listener's constructor takes a space and assigns it to a local variable; it then calls the static exportObject method of the UnicastRemoteObject class. This method is part of Remote Method Invocation package and is used by objects that support a remote interface. In effect, this method exports the listener as a remote object that can be called by the space when an event occurs for which a notification is needed. For more information on this topic you can refer to the Java™ Remote Method Invocation Specification from Sun Microsystems.

The Listener class implements the only method, notify, of the RemoteEventListener interface. The notify method is called remotely from the space whenever an entry matching the template is written to the space. When notify is called, the space passes it a remote event object, which we ignore (for now, the arrival of the event is enough to tell us a matching entry was written into the space). When the notify method is called by the remote space, it creates a message template, uses it to read a Message entry from the space, and then prints the entry's content field.

When we run our application, we see the following output:

Hello World

Now that we've seen a simple example of using notification, let's dive into the details of the API.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset