add JedisDataException and JedisConnectionException

This commit is contained in:
Jonathan Leibiusky
2011-01-30 17:46:17 -03:00
parent b4ad7697b7
commit 2a4a43f4cd
31 changed files with 269 additions and 263 deletions

View File

@@ -11,6 +11,7 @@ import java.util.Map;
import java.util.Set;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.util.JedisByteHashMap;
import redis.clients.util.SafeEncoder;

View File

@@ -10,6 +10,8 @@ import static redis.clients.jedis.Protocol.Keyword.UNSUBSCRIBE;
import java.util.Arrays;
import java.util.List;
import redis.clients.jedis.exceptions.JedisException;
public abstract class BinaryJedisPubSub {
private int subscribedChannels = 0;
private Client client;

View File

@@ -3,11 +3,12 @@ package redis.clients.jedis;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import redis.clients.jedis.Protocol.Command;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.util.RedisInputStream;
import redis.clients.util.RedisOutputStream;
import redis.clients.util.SafeEncoder;
@@ -60,26 +61,14 @@ public class Connection {
}
protected Connection sendCommand(final Command cmd, final byte[]... args) {
try {
connect();
} catch (UnknownHostException e) {
throw new JedisException("Could not connect to redis-server", e);
} catch (IOException e) {
throw new JedisException("Could not connect to redis-server", e);
}
connect();
protocol.sendCommand(outputStream, cmd, args);
pipelinedCommands++;
return this;
}
protected Connection sendCommand(final Command cmd) {
try {
connect();
} catch (UnknownHostException e) {
throw new JedisException("Could not connect to redis-server", e);
} catch (IOException e) {
throw new JedisException("Could not connect to redis-server", e);
}
connect();
protocol.sendCommand(outputStream, cmd, new byte[0][]);
pipelinedCommands++;
return this;
@@ -110,12 +99,16 @@ public class Connection {
public Connection() {
}
public void connect() throws UnknownHostException, IOException {
public void connect() {
if (!isConnected()) {
socket = new Socket(host, port);
socket.setSoTimeout(timeout);
outputStream = new RedisOutputStream(socket.getOutputStream());
inputStream = new RedisInputStream(socket.getInputStream());
try {
socket = new Socket(host, port);
socket.setSoTimeout(timeout);
outputStream = new RedisOutputStream(socket.getOutputStream());
inputStream = new RedisInputStream(socket.getInputStream());
} catch (IOException ex) {
throw new JedisConnectionException(ex);
}
}
}
@@ -128,7 +121,7 @@ public class Connection {
socket.close();
}
} catch (IOException ex) {
throw new JedisException(ex);
throw new JedisConnectionException(ex);
}
}
}

View File

@@ -1,7 +1,5 @@
package redis.clients.jedis;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -12,6 +10,7 @@ import java.util.Map;
import java.util.Set;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.exceptions.JedisDataException;
public class Jedis extends BinaryJedis implements JedisCommands {
public Jedis(final String host) {
@@ -1647,19 +1646,13 @@ public class Jedis extends BinaryJedis implements JedisCommands {
private void runChecks() {
if (client.isInMulti()) {
throw new JedisException(
throw new JedisDataException(
"Cannot use Jedis when in Multi. Please use JedisTransaction instead.");
}
try {
this.connect();
} catch (UnknownHostException e) {
throw new JedisException(e);
} catch (IOException e) {
throw new JedisException(e);
}
this.connect();
}
public void connect() throws UnknownHostException, IOException {
public void connect() {
if (!client.isConnected()) {
client.connect();
if (this.password != null) {
@@ -1668,7 +1661,7 @@ public class Jedis extends BinaryJedis implements JedisCommands {
}
}
public void disconnect() throws IOException {
public void disconnect() {
client.disconnect();
}

View File

@@ -1,22 +0,0 @@
package redis.clients.jedis;
import java.io.IOException;
public class JedisException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = -2946266495682282677L;
public JedisException(String message) {
super(message);
}
public JedisException(IOException e) {
super(e);
}
public JedisException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -10,6 +10,7 @@ import static redis.clients.jedis.Protocol.Keyword.UNSUBSCRIBE;
import java.util.Arrays;
import java.util.List;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.util.SafeEncoder;
public abstract class JedisPubSub {

View File

@@ -4,6 +4,8 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.util.RedisInputStream;
import redis.clients.util.RedisOutputStream;
import redis.clients.util.SafeEncoder;
@@ -44,13 +46,13 @@ public final class Protocol {
}
os.flush();
} catch (IOException e) {
throw new JedisException(e);
throw new JedisConnectionException(e);
}
}
private void processError(final RedisInputStream is) {
String message = is.readLine();
throw new JedisException(message);
throw new JedisDataException(message);
}
private Object process(final RedisInputStream is) {
@@ -67,10 +69,10 @@ public final class Protocol {
} else if (b == PLUS_BYTE) {
return processStatusCodeReply(is);
} else {
throw new JedisException("Unknown reply: " + (char) b);
throw new JedisConnectionException("Unknown reply: " + (char) b);
}
} catch (IOException e) {
throw new JedisException(e);
throw new JedisConnectionException(e);
}
return null;
}
@@ -94,7 +96,7 @@ public final class Protocol {
is.readByte();
is.readByte();
} catch (IOException e) {
throw new JedisException(e);
throw new JedisConnectionException(e);
}
return read;

View File

@@ -1,6 +1,5 @@
package redis.clients.jedis;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -28,8 +27,7 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands {
super(shards, algo, keyTagPattern);
}
@Override
public void disconnect() throws IOException {
public void disconnect() {
for (Jedis jedis : getAllShards()) {
jedis.quit();
jedis.disconnect();

View File

@@ -1,5 +1,7 @@
package redis.clients.jedis;
import redis.clients.jedis.exceptions.JedisException;
public abstract class TransactionBlock extends Transaction {
public TransactionBlock(Client client) {
super(client);

View File

@@ -0,0 +1,17 @@
package redis.clients.jedis.exceptions;
public class JedisConnectionException extends JedisException {
private static final long serialVersionUID = 3878126572474819403L;
public JedisConnectionException(String message) {
super(message);
}
public JedisConnectionException(Throwable cause) {
super(cause);
}
public JedisConnectionException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,17 @@
package redis.clients.jedis.exceptions;
public class JedisDataException extends JedisException {
private static final long serialVersionUID = 3878126572474819403L;
public JedisDataException(String message) {
super(message);
}
public JedisDataException(Throwable cause) {
super(cause);
}
public JedisDataException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,18 @@
package redis.clients.jedis.exceptions;
public class JedisException extends RuntimeException {
private static final long serialVersionUID = -2946266495682282677L;
public JedisException(String message) {
super(message);
}
public JedisException(Throwable e) {
super(e);
}
public JedisException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -3,7 +3,8 @@ package redis.clients.util;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import redis.clients.jedis.JedisException;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisException;
public abstract class Pool<T> {
private final GenericObjectPool internalPool;
@@ -18,8 +19,8 @@ public abstract class Pool<T> {
try {
return (T) internalPool.borrowObject();
} catch (Exception e) {
throw new JedisException("Could not get a resource from the pool",
e);
throw new JedisConnectionException(
"Could not get a resource from the pool", e);
}
}

View File

@@ -20,7 +20,8 @@ import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import redis.clients.jedis.JedisException;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisException;
public class RedisInputStream extends FilterInputStream {
@@ -87,13 +88,12 @@ public class RedisInputStream extends FilterInputStream {
}
String reply = sb.toString();
if (reply.length() == 0) {
throw new JedisException(
throw new JedisConnectionException(
"It seems like server has closed the connection.");
}
return reply;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
if (count == limit) {
fill();

View File

@@ -2,8 +2,8 @@ package redis.clients.util;
import java.io.UnsupportedEncodingException;
import redis.clients.jedis.JedisException;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.exceptions.JedisException;
/**
* The only reason to have this is to be able to compatible with java 1.5 :(