简体   繁体   English

java jndi ldap连接超时

[英]java jndi ldap connection timeout

I want to control the connection timeout by setting com.sun.jndi.ldap.connect.timeout property. 我想通过设置com.sun.jndi.ldap.connect.timeout属性来控制连接超时。 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). 它适用于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? 什么可能导致这个?

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. 目标主机似乎已启动且目标LDAP服务未在端口10389上侦听。因此目标主机使用RST响应传入连接请求,因此在客户端立即抛出异常。 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. 也许这个后续主题可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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