簡體   English   中英

ldap超時在Linux中不起作用

[英]ldap timeout does not work in linux

我在這堂課上有問題:

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
        }
    }
}
}

它應該測試ldap調用的超時,在超時后獲取naimngException,它可以在Windows中正常工作,但在Linux中則不能,在那里繼續等待ldap連接。 我無法在linux中正確設置超時,沒有獲得NamingException。 我該怎么辦?

當您使用Java 1.5時,您顯然錯過了這一點 :“注意:在Java SDK v 6.0之前的系統上,此屬性將被忽略,因為SDK不支持讀取超時。”

這必須意味着該屬性本身是在1.6中引入的,因為Socket自年點以來已讀取超時。

出於多種原因,不應在新代碼中使用JNDI,其中一個原因是超時問題,盡管還有許多其他原因:

  • JNDI使用斷開的配置
  • JNDI不支持完整的LDAP標准
  • 網上的JNDI例子真是可怕

請改用UnboundID LDAP SDK 此SDK不僅支持連接超時,而且還支持一般的操作超時,並且支持在超時時自動放棄操作。

也可以看看

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM