Jedis Pipeline Multi抛出ClassCastException

时间:2013-04-20 11:39:39

标签: exception pipeline jedis

public class JedisPipeline {
    private static JedisPool pool = new JedisPool(new JedisPoolConfig(), "127.0.0.1", 6379);
    public static void main(String args[]){

        Jedis jedis = pool.getResource();
        Pipeline pipeline = jedis.pipelined();
        pipeline.multi();
        //pipeline.hmset("Id", new HashMap<String,String>() );
        for(int i=0; i < 1000 ; i++){
            pipeline.hincrBy("Id", i+"", i);
        }
        pipeline.exec();
        pool.returnResource(jedis);
        //pool.destroy();

        //pool = new JedisPool(new JedisPoolConfig(), "127.0.0.1", 6379);
        jedis = pool.getResource();
        Map<String,String> map1 = jedis.hgetAll("Id");
        System.out.println("map1------->"+map1);
        pool.returnResource(jedis);
        //pool.destroy();


    }
}

我在上面的代码中遇到了问题。它会抛出一个ClassCastException,就好像我会破坏池并创建一个新的池对象一样,它可以正常工作。我是否正确使用Pipeline API?谁能帮我 ?我正在使用Jedis 2.1.0

Exception in thread "main" java.lang.ClassCastException: [B cannot be cast to java.util.List
    at redis.clients.jedis.Connection.getBinaryMultiBulkReply(Connection.java:189)
    at redis.clients.jedis.Jedis.hgetAll(Jedis.java:861)
    at com.work.jedisex.JedisFactory.main(JedisFactory.java:59)

修改代码以获取抛出异常的地图

Response<Map<String,String>> map1 = pipeline.hgetAll("Id");
pipeline.exec();
pipeline.sync();
pool.returnResource(jedis);
Map<String,String> map2 = map1.get();

1 个答案:

答案 0 :(得分:1)

看起来管道在exec()调用后没有关闭。因此,当您尝试在returnResource之后重用相同的Jedis对象时,它仍然包含来自上一操作的pipelinedResponses。

尝试这样做:

    pipeline.exec();
    pipeline.sync();
    pool.returnResource(jedis);

sync()调用应关闭管道。