Implements #701, add close() to JedisCluster
This commit is contained in:
@@ -2,12 +2,13 @@ package redis.clients.jedis;
|
|||||||
|
|
||||||
import redis.clients.jedis.BinaryClient.LIST_POSITION;
|
import redis.clients.jedis.BinaryClient.LIST_POSITION;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class JedisCluster implements JedisCommands, BasicCommands {
|
public class JedisCluster implements JedisCommands, BasicCommands, Closeable {
|
||||||
public static final short HASHSLOTS = 16384;
|
public static final short HASHSLOTS = 16384;
|
||||||
private static final int DEFAULT_TIMEOUT = 1;
|
private static final int DEFAULT_TIMEOUT = 1;
|
||||||
private static final int DEFAULT_MAX_REDIRECTIONS = 5;
|
private static final int DEFAULT_MAX_REDIRECTIONS = 5;
|
||||||
@@ -33,6 +34,21 @@ public class JedisCluster implements JedisCommands, BasicCommands {
|
|||||||
this.maxRedirections = maxRedirections;
|
this.maxRedirections = maxRedirections;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
if (connectionHandler != null) {
|
||||||
|
for (JedisPool pool : connectionHandler.getNodes().values()) {
|
||||||
|
try {
|
||||||
|
if (pool != null) {
|
||||||
|
pool.destroy();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
// pass
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String set(final String key, final String value) {
|
public String set(final String key, final String value) {
|
||||||
return new JedisClusterCommand<String>(connectionHandler, timeout,
|
return new JedisClusterCommand<String>(connectionHandler, timeout,
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package redis.clients.jedis.tests;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@@ -14,9 +15,11 @@ import org.junit.Test;
|
|||||||
import redis.clients.jedis.HostAndPort;
|
import redis.clients.jedis.HostAndPort;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
import redis.clients.jedis.JedisCluster;
|
import redis.clients.jedis.JedisCluster;
|
||||||
|
import redis.clients.jedis.JedisPool;
|
||||||
import redis.clients.jedis.exceptions.JedisAskDataException;
|
import redis.clients.jedis.exceptions.JedisAskDataException;
|
||||||
import redis.clients.jedis.exceptions.JedisClusterException;
|
import redis.clients.jedis.exceptions.JedisClusterException;
|
||||||
import redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException;
|
import redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException;
|
||||||
|
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||||
import redis.clients.jedis.exceptions.JedisException;
|
import redis.clients.jedis.exceptions.JedisException;
|
||||||
import redis.clients.jedis.exceptions.JedisMovedDataException;
|
import redis.clients.jedis.exceptions.JedisMovedDataException;
|
||||||
import redis.clients.jedis.tests.utils.JedisClusterTestUtil;
|
import redis.clients.jedis.tests.utils.JedisClusterTestUtil;
|
||||||
@@ -321,6 +324,33 @@ public class JedisClusterTest extends Assert {
|
|||||||
assertEquals("foo", jc.get("51"));
|
assertEquals("foo", jc.get("51"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCloseable() {
|
||||||
|
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
|
||||||
|
jedisClusterNode.add(new HostAndPort(nodeInfo1.getHost(), nodeInfo1.getPort()));
|
||||||
|
|
||||||
|
JedisCluster jc = null;
|
||||||
|
try {
|
||||||
|
jc = new JedisCluster(jedisClusterNode);
|
||||||
|
jc.set("51", "foo");
|
||||||
|
} finally {
|
||||||
|
if (jc != null) {
|
||||||
|
jc.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterator<JedisPool> poolIterator = jc.getClusterNodes().values().iterator();
|
||||||
|
while (poolIterator.hasNext()) {
|
||||||
|
JedisPool pool = poolIterator.next();
|
||||||
|
try {
|
||||||
|
pool.getResource();
|
||||||
|
fail("JedisCluster's internal pools should be already destroyed");
|
||||||
|
} catch (JedisConnectionException e) {
|
||||||
|
// ok to go...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static String getNodeServingSlotRange(String infoOutput) {
|
private static String getNodeServingSlotRange(String infoOutput) {
|
||||||
// f4f3dc4befda352a4e0beccf29f5e8828438705d 127.0.0.1:7380 master - 0 1394372400827 0 connected 5461-10922
|
// f4f3dc4befda352a4e0beccf29f5e8828438705d 127.0.0.1:7380 master - 0 1394372400827 0 connected 5461-10922
|
||||||
for (String infoLine : infoOutput.split("\n")) {
|
for (String infoLine : infoOutput.split("\n")) {
|
||||||
|
|||||||
Reference in New Issue
Block a user