Chapter 17 Using the Asynchronous Interface This chapter shows you how to use the Asynchronous Interface to LDAP in Java applications. This chapter contains the following sections:
This chapter shows you how to use the Asynchronous Interface to LDAP in Java applications.
"Synchronous vs. Asynchronous Connections"
"Common Uses for the Asynchronous Interface"
"New Classes in the Asynchronous Interface"
"Performing Asynchronous Searches"
"Where to Go for More Information"
LDAPMessage, which is the base class for LDAP request and response messages.
LDAPResponse, which extends LDAPMessage, represents a message received from an LDAP server in response to a request.
LDAPExtendedResponse, which extends LDAPResponse. It is the response that an LDAP server returns when handling an extended operation request.
LDAPResponseListener queues LDAPResponse messages.
LDAPSearchResult, which extends LDAPMessage. It contains a single LDAP entry and is one of the responses that an LDAP server can return when handling a search request.
LDAPSearchResultReference, which extends LDAPMessage. It contains a referral and is one of the responses that an LDAP server can return when handling a search request.
LDAPSearchListener queues search results and references.
Connect to all the servers.
Create a response listener for one search.
Share the response listener all the other searches.
Obtain and process the results.
Disconnect from the servers.
import netscape.ldap.*; import java.util.*; /* This example multiplexes the input from three different servers */ public class MultiplexServers { public static void main( String[] args ) { try { LDAPAsynch[] ld = new LDAPAsynch[3]; String[] hosts = { "foo1", "foo2", "foo3" }; int[] ports = { 389, 389, 2018 } String[] bases = { "o=Airius.com", "o=Acme.com", "dc=Acme,dc=com" }; /* search for all entries with surname of Jensen */ String MY_FILTER = "sn=Jensen"; for( int i = 0; i < ld.length; i++ ) { ld[i] = new LDAPAsynch(); /* Connect to server */ ld[i].connect( hosts[i], ports[i] ); } /* Get a response listener for one search */ LDAPSearchListener l = ld[0].search( bases[0], ld.SCOPE_SUB, MY_FILTER, null, false, (LDAPSearchListener)null ); /* Share the listener */ for( i = 1; i < ld.length; i++ ) { ld[i].search( bases[i], ld[i].SCOPE_SUB, MY_FILTER, null, false, l ); } /* Loop on results until finished */ LDAPMessage msg; while( (msg = l.getResponse()) != null ) { if ( msg instanceof LDAPSearchResultReference ) { String[] urls = ((LDAPSearchResultReference)msg).getUrls(); // Do something with the referrals... } else if ( msg instanceof LDAPSearchResult ) { LDAPEntry entry = ((LDAPSearchResult)msg).getEntry(); // The rest of the processing is the same as for // a synchronous search System.out.println( entry.getDN() ); } else { // A search response LDAPResponse res = (LDAPResponse)msg; int status = res.getResultCode(); if ( status == LDAPException.SUCCESS ) { // Nothing to do } else { String err = LDAPException.errorCodeToString(status); throw new LDAPException( err, status, res.getErrorMessage(), res.getMatchedDN() ); } } } } catch ( LDAPException e ) { System.err.println( e.toString() ); } /* Done, so disconnect */ if ( ld.isConnected() ) { ld.disconnect(); } } }
Connect to the server.
Share (multiplex) the response listener with the other searches.
Disconnect from the server.
import netscape.ldap.*; import java.util.*; /* This example multiplexes the input from three searches in different subtrees of the same server */ public class MultiplexTrees { public static void main( String[] args ) { try { LDAPAsynch ld = new LDAPAsynch(); /* Connect to server */ String MY_HOST = "localhost"; int MY_PORT = 389; ld.connect( MY_HOST, MY_PORT ); String MY_FILTER = "sn=Jensen"; String[] bases = { "o=Airius.com", "o=Acme.com", "dc=Acme,dc=com" }; /* Get a response listener for one search */ LDAPSearchListener l = ld.search( bases[0], ld.SCOPE_SUB, MY_FILTER, null, false, (LDAPSearchListener)null ); /* Share the listener */ for( i = 1; i < bases.length; i++ ) { ld.search( bases[i], ld.SCOPE_SUB, MY_FILTER, null, false, l ); } /* Loop on results until finished */ LDAPMessage msg; while( (msg = l.getResponse()) != null ) { /* The rest is the same as in the previous example */ /* ... */