我想使用redis sub / pub,但是当我订阅一个频道,2分钟后,控制台输出异常: 似乎服务器已关闭连接。
redis版本:redis-3.0.3
jedis版本:2.3.0
os:OS X Yosemite 10.10.5
Subscribe.class
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(10);
jedisPoolConfig.setMaxWait(4000);
jedisPoolConfig.setTestOnBorrow(true);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, "127.0.0.1", 6379);
final Jedis jedis = jedisPool.getResource();
System.out.println(jedis.configGet("timeout"));
System.out.println(jedis.configGet("tcp-keepalive"));
final JedisPubSub jedisPubSub = new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
System.out.println("onMessage");
}
@Override
public void onPMessage(String pattern, String channel, String message) {
System.out.println("onPMessage");
}
@Override
public void onSubscribe(String channel, int subscribedChannels) {
System.out.println("onSubscribe");
}
@Override
public void onUnsubscribe(String channel, int subscribedChannels) {
System.out.println("onUnsubscribe");
}
@Override
public void onPUnsubscribe(String pattern, int subscribedChannels) {
System.out.println("onPUnsubscribe");
}
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {
System.out.println("onPSubscribe");
}
};
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(dateFormat.format(new Date()));
jedis.subscribe(jedisPubSub, "/atm/test");
Publish.class
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.publish("/atm/test", "lqiaing---hello");
然后在控制台输出后2分钟:
[timeout, 0]
[tcp-keepalive, 0]
2015-12-03 19:01:55
onSubscribe
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException:
It seems like server has closed the connection.
at redis.clients.util.RedisInputStream.readLine(RedisInputStream.java:91)
at redis.clients.jedis.Protocol.processMultiBulkReply(Protocol.java:110)
at redis.clients.jedis.Protocol.process(Protocol.java:63)
at redis.clients.jedis.Protocol.read(Protocol.java:122)
at redis.clients.jedis.Connection.getObjectMultiBulkReply(Connection.java:196)
at redis.clients.jedis.JedisPubSub.process(JedisPubSub.java:88)
at redis.clients.jedis.JedisPubSub.proceed(JedisPubSub.java:83)
at redis.clients.jedis.Jedis.subscribe(Jedis.java:1974)
at Subscribe.main(Subscribe.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
谁能帮帮我?非常感谢!
答案 0 :(得分:0)
我不是OSX用户,但它可能是配置为终止超过120秒的TCP连接的操作系统。您是否尝试将tcp-keepalive选项设置为某个值(如30秒)以检查连接是否仍然死亡?