java jndi ldap连接超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7512864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java jndi ldap connection timeout
提问by calin014
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).
我想通过设置com.sun.jndi.ldap.connect.timeout
属性来控制连接超时。它适用于 1000 毫秒以下的值,但如果我将值设置为大于 1000,超时不会增加(它保持在 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?
什么可能导致这种情况?
采纳答案by user207421
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 delayreceiving the error? The connect timeout is for the case when the target host is temporarily unreachable or the target service is reallybusy.
如果目标主机以错误代码响应连接请求,则一旦收到错误代码,连接就会失败。看起来目标主机已启动,目标 LDAP 服务未在端口 10389 上侦听。因此目标主机使用 RST 响应传入的连接请求,因此客户端立即抛出异常。这是预期的行为。您肯定不想延迟接收错误?连接超时是针对目标主机暂时不可达或目标服务真的很忙的情况。
回答by tobijdc
This is a postabout this topic. Seems this property doesn't really work this way. Maybe this follow-up threadcan help to.