|
Server StructureA server application will internally look like this:
Again, the following code has been simplified by omitting various checks on exceptions and other conditions. It exports an implementation of a file classifier service as a FileClassifierImpl object. The full version of the code is given in Chapter 8. I won't give detailed explanations right now ”this is just to show how the preceding schema translates into actual code. 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 |