Implements Closeable to Pooled Jedis & ShardedJedis
* Implement Closeable from Jedis, ShardedJedis with Pooled ** resources from JedisPool, JedisSentinelPool, ShardedJedis, ShardedJedisPool * Connection class : check whether Jedis Connection is broken ** when it's time to throw JedisConnectionException, mark Connection to broken
This commit is contained in:
@@ -11,7 +11,6 @@ import java.util.List;
|
||||
import redis.clients.jedis.Protocol.Command;
|
||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||
import redis.clients.jedis.exceptions.JedisDataException;
|
||||
import redis.clients.jedis.exceptions.JedisException;
|
||||
import redis.clients.util.RedisInputStream;
|
||||
import redis.clients.util.RedisOutputStream;
|
||||
import redis.clients.util.SafeEncoder;
|
||||
@@ -25,6 +24,8 @@ public class Connection implements Closeable {
|
||||
private int pipelinedCommands = 0;
|
||||
private int timeout = Protocol.DEFAULT_TIMEOUT;
|
||||
|
||||
private boolean broken = false;
|
||||
|
||||
public Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
@@ -45,7 +46,8 @@ public class Connection implements Closeable {
|
||||
socket.setKeepAlive(true);
|
||||
socket.setSoTimeout(0);
|
||||
} catch (SocketException ex) {
|
||||
throw new JedisException(ex);
|
||||
broken = true;
|
||||
throw new JedisConnectionException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +56,8 @@ public class Connection implements Closeable {
|
||||
socket.setSoTimeout(timeout);
|
||||
socket.setKeepAlive(false);
|
||||
} catch (SocketException ex) {
|
||||
throw new JedisException(ex);
|
||||
broken = true;
|
||||
throw new JedisConnectionException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,14 +66,6 @@ public class Connection implements Closeable {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
protected void flush() {
|
||||
try {
|
||||
outputStream.flush();
|
||||
} catch (IOException e) {
|
||||
throw new JedisConnectionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
protected Connection sendCommand(final Command cmd, final String... args) {
|
||||
final byte[][] bargs = new byte[args.length][];
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
@@ -80,17 +75,29 @@ public class Connection implements Closeable {
|
||||
}
|
||||
|
||||
protected Connection sendCommand(final Command cmd, final byte[]... args) {
|
||||
connect();
|
||||
Protocol.sendCommand(outputStream, cmd, args);
|
||||
pipelinedCommands++;
|
||||
return this;
|
||||
try {
|
||||
connect();
|
||||
Protocol.sendCommand(outputStream, cmd, args);
|
||||
pipelinedCommands++;
|
||||
return this;
|
||||
} catch (JedisConnectionException ex) {
|
||||
// Any other exceptions related to connection?
|
||||
broken = true;
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
protected Connection sendCommand(final Command cmd) {
|
||||
connect();
|
||||
Protocol.sendCommand(outputStream, cmd, new byte[0][]);
|
||||
pipelinedCommands++;
|
||||
return this;
|
||||
try {
|
||||
connect();
|
||||
Protocol.sendCommand(outputStream, cmd, new byte[0][]);
|
||||
pipelinedCommands++;
|
||||
return this;
|
||||
} catch (JedisConnectionException ex) {
|
||||
// Any other exceptions related to connection?
|
||||
broken = true;
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
public Connection(final String host, final int port) {
|
||||
@@ -139,6 +146,7 @@ public class Connection implements Closeable {
|
||||
outputStream = new RedisOutputStream(socket.getOutputStream());
|
||||
inputStream = new RedisInputStream(socket.getInputStream());
|
||||
} catch (IOException ex) {
|
||||
broken = true;
|
||||
throw new JedisConnectionException(ex);
|
||||
}
|
||||
}
|
||||
@@ -147,7 +155,7 @@ public class Connection implements Closeable {
|
||||
@Override
|
||||
public void close() {
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
if (isConnected()) {
|
||||
@@ -158,6 +166,7 @@ public class Connection implements Closeable {
|
||||
socket.close();
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
broken = true;
|
||||
throw new JedisConnectionException(ex);
|
||||
}
|
||||
}
|
||||
@@ -172,7 +181,7 @@ public class Connection implements Closeable {
|
||||
protected String getStatusCodeReply() {
|
||||
flush();
|
||||
pipelinedCommands--;
|
||||
final byte[] resp = (byte[]) Protocol.read(inputStream);
|
||||
final byte[] resp = (byte[]) readProtocolWithCheckingBroken();
|
||||
if (null == resp) {
|
||||
return null;
|
||||
} else {
|
||||
@@ -192,13 +201,13 @@ public class Connection implements Closeable {
|
||||
public byte[] getBinaryBulkReply() {
|
||||
flush();
|
||||
pipelinedCommands--;
|
||||
return (byte[]) Protocol.read(inputStream);
|
||||
return (byte[]) readProtocolWithCheckingBroken();
|
||||
}
|
||||
|
||||
public Long getIntegerReply() {
|
||||
flush();
|
||||
pipelinedCommands--;
|
||||
return (Long) Protocol.read(inputStream);
|
||||
return (Long) readProtocolWithCheckingBroken();
|
||||
}
|
||||
|
||||
public List<String> getMultiBulkReply() {
|
||||
@@ -209,29 +218,29 @@ public class Connection implements Closeable {
|
||||
public List<byte[]> getBinaryMultiBulkReply() {
|
||||
flush();
|
||||
pipelinedCommands--;
|
||||
return (List<byte[]>) Protocol.read(inputStream);
|
||||
return (List<byte[]>) readProtocolWithCheckingBroken();
|
||||
}
|
||||
|
||||
public void resetPipelinedCount() {
|
||||
pipelinedCommands = 0;
|
||||
pipelinedCommands = 0;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Object> getRawObjectMultiBulkReply() {
|
||||
return (List<Object>) Protocol.read(inputStream);
|
||||
return (List<Object>) readProtocolWithCheckingBroken();
|
||||
}
|
||||
|
||||
public List<Object> getObjectMultiBulkReply() {
|
||||
flush();
|
||||
pipelinedCommands--;
|
||||
return getRawObjectMultiBulkReply();
|
||||
flush();
|
||||
pipelinedCommands--;
|
||||
return getRawObjectMultiBulkReply();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Long> getIntegerMultiBulkReply() {
|
||||
flush();
|
||||
pipelinedCommands--;
|
||||
return (List<Long>) Protocol.read(inputStream);
|
||||
return (List<Long>) readProtocolWithCheckingBroken();
|
||||
}
|
||||
|
||||
public List<Object> getAll() {
|
||||
@@ -243,7 +252,7 @@ public class Connection implements Closeable {
|
||||
flush();
|
||||
while (pipelinedCommands > except) {
|
||||
try {
|
||||
all.add(Protocol.read(inputStream));
|
||||
all.add(readProtocolWithCheckingBroken());
|
||||
} catch (JedisDataException e) {
|
||||
all.add(e);
|
||||
}
|
||||
@@ -255,6 +264,28 @@ public class Connection implements Closeable {
|
||||
public Object getOne() {
|
||||
flush();
|
||||
pipelinedCommands--;
|
||||
return Protocol.read(inputStream);
|
||||
return readProtocolWithCheckingBroken();
|
||||
}
|
||||
|
||||
public boolean isBroken() {
|
||||
return broken;
|
||||
}
|
||||
|
||||
protected void flush() {
|
||||
try {
|
||||
outputStream.flush();
|
||||
} catch (IOException ex) {
|
||||
broken = true;
|
||||
throw new JedisConnectionException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object readProtocolWithCheckingBroken() {
|
||||
try {
|
||||
return Protocol.read(inputStream);
|
||||
} catch (JedisConnectionException exc) {
|
||||
broken = true;
|
||||
throw exc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user