All
I have installed latest Redis 2.4.16 and trying to use its Pub/Sub system with java. I am putting a message to a channel every second. There is no issue with Publisher but Subscriber crashes with the message
Exception:
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)
Here is my Code:
Publisher:
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());
}
}
});
Subscriber:
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);
Pool Configuration:
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;
For installing Redis, I simply used command
make PREFIX=/Users/ggg/dev/dist/redis/ install
After this I did not use ./install_server.sh
Jedis version is 2.1.0, platform is Mac OS X.
Note: What I have noticed is subscriber crashes near about 30 seconds after start.