Join Manager

Contents

  1. Join Manager
  2. Getting information from JoinManager
  3. Summary
Finding a lookup service involves a common series of steps. Subequent interaction with them also involves common steps. A join manager encapulates these into one convenience class.

1. Join Manager

A service trying to make itself available will usually have the following policy

  1. Find all possible service locators
  2. Register with all of them
  3. Keep re-registering till the end of time
  4. If any changes of state occur, notify all locators that the service is registered with
Having written this code once, it can be copied from one service to another. Better, it can be encapsulated in an object. Even better, Sun have done this as the class JoinManager. This can be used to severely reduce the amount of code needed by a service. For example, the option2 service can become


package joinmgr;

import com.sun.jini.lookup.JoinManager;
import net.jini.core.lookup.ServiceID;
import com.sun.jini.lookup.ServiceIDListener;
import com.sun.jini.lease.LeaseRenewalManager;

/**
 * FileClassifierServer.java
 *
 *
 * Created: Wed Mar 17 14:23:44 1999
 *
 * @author Jan Newmarch
 * @version 1.0
 */

public class FileClassifierServer implements ServiceIDListener {
    
    public static void main(String argv[]) {
	new FileClassifierServer();
    }

    public FileClassifierServer() {

	JoinManager joinMgr = null;
	try {
	    joinMgr = new JoinManager(new FileClassifierImpl(),
					      null,
					      this,
					      new LeaseRenewalManager());
	} catch(Exception e) {
	    e.printStackTrace();
	    System.exit(1);
	}
        // stay around long enough to receive replies
        try {
            Thread.currentThread().sleep(1000000L);
        } catch(java.lang.InterruptedException e) {
            // do nothing
        }

    }

    public void serviceIDNotify(ServiceID serviceID) {
	System.out.println("got service ID " + serviceID.toString());
    }
    
} // FileClassifierServer

2. Getting information from JoinManager

The JoinManager would appear to be unhelpful in supplying information about the lookup locators it finds. It is available, but by a slightly circuitous route. A service can register a ServiceIDListener to the JoinManager. This will be invoked whenever a new locator is found, by its serviceIDNotify() method. A ServiceID is not particularly useful, so we just ignore it. However, within this method we do know that a new service locator has been found.

The complete set of service locators may be found from the JoinManager method getJoinSet(), whuch returns an array of ServiceRegistrar's. We have met this class before: its method getLocator() will return a LookupLocator, which has information such as host in getHost(). These can be put together as


protected JoinManager joinmgr;
...

joinmgr = new JoinManager(service, null, 
                                          this, new LeaseManager());
...

public  void serviceIDNotify(ServiceID serviceID) {
    ServiceRegistrar registrars = joinmgr.getJoinSet();
    for (int n = 0; n < registrar.length; n++) {
        LookupLocator locator = registrars[n].getLocator();
	String hostName = locator.getHost();
	...
    }
}

If you want to find out which is the latest locator to be found, it looks like you will have to cache the previous set, and find which is new in the array returned. Each call to getJoinSet() will return a new array.

3. Summary

A JoinManager can be used by a service to simplify many of the aspects of locating, registering and renewing leases for a service.

This file is Copyright ©Jan Newmarch (http://jan.newmarch.name) jan@newmarch.name

The copyright is the OpenContent License (http://www.opencontent.org/opl.shtml), which is the ``document'' version of the GNU OpenSource license.