A first look at Jini lookup and discovery protocols
LookupDiscovery
The discovery protocol is implemented in the Jini class LookupDiscovery. In practice, LookupDiscovery uses both multicast requests and multicast announcements. At startup, LookupDiscovery actively seeks lookup services using multicast requests. When it has found all the lookup services on the network, it switches to passively listening for multicast announcements.
Jini is a toolbox to build distributed applications that dynamically communicate with each other. |
LookupDiscovery sends discovered and discarded events to the application when it finds or loses track of a lookup service. The DoLookupDiscovery application in Listing 1 demonstrates how to use it. It registers as a listener for the discovered events and prints all the lookup services available on the network.
keeps the application running forever.Thread.currentThread<br>().join()
Listing 1. DoLookupDiscovery.java
import java.rmi.*; import net.jini.discovery.*; import net.jini.core.lookup.*; public class DoLookupDiscovery { public static void main(String[] args) throws Exception { System.setSecurityManager(new RMISecurityManager()); LookupDiscovery ld = new LookupDiscovery(LookupDiscovery.NO_GROUPS); ld.addDiscoveryListener(new DiscoveryListener() { public void discovered(DiscoveryEvent de) { try { // Invoke getRegistrar() on the // LookupLocator to perform unicast // discovery of the lookup service. ServiceRegistrar[] registrars = de.getRegistrars(); for(int i = 0; i < registrars.length; i++) System.out.println( registrars[i].getLocator()); } catch (Exception e) {} } public void discarded(DiscoveryEvent de) { } }); ld.setGroups(LookupDiscovery.ALL_GROUPS); Thread.currentThread().join(); } }
Running the application
The Jini Starter Kit from Sun ships with a default lookup service called reggie. To test this application, you will need to run one or more copies of reggie. DoLookupDiscovery will print the list of available reggie. To compile and run the DoLookupDiscovery, follow these steps:
javac -classpath jini-ext.jar;jini-core.jar DoLookupDiscovery.java
grant { permission java.security.AllPermission "", ""; };
rmid
Warning! This creates a log directory that you must manually delete between calls to rmid.
java -jar c:\jini1_0\lib\reggie.jar file:///c:\jini1_0\reggie-dl.jar policy.all c:\reggie_public public
To invoke a second copy of reggie, you must change the last two parameters, for example:
java -jar c:\jini1_0\lib\reggie.jar file:///c:\jini1_0\lib\reggie-dl.jar
policy.all c:\reggie_second second
Warning! Again the fifth parameter is a log directory that you must manually delete between calls to reggie.
java -classpath c:\jini1_0\lib\jini-ext.jar;. -Djava.security.policy=policy.all DoLookupDiscovery
(Editor's note: Some lines of code above have been broken for display purposes.)
It will search for lookup services and print them.
To effectively test DoMulticast, you should start several copies of reggie, the lookup service. It is more interesting if you start some instances of reggie before running DoLookupDiscovery and others while DoLookupDiscovery is running. See DoLookupDiscovery dynamically discover new lookup services.
Conclusion
Jini is a toolbox to build distributed applications that dynamically communicate with each other. The discovery protocol allows servers to explore their environment at runtime.
Future articles in Gamelan will show you how to write your own Jini services.
About the author
Benoît Marchal is a software engineer and consultant based in Namur, Belgium. He has been working extensively on Java and XML. He also likes teaching and writing. Ben runs his own consulting company, Pineapplesoft, and can be reached at bmarchal@pineapplesoft.com. His first book, XML by Example will be published by Que at the end of the year.
Page 2 of 2
This article was originally published on November 4, 1999