Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java jndi ldap connection timeout

I want to control the connection timeout by setting com.sun.jndi.ldap.connect.timeout property. It works well for values under 1000 ms, but if I set values bigger then 1000, the timeout doesn't increase (it remains at 1000).

Here is the code I tried to test it with (the server is down):

long start = System.currentTimeMillis();

try
{
    Hashtable env = new Hashtable();

    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:10389");
    env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
    env.put(Context.SECURITY_CREDENTIALS, "secret");
    env.put("com.sun.jndi.ldap.connect.timeout", "10000");

    InitialLdapContext context = new InitialLdapContext(env, null);

} catch (NamingException e)
{
    System.out.println("Failed because " + e.getRootCause()
            .getMessage() + ". Timeout: " + ((System.currentTimeMillis() - start)) + " ms.");
}

What might cause this?

like image 281
calin014 Avatar asked Apr 27 '26 01:04

calin014


1 Answers

If the target host responds to the connect request with an error code, the connection fails as soon as the error code is received. It appears that the target host was up and the target LDAP service wasn't listening at port 10389. So the target host responded to the incoming connect request with an RST, so an exception was thrown immediately at the client. This is expected behaviour. You surely don't want to delay receiving the error? The connect timeout is for the case when the target host is temporarily unreachable or the target service is really busy.

like image 128
user207421 Avatar answered Apr 28 '26 14:04

user207421