Basic Lookup Using the Service Discovery Manager
Version 1.00 – May 14, 2004
Example logo
SDM examples main page
prev | next

Example 1: Basic Lookup

The Service Discovery Manager (SDM) provides the same basic lookup functionality found in the ServiceRegistrar interface. It also adds local filtering, lookup discovery, and other capabilities that will be discussed in subsequent examples. Example 1 provides a simple SDM lookup program. Please review the code in the file BasicLookup.java. A detailed explanation of that code follows.

Dissecting the Example

The code in Section 1 (see the comment labeled "// 1:") creates a default service discovery manager by passing null values to the arguments of its constructor. In this case, the SDM implicitly creates a LookupDiscoveryManager (LDM), which is a subclass of DiscoveryManagement, and a LeaseRenewalManager (LRM). The implicitly created LDM object is configured to search only for services that join with the public group. The implicitly created LRM object is used to manage leases obtained by the service discovery manager.

Note that references to the DiscoveryManagement and LeaseRenewalManager objects used by the service discovery manager can be subsequently obtained through the getDiscoveryManager and getLeaseRenewalManager methods, respectively. An implicitly created DiscoveryManagement object has the same lifetime as its associated service discovery manager object. It will be terminated when the service discovery manager is terminated. There is no such restriction on any implicitly created LeaseRenewalManager objects.

Upon construction, the service discovery manager may initiate the multicast discovery process, which can throw an IOException. This potential exception is handled through the try-catch block surrounding the service discovery manager's constructor.

The code in Section 2 creates a ServiceTemplate used to query for any service instance that implements the TransactionManager interface. A transaction manager service implementation is provided with the starter kit. Please refer to the starter kit's "Starting Services" documentation for details on starting a transaction manager service.

The code in Section 3 performs a non-blocking lookup query after an arbitrarily short delay. A delay is necessary in order to allow the lookup discovery process some time to locate available lookup service instances before the lookup query is performed. Blocking queries, described later, provide similar functionality without explicitly programming a timing delay.

The code in Section 4 displays the results of the lookup service query. If a transaction manager is found, its ServiceID is displayed. Otherwise, a message is displayed indicating that a service could not be found before the time-out value was reached. Normally, the service reference would be used to do something more interesting, but the lookup process is the focus of this example.

Finally, the code in Section 5 calls the terminate method on the service discovery manager. This allows the service discovery manager to release any obtained resources. This call is not strictly necessary here, because the main method is exiting, but it's good programming practice.

Note that the terminate method will also terminate any implicitly created LookupDiscoveryManager. The service discovery manager will not call terminate for any externally created DiscoveryManagement object that was passed into its constructor. It is the entity's responsibility to explicitly call terminate on a DiscoveryManagement object that was explicitly created outside the service discovery manager's constructor.

Running the Example

The example can be run on a UNIX platform by doing the following:

	$ cd bin12 (or bin20 for Jini 2.0 environments)
	$ run_basic_lookup.sh

The example can be run on a Windows platform by doing the following:

	$ cd bat12 (or bat20 for Jini 2.0 environments)
	$ run_basic_lookup.bat

Assuming there is at least one publicly available lookup service and one publicly available transaction manager service, you should see output similar to the following:

 
 pion 231 =>run_basic_lookup.sh
+ hostname 
CODEBASEHOST=pion
+ . JINI_HOME.sh 
EXJINIHOME=/files/jini1_2_1
+ java -Djava.security.policy=../policy/policy.all -Djava.rmi.server.codebase=http://pion:8081/sdm-dl.jar -jar ../lib12/BasicLookup.jar 
Creating ServiceDiscoveryManager ...
Creating ServiceTemplate for a net.jini.core.transaction.server.TransactionManager instance
Attempting service lookup for a net.jini.core.transaction.server.TransactionManager instance
Number of lookup services discovered: 1
    Service Registrar: c163ea3b-7874-499c-bd0d-95f421348ae9
        Group: 
TransactionManager found, id=8852573c-fb09-4119-a7e9-e63ca90073fc

If not, then please refer to Appendix A for troubleshooting advice. Appendix A also contains another non-blocking lookup example that returns multiple service references.

SDM examples main page
prev | next
Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.