90.

previous chapter table of contents next chapter
  

The SimpleService Program

A unicast server that exports its service and does nothing else is shown in the following SimpleService program:

 package basic; import net.jini.core.discovery.LookupLocator; import net.jini.core.lookup.ServiceRegistrar; import net.jini.core.lookup.ServiceItem; import net.jini.core.lookup.ServiceRegistration; import java.io.Serializable; /**  * SimpleService.java  */ public class SimpleService implements Serializable {     static public void main(String argv[]) {         new SimpleService();     }     public SimpleService() {         LookupLocator lookup = null;         ServiceRegistrar registrar = null;         try {             lookup = new LookupLocator("jini://localhost");         } catch(java.net.MalformedURLException e) {             System.err.println("Lookup failed: " + e.toString());             System.exit(1);         }              try {             registrar = lookup.getRegistrar();         } catch (java.io.IOException e) {             System.err.println("Registrar search failed: " + e.toString());             System.exit(1);         } catch (java.lang.ClassNotFoundException e) {             System.err.println("Registrar search failed: " + e.toString());             System.exit(1);         }           // register ourselves as service, with no serviceID         // or set of attributes         ServiceItem item = new ServiceItem(null, this, null);         ServiceRegistration reg = null;         try {             // ask to register for 10,000,000 milliseconds             reg = registrar.register(item, 10000000L);         } catch(java.rmi.RemoteException e) {             System.err.println("Register exception: " + e.toString());         }         System.out.println("Service registered");         // we can exit here if the exported service object can do         // everything, or we can sleep if it needs to communicate         // to us or we need to renew a lease later         //         // Typically, we will need to renew a lease later     } } // SimpleService 

Running the SimpleService

The SimpleService program needs to be compiled and run with jini-core.jar in its CLASSPATH . When run, it will attempt to connect to the service locator, so obviously one needs to be running on the machine specified in order for this to happen. Otherwise, it will throw an exception and terminate.

The instance data for the service object is transferred in serialized form across socket connections. This instance data is kept in this serialized form by the lookup services. Later, when a client asks for the service to be reconstituted, it will use this instance data and also will need the class files. At this point, the class files will also need to be transferred, probably by an HTTP server. There is no need for additional RMI support services, such as rmiregistry or rmid , since all registration is done by the register() method.

Information from the ServiceRegistration

The ServiceRegistrar object's register() method is used to register the service, and in doing so returns a ServiceRegistration object. This can be used to give information about the registration itself. The relevant methods are these:

 ServiceID getServiceID(); Lease getLease(); 

The service ID can be stored by the application if it is going to re-register again later. The lease object can be used to control the lease granted by the lookup locator, and it will be discussed in more detail in Chapter 7. For now, we can just use it to find out how long the lease has been granted for by using its getExpiration() method:

 long duration = reg.getLease().getExpiration() -                 System.currentTimeMillis(); System.out.println("Lease expires at: " +                    duration +                    " milliseconds from now"); 

Service ID

A service is unique in all the world. It runs on a particular machine and performs certain tasks . However, it will probably register itself with many lookup services. It should have the same "identity" on all of these. In addition, if either the service or one of these locators crashes or restarts, then this identity should be the same as before.

The ServiceID plays the role of unique identifier for a service. It is a 128-bit number generated in a pseudo-random manner and is effectively unique ”the chance that the generator might duplicate this number is vanishingly small. Services do not generate this identifier because the actual algorithm is not a public method of any class. Instead, a lookup service should be used. When a service needs a new identifier, it should register with a lookup service using a null service ID. The lookup service will then return a value.

The first time a service starts, it should ask for a service ID from the first lookup service it registers with. It should reuse this for registration with every other lookup service from then on. If it crashes and restarts, then it should use the same service ID again, which means that it should save the ID in persistent storage and retrieve it on restarting. The previous code is not well-behaved in this respect.

  


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