Service Registration

This chapter looks at how services register themelves with lookup locators so that they can later be found b y clients.

1. ServiceRegistrar

A service registers itself with a LookupLocator using the ServiceRegistrar method register():

package net.jini.core.lookup;

public Class ServiceRegistrar {
    public ServiceRegistration register(ServiceItem item,
                                       long leaseDuration)
                               throws java.rmi.RemoteException;
}
The second parameter here is a request for the length of time the lookup service will keep the service registered. This request need not be honored: the lookup service may reject it completely, or only grant a lesser time interval. This is discussed in the section on leases (leases section???). The first parameter is of type

package net.jini.core.lookup;

public Class ServiceItem {
    public ServiceID serviceID;
    public java.lang.Object service;
    public Entry[] attributeSets;

    public ServiceItem(ServiceID serviceID,
                   java.lang.Object service,
                   Entry[] attrSets);
}

2. ServiceItem

The service will make an object of this type, using the constructor and pass it into register(). The serviceID is set to null when the service is registered for the first time. The lookup service will set a non-null value as it registers the service. On subsequent registrations or re-registrations, this non-null value should be used. It only has meaning to the particular service locator that sets it.

The second parameter is the service object that is being registered. This object will be serialised and sent to the service locator for storage. When a client later requests a service, this is the object it will be given. To avoid confusion between the original service and the object that is sent to the lookup service, I shall use the term exported service object, although this terminology is not used in the formal Jini documentation. There are several things to note about the exported service object:

The third parameter is a set of entries giving information about the service in addition to the service item itself. If there is no additional information, this can be null.

3. Registration

The service attempts to register itself by calling register(). This may throw an java.rmi.RemoteException which must be caught. The return value is of type ServiceRegistration

4. ServiceRegistration

This object is created by the service locator and is returned by RMI to run in the service. This acts as a proxy object to control the state maintained about the exported service object stored on the lookup service. This object maintains a field serviceID which is used to identify the exported service object on the lookup service. this can be retrieved by getServiceID() for reuse by the service if it needs to do so.

Other methods such as

 
void addAttributes(Entry[] attrSets);
void modifyAttributes(Entry[] attrSetTemplates, Entry[] attrSets);
void setAttributes(Entry[] attrSets);
can be used to change the attributes stored on the lookup service.

The final public method for this class is getLease() which returns a Lease object, which allows renewal or cancellation of the lease. This will be discussed in more detail later.

The service then has nothing else to do. If the exported service object can do the full task of the service, then this instance can exit. If the exported service object acts as a proxy and needs to communicate back to the service then the service can sleep so that it continues existence. If the service needs to stay around so that it can re-register itself when timeout occurs then it can sleep as well.

A unicast service that exports itself and has nothing else to do is in the following program:



import net.jini.core.discovery.LookupLocator;
import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceItem;
import net.jini.core.lookup.ServiceRegistration;
import java.io.Serializable;

/**
 * SimpleService.java
 *
 *
 * Created: Fri Mar 12 22:34:53 1999
 *
 * @author Jan Newmarch
 * @version
 */

public class SimpleService implements Serializable {
    
    static public void main(String argv[]) {
        new SimpleService();
    }
   
    public SimpleService() {
	LookupLocator lookup = null;
	ServiceRegistrar registrar = null;

        try {
            lookup = new LookupLocator("jini://localhost");
        } catch(java.net.MalformedURLException e) {
            System.err.println("Lookup failed: " + e.toString());
	    System.exit(1);
        }

	try {
	    registrar = lookup.getRegistrar();
	} catch (java.io.IOException e) {
            System.err.println("Registrar search failed: " + e.toString());
	    System.exit(1);
	} catch (java.lang.ClassNotFoundException e) {
            System.err.println("Registrar search failed: " + e.toString());
	    System.exit(1);
	}

	// register ourselves as service, with no serviceID
	// or set of attributes
	ServiceItem item = new ServiceItem(null, this, null);
	ServiceRegistration reg = null;
	try {
	    reg = registrar.register(item, 10000000L);
	} catch(java.rmi.RemoteException e) {
	    System.err.println("Register exception: " + e.toString());
	}
	// we can exit here if the exported service object can do
	// everything, or we can sleep if it needs to communicate 
	// to us or we need to re-register later 
    }
   
} // SimpleService

How do we re-register???

4.1 Running the SimpleService

The program needs to be compiled and run with reggie.jar and jini-code.jar in its CLASSPATH. When run, it will attempt to connect to the service locator, so obviously one needs to be running on the machine specified in order for this to happen. Otherwise, it will throw an exception and terminate.

This does not need additional support services such as rmiregistry or rmid since all registration is done by the method register()

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.