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

Warning: this is part of the com.sun.jini package, which may change for Jini 1.1.

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.

There are a number of possible constructors for JoinManager. The simplest is


JoinManager(java.lang.Object obj, 
            Entry[] attrSets, 
            ServiceIDListener callback, 
            LeaseRenewalManager leaseMgr) 
This specifies the service to be managed and its entry attributes. The callback will have its serviceIDNotify() method called when a new locator is discovered. This argument can be null if the application has no interest in this. The leaseMgr can also be set to null and will then be created as needed.

Using this constructor will initiate a search for service locators belonging to the group ``public'', which is defined by a group value of the empty string "". There is no constant for this, and the locators from Sun do not appear to belong to this group, so most applications will need to follow this up immediately with a call to search for locators belonging to any group:


JoinManager joinMgr = new JoinManager(obj, null, null, null);
joinMgr.setGroups(LookupDiscovery.ALL_GROUPS);

The second constructor


JoinManager(java.lang.Object obj, 
            Entry[] attrSets, 
            java.lang.String[] groups,
            LookupLocator[] locators,
            ServiceIDListener callback,
            LeaseRenewalManager leaseMgr) 
adds groups and locators. This allows multicast search for locators belonging to certain groups, and also unicast lookup for known locators. A multicast only search for any groups would use

JoinManager joinMgr = new JoinManager(obj, null, 
                                      LookupDiscovery.ALL_GROUPS,
                                      null, null, null);
where both additional parameters are set to null. On the other hand, a unicast lookup for a single known site would be done by

LookupLocator[] locators = new LookupLocator[1];
locators[0] new LookupLocator("http://www.all_about_files.com");
JoinManager joinMgr = new JoinManager(obj, null, 
                                      LookupDiscovery.NO_GROUPS,
                                      locators, null, null);
(This code ignored exception handling.)

For example, the option2 service can become


package joinmgr;

import option2.FileClassifierImpl;

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

/**
 * 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();

        // stay around long enough to receive replies
        try {
            Thread.currentThread().sleep(1000000L);
        } catch(java.lang.InterruptedException e) {
            // do nothing
        }

    }

    public FileClassifierServer() {

	JoinManager joinMgr = null;
	try {
	    /* this is one way of doing it
	    joinMgr = new JoinManager(new FileClassifierImpl(),
				      null,
				      this,
				      new LeaseRenewalManager());
	    joinMgr.setGroups(null);
	    */
	    /* here is another */
	    joinMgr = new JoinManager(new FileClassifierImpl(),
				      null,
				      LookupDiscovery.ALL_GROUPS,
				      null,
				      this,
				      new LeaseRenewalManager());
	} catch(Exception e) {
	    e.printStackTrace();
	    System.exit(1);
	}
    }

    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 (©) 1999 by Jan Newmarch (http://jan.newmarch.name) jan@newmarch.name.

This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v0.4 or later (the latest version is presently available at http://www.opencontent.org/openpub/). Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.