Addded default socket timeout of 2000 ms
This commit is contained in:
@@ -5,6 +5,7 @@ import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -18,6 +19,31 @@ public class Connection {
|
||||
private DataOutputStream outputStream;
|
||||
private DataInputStream inputStream;
|
||||
private int pipelinedCommands = 0;
|
||||
private int timeout = 2000;
|
||||
|
||||
public int getTimeout() {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
public void setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public void setTimeoutInfinite() {
|
||||
try {
|
||||
socket.setSoTimeout(0);
|
||||
} catch (SocketException ex) {
|
||||
throw new JedisException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void rollbackTimeout() {
|
||||
try {
|
||||
socket.setSoTimeout(timeout);
|
||||
} catch (SocketException ex) {
|
||||
throw new JedisException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public Connection(String host) {
|
||||
super();
|
||||
@@ -61,6 +87,7 @@ public class Connection {
|
||||
public void connect() throws UnknownHostException, IOException {
|
||||
if (!connected) {
|
||||
socket = new Socket(host, port);
|
||||
socket.setSoTimeout(timeout);
|
||||
connected = socket.isConnected();
|
||||
outputStream = new DataOutputStream(socket.getOutputStream());
|
||||
inputStream = new DataInputStream(new BufferedInputStream(socket
|
||||
|
||||
@@ -21,6 +21,11 @@ public class Jedis {
|
||||
client = new Client(host, port);
|
||||
}
|
||||
|
||||
public Jedis(String host, int port, int timeout) {
|
||||
client = new Client(host, port);
|
||||
client.setTimeout(timeout);
|
||||
}
|
||||
|
||||
public String ping() {
|
||||
client.ping();
|
||||
return client.getStatusCodeReply();
|
||||
@@ -485,7 +490,10 @@ public class Jedis {
|
||||
args.add(String.valueOf(timeout));
|
||||
|
||||
client.blpop(args.toArray(new String[args.size()]));
|
||||
return client.getMultiBulkReply();
|
||||
client.setTimeoutInfinite();
|
||||
List<String> multiBulkReply = client.getMultiBulkReply();
|
||||
client.rollbackTimeout();
|
||||
return multiBulkReply;
|
||||
}
|
||||
|
||||
public int sort(String key, SortingParams sortingParameters, String dstkey) {
|
||||
@@ -506,7 +514,11 @@ public class Jedis {
|
||||
args.add(String.valueOf(timeout));
|
||||
|
||||
client.brpop(args.toArray(new String[args.size()]));
|
||||
return client.getMultiBulkReply();
|
||||
client.setTimeoutInfinite();
|
||||
List<String> multiBulkReply = client.getMultiBulkReply();
|
||||
client.rollbackTimeout();
|
||||
|
||||
return multiBulkReply;
|
||||
}
|
||||
|
||||
public String auth(String password) {
|
||||
@@ -521,7 +533,9 @@ public class Jedis {
|
||||
}
|
||||
|
||||
public void subscribe(JedisPubSub jedisPubSub, String... channels) {
|
||||
client.setTimeoutInfinite();
|
||||
jedisPubSub.proceed(client, channels);
|
||||
client.rollbackTimeout();
|
||||
}
|
||||
|
||||
public void publish(String channel, String message) {
|
||||
@@ -529,7 +543,9 @@ public class Jedis {
|
||||
}
|
||||
|
||||
public void psubscribe(JedisPubSub jedisPubSub, String... patterns) {
|
||||
client.setTimeoutInfinite();
|
||||
jedisPubSub.proceedWithPatterns(client, patterns);
|
||||
client.rollbackTimeout();
|
||||
}
|
||||
|
||||
public int zcount(String key, double min, double max) {
|
||||
|
||||
@@ -11,4 +11,4 @@ public class JedisTest {
|
||||
Jedis jedis = new Jedis("localhost");
|
||||
jedis.dbSize();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.Protocol;
|
||||
|
||||
public abstract class JedisCommandTestBase extends Assert {
|
||||
|
||||
@@ -20,7 +21,7 @@ public abstract class JedisCommandTestBase extends Assert {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
jedis = new Jedis("localhost");
|
||||
jedis = new Jedis("localhost", Protocol.DEFAULT_PORT, 500);
|
||||
jedis.connect();
|
||||
jedis.auth("foobared");
|
||||
jedis.flushDB();
|
||||
@@ -32,10 +33,10 @@ public abstract class JedisCommandTestBase extends Assert {
|
||||
}
|
||||
|
||||
protected Jedis createJedis() throws UnknownHostException, IOException {
|
||||
Jedis j = new Jedis("localhost");
|
||||
j.connect();
|
||||
j.auth("foobared");
|
||||
j.flushAll();
|
||||
return j;
|
||||
Jedis j = new Jedis("localhost");
|
||||
j.connect();
|
||||
j.auth("foobared");
|
||||
j.flushAll();
|
||||
return j;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user