简体   繁体   English

ldap超时在Linux中不起作用

[英]ldap timeout does not work in linux

I have a problem with this class: 我在这堂课上有问题:

package it.test;


import java.net.Socket;
import java.net.ServerSocket;
import java.io.*;
import javax.naming.*;
import javax.naming.directory.*;

import java.util.Calendar;
import java.util.Hashtable;

public class ReadTimeoutTest {

public static void main(String[] args) throws Exception {

    boolean passed = false;

    // Set up the environment for creating the initial context
    Hashtable<String, String> env = new Hashtable<String, String>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY,
            "com.sun.jndi.ldap.LdapCtxFactory");
    env.put("com.sun.jndi.ldap.read.timeout", "1000");
    env.put(Context.PROVIDER_URL, "ldap://localhost:2001");

    Server s = new Server();

    try {

        // start the server
        s.start();

        // Create initial context
        DirContext ctx = new InitialDirContext(env);
        System.out.println("LDAP Client: Connected to the Server");

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
        System.out.println("Server: Connection refused"+" "
        +Calendar.getInstance().getTime());
    }
    s.interrupt();
}

static class Server extends Thread {

    static int serverPort = 2001;

    Server() {
    }

    public void run() {
        try {
            ServerSocket serverSock = new ServerSocket(serverPort);
            Socket socket = serverSock.accept();
            System.out.println("Server: Connection accepted"+" "
                    +Calendar.getInstance().getTime());

            BufferedInputStream bin = new BufferedInputStream(socket.
                    getInputStream());
            while (true) {
                bin.read();
            }
        } catch (IOException e) {
            // ignore
        }
    }
}
}

It should tests timeout for ldap call, getting a naimngException after the timeout it works fine in windows but not in linux, where it continue to wait for a ldap connection. 它应该测试ldap调用的超时,在超时后获取naimngException,它可以在Windows中正常工作,但在Linux中则不能,在那里继续等待ldap连接。 I cannot set the timeout correctly in linux, obtaining no NamingException. 我无法在linux中正确设置超时,没有获得NamingException。 What should i do? 我该怎么办?

As you are using Java 1.5 you have evidently missed this : "NOTE: On systems earlier than the Java SDK, v 6.0, this property is ignored because there is no support in the SDK for read timeouts." 当您使用Java 1.5时,您显然错过了这一点 :“注意:在Java SDK v 6.0之前的系统上,此属性将被忽略,因为SDK不支持读取超时。”

This must mean that the property itself was introduced in 1.6, because Sockets have had read timeouts since the year dot. 这必须意味着该属性本身是在1.6中引入的,因为Socket自年点以来已读取超时。

JNDI should not be used in new code for many reasons, one of which is the timeout problem, though there are many others: 出于多种原因,不应在新代码中使用JNDI,其中一个原因是超时问题,尽管还有许多其他原因:

  • JNDI uses a disconnected configuration JNDI使用断开的配置
  • JNDI does not support full LDAP standard JNDI不支持完整的LDAP标准
  • JNDI examples on the net are truly horrible 网上的JNDI例子真是可怕

Use the UnboundID LDAP SDK instead. 请改用UnboundID LDAP SDK This SDK has support not only for connection timeouts, but operation timeouts in general, and support for abandoning operations automatically when they timeout. 此SDK不仅支持连接超时,而且还支持一般的操作超时,并且支持在超时时自动放弃操作。

see also 也可以看看

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

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