gpt4 book ai didi

java - redis 好像服务器关闭了连接

转载 作者:IT王子 更新时间:2023-10-29 06:02:46 27 4
gpt4 key购买 nike

我想使用 redis sub/pub,但是当我订阅一个 channel 时,2 分钟后,控制台输出异常: 似乎服务器已关闭连接。

redis版本:redis-3.0.3

绝地武士版本:2.3.0

操作系统:OS X Yosemite 10.10.5

订阅类

    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");

发布类

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)

谁能帮帮我?非常感谢!

最佳答案

我不是 OSX 用户,但可能是操作系统配置为终止超过 120 秒的 TCP 连接。您是否尝试过将 tcp-keepalive 选项设置为某个值(例如 30 秒)以检查连接是否仍然断开?

关于java - redis 好像服务器关闭了连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34131736/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com