diff --git a/src/main/java/net/spy/memcached/ConnectionFactory.java b/src/main/java/net/spy/memcached/ConnectionFactory.java index 1c09cb5a7..20d18757f 100644 --- a/src/main/java/net/spy/memcached/ConnectionFactory.java +++ b/src/main/java/net/spy/memcached/ConnectionFactory.java @@ -107,6 +107,15 @@ MemcachedNode createMemcachedNode(String name, */ boolean useNagleAlgorithm(); + /** + * If true, keep alive will be used on connected sockets. + * + *

+ * See {@link java.net.Socket#setKeepAlive(boolean)} for more information. + *

+ */ + boolean getKeepAlive(); + /** * Observers that should be established at the time of connection * instantiation. diff --git a/src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java b/src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java index 62a68998f..a6a210132 100644 --- a/src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java +++ b/src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java @@ -59,6 +59,7 @@ public class ConnectionFactoryBuilder { private boolean isDaemon = true; private boolean shouldOptimize = false; private boolean useNagle = false; + protected boolean keepAlive = false; //private long maxReconnectDelay = //DefaultConnectionFactory.DEFAULT_MAX_RECONNECT_DELAY; private long maxReconnectDelay = 1; @@ -467,6 +468,9 @@ public Map getAPIReadPriority() { } /* ENABLE_REPLICATION end */ + public void setKeepAlive(boolean on) { + keepAlive = on; + } /** * Get the ConnectionFactory set up with the provided parameters. */ @@ -591,6 +595,11 @@ public boolean useNagleAlgorithm() { return useNagle; } + @Override + public boolean getKeepAlive() { + return keepAlive; + } + @Override public long getMaxReconnectDelay() { return maxReconnectDelay; diff --git a/src/main/java/net/spy/memcached/DefaultConnectionFactory.java b/src/main/java/net/spy/memcached/DefaultConnectionFactory.java index 1a055ea3a..dbe2b05ff 100644 --- a/src/main/java/net/spy/memcached/DefaultConnectionFactory.java +++ b/src/main/java/net/spy/memcached/DefaultConnectionFactory.java @@ -318,7 +318,10 @@ public Transcoder getDefaultCollectionTranscoder() { public boolean useNagleAlgorithm() { return false; } - + @Override + public boolean getKeepAlive() { + return false; + } public boolean shouldOptimize() { return true; } diff --git a/src/main/java/net/spy/memcached/MemcachedConnection.java b/src/main/java/net/spy/memcached/MemcachedConnection.java index 351a44b9f..9f43663ff 100644 --- a/src/main/java/net/spy/memcached/MemcachedConnection.java +++ b/src/main/java/net/spy/memcached/MemcachedConnection.java @@ -630,6 +630,7 @@ private MemcachedNode makeMemcachedNode(String name, SocketChannel ch = SocketChannel.open(); ch.configureBlocking(false); ch.socket().setTcpNoDelay(!connFactory.useNagleAlgorithm()); + ch.socket().setKeepAlive(connFactory.getKeepAlive()); ch.socket().setReuseAddress(true); /* The codes above can be replaced by the codes below since java 1.7 */ // ch.setOption(StandardSocketOptions.TCP_NODELAY, !f.useNagleAlgorithm()); @@ -1307,6 +1308,7 @@ private void attemptReconnects() { ch = SocketChannel.open(); ch.configureBlocking(false); ch.socket().setTcpNoDelay(!connFactory.useNagleAlgorithm()); + ch.socket().setKeepAlive(connFactory.getKeepAlive()); ch.socket().setReuseAddress(true); /* The codes above can be replaced by the codes below since java 1.7 */ // ch.setOption(StandardSocketOptions.TCP_NODELAY, !f.useNagleAlgorithm()); diff --git a/src/test/java/net/spy/memcached/ClientBaseCase.java b/src/test/java/net/spy/memcached/ClientBaseCase.java index c8dc7f3df..8a9f55c96 100644 --- a/src/test/java/net/spy/memcached/ClientBaseCase.java +++ b/src/test/java/net/spy/memcached/ClientBaseCase.java @@ -152,6 +152,11 @@ public boolean useNagleAlgorithm() { return inner.useNagleAlgorithm(); } + @Override + public boolean getKeepAlive() { + return inner.getKeepAlive(); + } + @Override public Collection getInitialObservers() { return observers;