Created
September 11, 2016 10:03
-
-
Save zonia3000/7d8f219461de3100665c65102d84c8e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Socket factory with keep alive setting. | |
* For using with com.sun.jndi.ldap.LdapCtxFactory add it in the env Hastable: | |
* env.put("java.naming.ldap.factory.socket", "yourpackage.KeepAliveSocketFactory"); | |
*/ | |
public class KeepAliveSocketFactory extends SocketFactory { | |
@Override | |
public Socket createSocket() throws IOException { | |
Socket socket = new Socket(); | |
socket.setKeepAlive(true); | |
return socket; | |
} | |
@Override | |
public Socket createSocket(InetAddress host, int port) throws IOException { | |
Socket socket = new Socket(host, port); | |
socket.setKeepAlive(true); | |
return socket; | |
} | |
@Override | |
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException { | |
Socket socket = new Socket(address, port, localAddress, localPort); | |
socket.setKeepAlive(true); | |
return socket; | |
} | |
@Override | |
public Socket createSocket(String host, int port) throws IOException, SocketException { | |
Socket socket = new Socket(host, port); | |
socket.setKeepAlive(true); | |
return socket; | |
} | |
@Override | |
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException { | |
Socket socket = new Socket(host, port, localHost, localPort); | |
socket.setKeepAlive(true); | |
return socket; | |
} | |
public static SocketFactory getDefault() { | |
synchronized (KeepAliveSocketFactory.class) { | |
return new KeepAliveSocketFactory(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment