Reputation: 787
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. I cannot set the timeout correctly in linux, obtaining no NamingException. What should i do?
Upvotes: 0
Views: 1431
Reputation: 11134
JNDI should not be used in new code for many reasons, one of which is the timeout problem, though there are many others:
Use the UnboundID LDAP SDK instead. This SDK has support not only for connection timeouts, but operation timeouts in general, and support for abandoning operations automatically when they timeout.
Upvotes: 0
Reputation: 310913
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."
This must mean that the property itself was introduced in 1.6, because Sockets have had read timeouts since the year dot.
Upvotes: 3