Java 设置Ldap搜索上的方法超时 private Authentication authenticateUserPassword(UsernamePasswordAuthenticationToken令牌)引发NamingException{ 对象登录=登录(令牌); debug(“启动身份验证登录='{}',登录名); 对象密码=token.getCredentials(); LdapContext ctx=createLdapCtx(登录,密码); SearchControls ctrls=createSearchControls(); String filter=String.format(this.filter,login); NamingEnumeration ne=ctx.search(dn、过滤器、CTRL); ....

Java 设置Ldap搜索上的方法超时 private Authentication authenticateUserPassword(UsernamePasswordAuthenticationToken令牌)引发NamingException{ 对象登录=登录(令牌); debug(“启动身份验证登录='{}',登录名); 对象密码=token.getCredentials(); LdapContext ctx=createLdapCtx(登录,密码); SearchControls ctrls=createSearchControls(); String filter=String.format(this.filter,login); NamingEnumeration ne=ctx.search(dn、过滤器、CTRL); ....,java,timeout,blocking,nonblocking,Java,Timeout,Blocking,Nonblocking,我有以下登录用户的方法。它依赖于LDAP。有时它挂在最后一行。我不知道为什么。有时它会在性能测试中复制 是否有办法等待一段时间,若方法并没有响应—返回一些预定义的值 附言 private LdapContext createLdapCtx(对象登录,对象密码)引发NamingException{ Hashtable props=新的Hashtable(); 道具放置(Context.INITIAL\u Context\u工厂,工厂); props.put(Context.PROVIDER\uUR

我有以下登录用户的方法。它依赖于LDAP。有时它挂在最后一行。我不知道为什么。有时它会在性能测试中复制

是否有办法等待一段时间,若方法并没有响应—返回一些预定义的值

附言

private LdapContext createLdapCtx(对象登录,对象密码)引发NamingException{
Hashtable props=新的Hashtable();
道具放置(Context.INITIAL\u Context\u工厂,工厂);
props.put(Context.PROVIDER\uURL,URL);
props.put(Context.SECURITY_认证,“simple”);
put(Context.SECURITY_PRINCIPAL,String.format(domain,login));
props.put(Context.SECURITY_凭证、password.toString());
返回新的InitialLdapContext(props,null);
}
您可以设置:

新的环境属性:
com.sun.jndi.ldap.read.timeout
可用于指定ldap操作的读取超时。此属性的值是整数的字符串表示形式,表示ldap操作的读取超时(以毫秒为单位)

因此,您只需更新
createLdapCtx
方法,将该环境变量指定为您选择的值:

private LdapContext createLdapCtx(Object login, Object password) throws NamingException {
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put(Context.INITIAL_CONTEXT_FACTORY, factory);
    props.put(Context.PROVIDER_URL, url);
    props.put(Context.SECURITY_AUTHENTICATION, "simple");
    props.put(Context.SECURITY_PRINCIPAL, String.format(domain, login));
    props.put(Context.SECURITY_CREDENTIALS, password.toString());

    return new InitialLdapContext(props, null);
}
如果服务器在1秒内没有响应,这将导致LDAP服务提供程序中止读取尝试。如果达到超时,将出现
NamingException



请注意,从中,您不能使用方法
SearchControls.setTimeLimit
,因为此参数不适用于读取超时。

您的SearchControls很可能有一个设置超时的方法。您可以检查它是否有超时吗?您应该能够执行
ctrls.setTimeLimit(ms)

@realponsust和Tunaki请澄清difference@gstackoverflow这可能不完全正确(我不确定所有贪婪的细节),但
setTimeLimit
不用于搜索操作(我不确定原因是什么)NamingException是常见的ldap异常。我没有检查,但我认为会抛出TimeLimitExceedexception
private LdapContext createLdapCtx(Object login, Object password) throws NamingException {
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put(Context.INITIAL_CONTEXT_FACTORY, factory);
    props.put(Context.PROVIDER_URL, url);
    props.put(Context.SECURITY_AUTHENTICATION, "simple");
    props.put(Context.SECURITY_PRINCIPAL, String.format(domain, login));
    props.put(Context.SECURITY_CREDENTIALS, password.toString());

    return new InitialLdapContext(props, null);
}
props.put("com.sun.jndi.ldap.read.timeout", "1000"); // 1 second of timeout here