A server application will internally look as shown in Table 1-2.
| Pseudocode | Where Discussed |
|---|---|
| prepare for discovery | Chapter 4, "Discovering a Lookup Service" |
| discover a lookup service | Chapter 4, "Discovering a Lookup Service" |
| create information about a service | Chapter 5, "Entry Objects" |
| export a service | Chapter 6, "Service Registration" |
| renew leasing periodically | Chapter 8, "Leasing" |
Again, the following code is simplified, with various checks on exceptions and other conditions omitted. It exports an implementation of a file classifier service as a FileClassifierImpl object. The full version of the code is given in a later chapter. I don't provide detailed code explanations right now, as this example is just intended to show how the preceding schema translates into actual code.
package nonworking;
public class FileClassifierServer implements DiscoveryListener {
protected LeaseRenewalManager leaseManager = new LeaseRenewalManager();
public static void main(String argv[]) {
new FileClassifierServer();
// keep server running (almost) forever to
// - allow time for locator discovery and
// - keep re-registering the lease
Thread.currentThread().sleep(Lease.FOREVER);
}
public FileClassifierServer() {
LookupDiscovery discover = null;
// Prepare for discovery - empty here
// Discover a lookup service
// This uses the asynchronous multicast protocol,
// which calls back into the discovered() method
discover = new LookupDiscovery(LookupDiscovery.ALL_GROUPS);
discover.addDiscoveryListener(this);
}
public void discovered(DiscoveryEvent evt) {
ServiceRegistrar registrar = evt.getRegistrars()[0];
// At this point we have discovered a lookup service
// Create information about a service
ServiceItem item = new ServiceItem(null,
new FileClassifierImpl(),
null);
// Export a service
ServiceRegistration reg = registrar.register(item, Lease.FOREVER);
// Renew leasing
leaseManager.renewUntil(reg.getLease(), Lease.FOREVER, this);
}
} // FileClassifierServer