Java LDAP 和 JNDI 的 ServiceUnavailableException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18630567/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:40:31  来源:igfitidea点击:

ServiceUnavailableException with LDAP and JNDI

javasocketstcpldapjndi

提问by Hrvoje G.

I am using JNDI to connect to LDAP and I need it to work with many simultaneous TCP connections. All connections need to be established at the same time. The mystery is that sometimes it works and sometimes it establishes just some connections e.g. 17 of 30. I saw similar problems by googling but nobody provided their solution. Thanks! Stack trace:

我正在使用 JNDI 连接到 LDAP,我需要它与许多同时进行的 TCP 连接一起工作。所有连接都需要同时建立。神秘的是,有时它有效,有时它只建立一些连接,例如 30 个中的 17 个。我通过谷歌搜索看到了类似的问题,但没有人提供他们的解决方案。谢谢!堆栈跟踪:

javax.naming.ServiceUnavailableException: 172.16.0.xxx:389; socket closed
at com.sun.jndi.ldap.Connection.readReply(Connection.java:464)
at com.sun.jndi.ldap.LdapClient.ldapBind(LdapClient.java:370)
at com.sun.jndi.ldap.LdapClient.authenticate(LdapClient.java:222)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2752)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:328)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:205)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:223)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:166)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:96)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:695)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:318)
at javax.naming.InitialContext.init(InitialContext.java:253)
at javax.naming.InitialContext.<init>(InitialContext.java:227)
at javax.naming.directory.InitialDirContext.<init>(InitialDirContext.java:112)

EDIT: some code

编辑:一些代码

socket = new Socket(hostname, port);
is = socket.getInputStream();
os = socket.getOutputStream();
bif = new BufferedInputStream(is);
bof = new BufferedOutputStream(os);

env = new Hashtable<String, String>();
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, usr);
env.put(Context.SECURITY_CREDENTIALS, pwd);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.REFERRAL,"follow");
env.put("java.naming.ldap.derefAliases", "never");
env.put(Context.PROVIDER_URL, "ldap:// /*address*/);
try {
    ctx = new InitialDirContext(env);//ERROR
} catch (ServiceUnavailableException e1) {
    e1.printStackTrace();
    System.out.println("service unavailable");
}

采纳答案by Hrvoje G.

Just to share that the problem was with limited file descriptors on ldap server and after raising the limit it works fine.

只是为了分享问题出在 ldap 服务器上的文件描述符有限,并且在提高限制后它工作正常。