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 i开发者_高级运维t 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.
精彩评论