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.
A service trying to make itself available will usually have the following policy
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
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.
A JoinManager
can be used by a service to simplify many of
the aspects of locating, registering and renewing leases for a service.