Contents
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.
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 not 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
.
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
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.