全部
我已经安装了最新的 Redis 2.4.16 并尝试将其 Pub/Sub 系统与 java 一起使用。我每秒都在向频道发送一条消息。发布者没有问题,但订阅者因消息而崩溃
例外:
redis.clients.jedis.exceptions.JedisDataException: ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context
at redis.clients.jedis.Protocol.processError(Protocol.java:59)
at redis.clients.jedis.Protocol.process(Protocol.java:66)
at redis.clients.jedis.Protocol.read(Protocol.java:131)
at redis.clients.jedis.Connection.getObjectMultiBulkReply(Connection.java:206)
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:1971)
at com.jedis.test.JedisSub$1.run(JedisSub.java:22)
at java.lang.Thread.run(Thread.java:680)
这是我的代码:
出版商:
final Jedis jedis = new Jedis("localhost");
ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(10);
newFixedThreadPool.submit(new Runnable() {
@Override
public void run() {
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
jedis.publish("CC", new Date().toString());
}
}
});
订户:
JedisPool jedisPool = new JedisPool(poolConfig,"localhost", 6379, 100);
final Jedis subscriberJedis = jedisPool.getResource();
new Thread(new Runnable() {
@Override
public void run() {
try {
subscriberJedis.subscribe(new JedisPubSub() …..,"CC");
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
jedisPool.returnResource(subscriberJedis);
池配置:
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.maxActive = 10;
poolConfig.maxIdle = 5;
poolConfig.minIdle = 1;
poolConfig.testOnBorrow = true;
poolConfig.numTestsPerEvictionRun = 10;
poolConfig.timeBetweenEvictionRunsMillis = 60000;
poolConfig.maxWait = 3000;
poolConfig.whenExhaustedAction = org.apache.commons.pool.impl.GenericObjectPool.WHEN_EXHAUSTED_FAIL;
为了安装 Redis,我只是使用了命令
make PREFIX=/Users/ggg/dev/dist/redis/ install
在此之后我没有使用./install_server.sh
Jedis版本为2.1.0,平台为Mac OS X。
注意:我注意到订阅者在启动后大约 30 秒崩溃。