简体   繁体   中英

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?

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.

This is a post about this topic. Seems this property doesn't really work this way. Maybe this follow-up thread can help to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM