Menu

Passing an HealthCheck() method to LDAPConnectionPool creation

2013-12-04
2013-12-05
  • Oscar Golden L.

    Oscar Golden L. - 2013-12-04

    Hi,
    When creating a new LDAP connection with a number of initial connections, I was wondering how it is possible to specify to the constructor a healthCheck() method we want to apply at the creation of the initial connections.
    By default the constructor makes use of the default healthCheck = new LDAPConnectionPoolHealthCheck(); which is basically empty.

    Thanks

     
  • Neil Wilson

    Neil Wilson - 2013-12-04

    At present, the connection pool doesn't really provide a way to do that, and you are right to point that out as an oversight. I'll update the code to provide constructors that allow you to provide a health check.

    For now, you do have a couple of options that you could use. One option would be to create a custom ServerSet implementation that wraps another server set and overrides the health check that should be used when creating connections. For example:

    public final class HealthCheckWrapperServerSet
           extends ServerSet
    {
      private final LDAPConnectionPoolHealthCheck overrideHealthCheck;
      private final ServerSet wrappedServerSet;
    
      public HealthCheckWrapperServerSet(final ServerSet wrappedServerSet,
                  final LDAPConnectionPoolHealthCheck overrideHealthCheck)
      {
        this.wrappedServerSet    = wrappedServerSet;
        this.overrideHealthCheck = overrideHealthCheck;
      }
    
      @Override()
      public LDAPConnection getConnection()
             throws LDAPException
      {
        return getConnection(overrideHealthCheck);
      }
    
      @Override()
      public LDAPConnection getConnection(
           final LDAPConnectionPoolHealthCheck healthCheck)
           throws LDAPException
      {
        return wrappedServerSet.getConnection(overrideHealthCheck);
      }
    }
    

    Another option would be to create a connection pool with a single initial connection, then validate that connection with the health check after the fact and manually trigger the creation of the remaining connections. For example:

    // Create a connection pool with a single initial connection.
    final LDAPConnectionPool pool = new LDAPConnectionPool(
         serverSet, bindRequest, 1, maxConnections);
    
    // Set the pool that will be used for subsequent connections
    pool.setHealthCheck(healthCheck);
    
    // Trigger the creation of the maximum number of connections
    // by checking them all out of the pool.  All but the first
    // connection will use the configured health check.
    final LDAPConnection[] conns = new LDAPConnection[maxConnections];
    for (int i=0; i < maxConnections; i++)
    {
      conns[i] = pool.getConnection();
    }
    
    // Release all the connections back to the pool.  Manually
    // validate the first one with the health check.
    for (int i=0; i < maxConnections; i++)
    {
      if (i == 0)
      {
        try
        {
          healthCheck.ensureNewConnectionValid(conns[i]);
          pool.releaseConnection(conns[i]);
        }
        catch (final LDAPException le)
        {
          pool.releaseDefunctConnection(conns[i]);
        }
      }
      else
      {
        pool.releaseConnection(conns[i]);
      }
    }
    
     
  • Neil Wilson

    Neil Wilson - 2013-12-04

    I have just committed a change to the LDAP SDK that adds a couple of LDAPConnectionPool constructors that allow you to provide a health check instance.

    By the way, I went back and looked at the code and if you're creating a connection pool with a server set, then you can provide a value of zero for the initial number of connections. That allows you to create a pool without any connections, set the health check for the pool, and then cause it to create as many connections as you want it to include. That would make the second example I provided in my previous comment a little simpler.

     
  • Oscar Golden L.

    Oscar Golden L. - 2013-12-05

    Thank again for the support.

    Before I posted this message, I had essentially what you described essentially in your last reply: pool is created with no initial connection. The healthCheck is applied after the connection is being created.
    We have a requirement to have a few connections connected at boot time before the traffic kicks in. I was ready to wait to get this improvement.
    Thanks for your quick reply.

     
    • Neil Wilson

      Neil Wilson - 2013-12-05

      FYI, as I demonstrated in my example, you can trigger the connections to be created by checking them out of the pool and temporarily putting them in a list or array so that you can release them back to the pool. You can do this immediately after creating the pool without needing to wait for other operations that would cause them to be created.

       
  • Oscar Golden L.

    Oscar Golden L. - 2013-12-05

    Any idea when this change will be available on Maven repo ?

     
    • Neil Wilson

      Neil Wilson - 2013-12-05

      This change won't appear in the Maven Central Repository until the next release of the LDAP SDK, which will probably be in a few months. If you want this change now, the best way to get it is to check out the LDAP SDK from the source repository (svn checkout svn://svn.code.sf.net/p/ldap-sdk/code/trunk ldap-sdk) and build it for yourself (./build-se.sh or build-se.bat, depending on your platform).

       

Log in to post a comment.