The UnboundID LDAP SDK for Java provides basic support for client-side failover and load balancing through the ServerSet class. Whenever it attempts to create a connection to a directory server, the LDAP SDK can use a ServerSet instance to establish that connection. If the server set is configured with information about multiple directory servers, then it will be responsible for selecting which server to use and establishing the connection. If the initially-selected server is not available, then it may attempt to connect to one or more of the other defined servers before giving up and throwing an exception.
It is also possible to create an LDAPConnectionPool instance using a server set, and in that case that server set will be used to create each connection that is part of the pool. This may allow a pool to be created with connections to multiple servers, which can provide a basic form of client-side load balancing.
The LDAP SDK is currently provided with the following server set implementations:
If you wish to create individual connections using a server set, then it is only necessary to call the getConnection() method for that server set. For example:
String[] addresses = { "server1.example.com", "server2.example.com" }; int[] ports = { 389, 389 }; FailoverServerSet failoverSet = new FailoverServerSet(addresses, ports); LDAPConnection connection = failoverSet.getConnection();
Note that in most cases, it is desirable to create a server set ahead of time and keep a reference to it to be used whenever a new connection is to be created. In fact, this is necessary if you want to be able to fully utilize the capabilities of some server sets. For example, the round robin server set always loops through the servers in the order they were provided, so if you create an instance of the round robin server set and then immediately use it to obtain a connection, then that connection will always be established to the first server (unless it happens to be unavailable, in which case it will try other servers in the list in the provided order). All of the server set implementations provided as part of the UnboundID LDAP SDK for Java are threadsafe, so the getConnection method may be called concurrently by separate threads.
It is also possible to use a server set when creating a connection pool. In that case, all of the connections in that pool will be created by the server set, and therefore the pool may contain connections to multiple different servers. For example:
String[] addresses = { "server1.example.com", "server2.example.com" }; int[] ports = { 389, 389 }; RoundRobinServerSet roundRobinSet = new RoundRobinServerSet(addresses, ports); BindRequest bindRequest = new SimpleBindRequest("uid=pool.user,dc=example,dc=com", "password"); LDAPConnectionPool pool = new LDAPConnectionPool(roundRobinSet, bindRequest, 10);
The above example will create a connection pool with ten connections that are authenticated as the "uid=pool.user,dc=example,dc=com" user. If both servers are available, then the pool will have five connections each to server1.example.com:389 and server2.example.com:389, and operations processed in the pool should be roughly evenly balanced between the two servers.