com.unboundid.ldap.sdk
Class FailoverServerSet

java.lang.Object
  extended by com.unboundid.ldap.sdk.ServerSet
      extended by com.unboundid.ldap.sdk.FailoverServerSet

@NotMutable
@ThreadSafety(level=COMPLETELY_THREADSAFE)
public final class FailoverServerSet
extends ServerSet

This class provides a server set implementation that will attempt to establish connections to servers in the order they are provided. If the first server is unavailable, then it will attempt to connect to the second, then to the third, etc. Note that this implementation also makes it possible to use failover between distinct server sets, which means that it will first attempt to obtain a connection from the first server set and if all attempts fail, it will proceed to the second set, and so on. This can provide a significant degree of flexibility in complex environments (e.g., first use a round robin server set containing servers in the local data center, but if none of those are available then fail over to a server set with servers in a remote data center).

Example

The following example demonstrates the process for creating a failover server set with information about individual servers. It will first try to connect to ds1.example.com:389, but if that fails then it will try connecting to ds2.example.com:389:
 // Create arrays with the addresses and ports of the directory server
 // instances.
 String[] addresses =
 {
   server1Address,
   server2Address
 };
 int[] ports =
 {
   server1Port,
   server2Port
 };

 // Create the server set using the address and port arrays.
 FailoverServerSet failoverSet = new FailoverServerSet(addresses, ports);

 // Verify that we can establish a single connection using the server set.
 LDAPConnection connection = failoverSet.getConnection();
 RootDSE rootDSEFromConnection = connection.getRootDSE();
 connection.close();

 // Verify that we can establish a connection pool using the server set.
 SimpleBindRequest bindRequest =
      new SimpleBindRequest("uid=pool.user,dc=example,dc=com", "password");
 LDAPConnectionPool pool =
      new LDAPConnectionPool(failoverSet, bindRequest, 10);
 RootDSE rootDSEFromPool = pool.getRootDSE();
 pool.close();
 
This second example demonstrates the process for creating a failover server set which actually fails over between two different data centers (east and west), with each data center containing two servers that will be accessed in a round-robin manner. It will first try to connect to one of the servers in the east data center, and if that attempt fails then it will try to connect to the other server in the east data center. If both of them fail, then it will try to connect to one of the servers in the west data center, and finally as a last resort the other server in the west data center:
 // Create a round-robin server set for the servers in the "east" data
 // center.
 String[] eastAddresses =
 {
   eastServer1Address,
   eastServer2Address
 };
 int[] eastPorts =
 {
   eastServer1Port,
   eastServer2Port
 };
 RoundRobinServerSet eastSet =
      new RoundRobinServerSet(eastAddresses, eastPorts);

 // Create a round-robin server set for the servers in the "west" data
 // center.
 String[] westAddresses =
 {
   westServer1Address,
   westServer2Address
 };
 int[] westPorts =
 {
   westServer1Port,
   westServer2Port
 };
 RoundRobinServerSet westSet =
      new RoundRobinServerSet(westAddresses, westPorts);

 // Create the failover server set across the east and west round-robin sets.
 FailoverServerSet failoverSet = new FailoverServerSet(eastSet, westSet);

 // Verify that we can establish a single connection using the server set.
 LDAPConnection connection = failoverSet.getConnection();
 RootDSE rootDSEFromConnection = connection.getRootDSE();
 connection.close();

 // Verify that we can establish a connection pool using the server set.
 SimpleBindRequest bindRequest =
      new SimpleBindRequest("uid=pool.user,dc=example,dc=com", "password");
 LDAPConnectionPool pool =
      new LDAPConnectionPool(failoverSet, bindRequest, 10);
 RootDSE rootDSEFromPool = pool.getRootDSE();
 pool.close();
 


Constructor Summary
FailoverServerSet(java.util.List<ServerSet> serverSets)
          Creates a new failover server set that will fail over between the provided server sets.
FailoverServerSet(ServerSet... serverSets)
          Creates a new failover server set that will fail over between the provided server sets.
FailoverServerSet(java.lang.String[] addresses, int[] ports)
          Creates a new failover server set with the specified set of directory server addresses and port numbers.
FailoverServerSet(java.lang.String[] addresses, int[] ports, LDAPConnectionOptions connectionOptions)
          Creates a new failover server set with the specified set of directory server addresses and port numbers.
FailoverServerSet(java.lang.String[] addresses, int[] ports, javax.net.SocketFactory socketFactory)
          Creates a new failover server set with the specified set of directory server addresses and port numbers.
FailoverServerSet(java.lang.String[] addresses, int[] ports, javax.net.SocketFactory socketFactory, LDAPConnectionOptions connectionOptions)
          Creates a new failover server set with the specified set of directory server addresses and port numbers.
 
Method Summary
 LDAPConnection getConnection()
          Attempts to establish a connection to one of the directory servers in this server set.
 LDAPConnection getConnection(LDAPConnectionPoolHealthCheck healthCheck)
          Attempts to establish a connection to one of the directory servers in this server set, using the provided health check to further validate the connection.
 java.lang.Long getMaxFailoverConnectionAgeMillis()
          Retrieves the maximum connection age that should be used for "failover" connections (i.e., connections that are established to any server other than the most-preferred server, or established using any server set other than the most-preferred set).
 ServerSet[] getServerSets()
          Retrieves the server sets over which failover will occur.
 boolean reOrderOnFailover()
          Indicates whether the list of servers or server sets used by this failover server set should be re-ordered in the event that a failure is encountered while attempting to establish a connection.
 void setMaxFailoverConnectionAgeMillis(java.lang.Long maxFailoverConnectionAge)
          Specifies the maximum connection age that should be used for "failover" connections (i.e., connections that are established to any server other than the most-preferred server, or established using any server set other than the most-preferred set).
 void setReOrderOnFailover(boolean reOrderOnFailover)
          Specifies whether the list of servers or server sets used by this failover server set should be re-ordered in the event that a failure is encountered while attempting to establish a connection.
 void toString(java.lang.StringBuilder buffer)
          Appends a string representation of this server set to the provided buffer.
 
Methods inherited from class com.unboundid.ldap.sdk.ServerSet
toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FailoverServerSet

public FailoverServerSet(java.lang.String[] addresses,
                         int[] ports)
Creates a new failover server set with the specified set of directory server addresses and port numbers. It will use the default socket factory provided by the JVM to create the underlying sockets.

Parameters:
addresses - The addresses of the directory servers to which the connections should be established. It must not be null or empty.
ports - The ports of the directory servers to which the connections should be established. It must not be null, and it must have the same number of elements as the addresses array. The order of elements in the addresses array must correspond to the order of elements in the ports array.

FailoverServerSet

public FailoverServerSet(java.lang.String[] addresses,
                         int[] ports,
                         LDAPConnectionOptions connectionOptions)
Creates a new failover server set with the specified set of directory server addresses and port numbers. It will use the default socket factory provided by the JVM to create the underlying sockets.

Parameters:
addresses - The addresses of the directory servers to which the connections should be established. It must not be null or empty.
ports - The ports of the directory servers to which the connections should be established. It must not be null, and it must have the same number of elements as the addresses array. The order of elements in the addresses array must correspond to the order of elements in the ports array.
connectionOptions - The set of connection options to use for the underlying connections.

FailoverServerSet

public FailoverServerSet(java.lang.String[] addresses,
                         int[] ports,
                         javax.net.SocketFactory socketFactory)
Creates a new failover server set with the specified set of directory server addresses and port numbers. It will use the provided socket factory to create the underlying sockets.

Parameters:
addresses - The addresses of the directory servers to which the connections should be established. It must not be null or empty.
ports - The ports of the directory servers to which the connections should be established. It must not be null, and it must have the same number of elements as the addresses array. The order of elements in the addresses array must correspond to the order of elements in the ports array.
socketFactory - The socket factory to use to create the underlying connections.

FailoverServerSet

public FailoverServerSet(java.lang.String[] addresses,
                         int[] ports,
                         javax.net.SocketFactory socketFactory,
                         LDAPConnectionOptions connectionOptions)
Creates a new failover server set with the specified set of directory server addresses and port numbers. It will use the provided socket factory to create the underlying sockets.

Parameters:
addresses - The addresses of the directory servers to which the connections should be established. It must not be null or empty.
ports - The ports of the directory servers to which the connections should be established. It must not be null, and it must have the same number of elements as the addresses array. The order of elements in the addresses array must correspond to the order of elements in the ports array.
socketFactory - The socket factory to use to create the underlying connections.
connectionOptions - The set of connection options to use for the underlying connections.

FailoverServerSet

public FailoverServerSet(ServerSet... serverSets)
Creates a new failover server set that will fail over between the provided server sets.

Parameters:
serverSets - The server sets between which failover should occur. It must not be null or empty.

FailoverServerSet

public FailoverServerSet(java.util.List<ServerSet> serverSets)
Creates a new failover server set that will fail over between the provided server sets.

Parameters:
serverSets - The server sets between which failover should occur. It must not be null or empty.
Method Detail

getServerSets

public ServerSet[] getServerSets()
Retrieves the server sets over which failover will occur. If this failover server set was created from individual servers rather than server sets, then the elements contained in the returned array will be SingleServerSet instances.

Returns:
The server sets over which failover will occur.

reOrderOnFailover

public boolean reOrderOnFailover()
Indicates whether the list of servers or server sets used by this failover server set should be re-ordered in the event that a failure is encountered while attempting to establish a connection. If true, then any failed attempt to establish a connection to a server set at the beginning of the list may cause that server/set to be moved to the end of the list so that it will be the last one tried on the next attempt.

Returns:
true if the order of elements in the associated list of servers or server sets should be updated if a failure occurs while attempting to establish a connection, or false if the original order should be preserved.

setReOrderOnFailover

public void setReOrderOnFailover(boolean reOrderOnFailover)
Specifies whether the list of servers or server sets used by this failover server set should be re-ordered in the event that a failure is encountered while attempting to establish a connection. By default, the original order will be preserved, but if this method is called with a value of true, then a failed attempt to establish a connection to the server or server set at the beginning of the list may cause that server to be moved to the end of the list so that it will be the last server/set tried on the next attempt.

Parameters:
reOrderOnFailover - Indicates whether the list of servers or server sets should be re-ordered in the event that a failure is encountered while attempting to establish a connection.

getMaxFailoverConnectionAgeMillis

public java.lang.Long getMaxFailoverConnectionAgeMillis()
Retrieves the maximum connection age that should be used for "failover" connections (i.e., connections that are established to any server other than the most-preferred server, or established using any server set other than the most-preferred set). This will only be used if this failover server set is used to create an LDAPConnectionPool, for connections within that pool.

Returns:
The maximum connection age that should be used for failover connections, a value of zero to indicate that no maximum age should apply to those connections, or null if the maximum connection age should be determined by the associated connection pool.

setMaxFailoverConnectionAgeMillis

public void setMaxFailoverConnectionAgeMillis(java.lang.Long maxFailoverConnectionAge)
Specifies the maximum connection age that should be used for "failover" connections (i.e., connections that are established to any server other than the most-preferred server, or established using any server set other than the most-preferred set). This will only be used if this failover server set is used to create an LDAPConnectionPool, for connections within that pool.

Parameters:
maxFailoverConnectionAge - The maximum connection age that should be used for failover connections. It may be less than or equal to zero to indicate that no maximum age should apply to such connections, or null to indicate that the maximum connection age should be determined by the associated connection pool.

getConnection

public LDAPConnection getConnection()
                             throws LDAPException
Attempts to establish a connection to one of the directory servers in this server set. The connection should be established but unauthenticated. The caller may determine the server to which the connection is established using the LDAPConnection.getConnectedAddress() and LDAPConnection.getConnectedPort() methods.

Specified by:
getConnection in class ServerSet
Returns:
An LDAPConnection object that is established to one of the servers in this server set.
Throws:
LDAPException - If it is not possible to establish a connection to any of the servers in this server set.

getConnection

public LDAPConnection getConnection(LDAPConnectionPoolHealthCheck healthCheck)
                             throws LDAPException
Attempts to establish a connection to one of the directory servers in this server set, using the provided health check to further validate the connection. The connection should be established but unauthenticated. The caller may determine the server to which the connection is established using the LDAPConnection.getConnectedAddress() and LDAPConnection.getConnectedPort() methods.

Overrides:
getConnection in class ServerSet
Parameters:
healthCheck - The health check to use to make the determination, or null if no additional health check should be performed.
Returns:
An LDAPConnection object that is established to one of the servers in this server set.
Throws:
LDAPException - If it is not possible to establish a connection to any of the servers in this server set.

toString

public void toString(java.lang.StringBuilder buffer)
Appends a string representation of this server set to the provided buffer.

Overrides:
toString in class ServerSet
Parameters:
buffer - The buffer to which the string representation should be appended.