dinesh pazani
dinesh pazani

Reputation: 75

How to reduced timeout exception on AD host unreachable time using unboundid SDK?

I am using UNBOUNDID SDK for AD server authentication

 public static Boolean adAuthentication(String ldapUrl, int ldapPort,
        String bindUserName, String bindPassword) {

    SSLUtil sslUtil = new SSLUtil(null, new TrustAllTrustManager());
    SocketFactory socketFactory;
    LDAPConnection ldapConnection = null;
    Boolean isAuthentic = null;
    try {
        socketFactory = sslUtil.createSSLSocketFactory();
        LDAPConnectionOptions options = new LDAPConnectionOptions();
        options.setAbandonOnTimeout(true);
        **options.setResponseTimeoutMillis(10000);
        options.setConnectTimeoutMillis(10000);**           

        ldapConnection = new LDAPConnection(socketFactory, ldapUrl, ldapPort);
        ldapConnection.setConnectionOptions(options);

        if(ldapConnection.isConnected()){

            final BindRequest bindRequest = new SimpleBindRequest(bindUserName,  bindPassword);
            final BindResult bindResult = ldapConnection.bind(bindRequest);
            final ResultCode resultCode = bindResult.getResultCode();

            isAuthentic = resultCode.equals(ResultCode.SUCCESS) ? true : false;

            ldapConnection.close();
        }
    } catch (LDAPException ldapException) {
        logger.error("AD Host Exception ::: "+ ldapException);
    } catch (GeneralSecurityException exception) {
        logger.error("AD Security Exception ::: " + exception);
    }finally{
        if(ldapConnection!= null)
            ldapConnection.close();
    }
    return isAuthentic;
}

This code working fine on AD server reachable time suppose AD server is unreachable mean it throw following error after 60 seconds

"AD Host Exception ::: LDAPException(resultCode=91 (connect error), errorMessage='An error occurred while attempting to connect to server 000.000.00.00:369: java.io.IOException: Unable to establish a connection to server 000.000.00.00:369 within the configured timeout of 60000 milliseconds.')"

But i needed throw the error within 20 seconds. I set timeout limit as follow but no effect. options.setResponseTimeoutMillis(20000); options.setConnectTimeoutMillis(20000);

Thanks.

Upvotes: 0

Views: 947

Answers (1)

Neil Wilson
Neil Wilson

Reputation: 1736

Since you're providing connection information in the constructor, then the constructor is establishing the connection. However, you're not setting the connection options until after the constructor, so the constructor is using the default connection options.

Rather than using the LDAPConnection(SocketFactory,String,int) constructor, you should use the LDAPConnection(SocketFactory,LDAPConnectionOptions,String,int) constructor. This will cause the connection establishment to use the provided connection options instead of the default.

Upvotes: 2

Related Questions