为了避免在我的redis频道重复,我正在检查是否已经存在的消息通过在Redis中设置索引。以下是我的实施。但是,这是一个例外。Redis异常与Jedis客户端的交易
redis.clients.jedis.exceptions.JedisDataException: Please close pipeline or multi block before calling this method.
at redis.clients.jedis.Response.get(Response.java:23)
这是实施。
Jedis jedis = pool.getResource();
String id = message.getId();
Transaction transaction = jedis.multi();
redis.clients.jedis.Response<java.lang.Boolean> response = transaction.sismember(ID_SET_REDIS_KEY, id);
if (response != null && !response.get().booleanValue()) {
//add it to the
transaction.sadd(ID_SET_REDIS_KEY, id);
transaction.publish(redisChannelName, message);
}
transaction.exec();
pool.returnResource(jedis);
因为有多个发布者可能发布完全相同的消息,所以我需要在事务内部进行获取。
实际上,您可以通过“WATCH”获得交易结果。 http://redis.io/topics/transactions。我不认为有必要在这里写lua脚本。 @Soumya Simanta –