71.

previous chapter table of contents next chapter
  

Monitoring Changes to the Cache

The cache uses remote events to monitor the state of lookup services. It includes a local mechanism to pass some of these changes to a client by means of the ServiceDiscoveryListener interface:

 Package net.jini.lookup; interface ServiceDiscoveryListener {     void serviceAdded(ServiceDiscoveryEvent event);     void serviceChanged(ServiceDiscoveryEvent event);     void serviceRemoved(ServiceDiscoveryEvent event); } 

The ServiceDiscoveryListener methods take a parameter of type ServiceDiscoveryEvent This class has methods:

 package net.jini.lookup; class ServiceDiscoveryEvent extends EventObject {     ServiceItem getPostEventServiceItem();     ServiceItem getPreEventServiceItem(); } 

Clients are not likely to be interested in all events generated by lookup services, even for the services in which they are interested. For example, if a new service registers itself with ten lookup services, they will all generate transition events from NO_MATCH to MATCH , but the client will usually only be interested in seeing the first of these ”the other nine are just repeated information. Similarly, if a service's lease expires from one lookup service, then that doesn't matter much; but if it expires from all lookup services that the client knows of, then it does matter, because the service is no longer available to it. The cache consequently prunes events so that the client gets information about the real services rather than information about the lookup services.

In Chapter 14, an example was given on monitoring changes to services from a lookup service viewpoint, reporting each change to lookup services. A client-oriented view just monitors changes in services themselves , which can be done easily using ServiceDiscoveryEvent objects:

 package client; import java.rmi.RMISecurityManager; import net.jini.discovery.LookupDiscovery; import net.jini.lookup.ServiceDiscoveryListener; import net.jini.lookup.ServiceDiscoveryEvent; import net.jini.core.lookup.ServiceTemplate; import net.jini.core.lookup.ServiceItem; import net.jini.lookup.ServiceDiscoveryManager; import net.jini.discovery.LookupDiscoveryManager; import net.jini.lease.LeaseRenewalManager; import net.jini.lookup.LookupCache; /**  * ServiceMonitor.java  */ public class ServiceMonitor implements ServiceDiscoveryListener {     public static void main(String argv[]) {         new ServiceMonitor();         // stay around long enough to receive replies         try {             Thread.currentThread().sleep(100000L);         } catch(java.lang.InterruptedException e) {             // do nothing         }     }     public ServiceMonitor() {         ServiceDiscoveryManager clientMgr = null;         LookupCache cache = null;         System.setSecurityManager(new RMISecurityManager());         try {             LookupDiscoveryManager mgr =                 new LookupDiscoveryManager(LookupDiscovery.ALL_GROUPS,                                            null /* unicast locators */,                                            null /* DiscoveryListener */);             clientMgr = new ServiceDiscoveryManager(mgr,                                                 new LeaseRenewalManager());         } catch(Exception e) {             e.printStackTrace();             System.exit(1);         }         ServiceTemplate template = new ServiceTemplate(null, null,                                                        null);         try {             cache = clientMgr.createLookupCache(template,                                                 null, /* no filter */                                                 this /* listener */);         } catch(Exception e) {             e.printStackTrace();             System.exit(1);         }     }     // methods for ServiceDiscoveryListener     public void serviceAdded(ServiceDiscoveryEvent evt) {         // evt.getPreEventServiceItem() == null         ServiceItem postItem = evt.getPostEventServiceItem();         System.out.println("Service appeared: " +                            postItem.service.getClass().toString());     }     public void serviceChanged(ServiceDiscoveryEvent evt) {         ServiceItem preItem = evt.getPostEventServiceItem();         ServiceItem postItem = evt.getPreEventServiceItem() ;         System.out.println("Service changed: " +                            postItem.service.getClass().toString());     }     public void serviceRemoved(ServiceDiscoveryEvent evt) {         // evt.getPostEventServiceItem() == null         ServiceItem preItem = evt.getPreEventServiceItem();         System.out.println("Service disappeared: " +                            preItem.service.getClass().toString());     } } // ServiceMonitor 
  


A Programmer[ap]s Guide to Jini Technology
A Programmer[ap]s Guide to Jini Technology
ISBN: 1893115801
EAN: N/A
Year: 2000
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net