previous chapter table of contents next chapter

Receiving the ServiceMatches Object

If a client wishes to search for more than one match to a service request from a particular lookup service, then it specifies the maximum number of matches it would like returned by using the maxMatches parameter of the second lookup() method. The client gets back a ServiceMatches object that looks like this:

 package net.jini.core.lookup; public Class ServiceMatches {     public ServiceItem[] items;     public int totalMatches ; } 

The number of elements in items need not be the same as totalMatches . Suppose there are five matching services stored on the locator. In that case, totalMatches will be set to 5 after a lookup. However, if you used maxMatches to limit the search to at most two matches, then items will be set to be an array with only two elements.

In addition, not all elements of this array need be non-null! Note that in lookup(tmpl) when asking for only one match, an exception can be returned, such as when the service is not serializable. No exception is thrown here, because although one match might be bad, the others might still be okay. So a value of null as the array element value is used to signify this. The following code shows how to properly handle the ServiceMatches object:

 ServiceMatches matches = registrar.lookup(template, 10);     // NB: matches.totalMatches may be greater than matches.items.length     for (int n = 0; n < matches.items.length; n++) {     Toaster toaster = (Toaster) matches.items[n].service; if (toaster != null) {     toaster.setDarkness(1);     toaster.startToasting();     }  } 

This code will start up to ten toasters cooking at once!


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

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