Question

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


    }
}

I have a problem in the above code. It throws a ClassCastException, where as if I destroy the pool and create a new pool object it works properly. Am I using the Pipeline API properly ?. Can anyone help me ? I am using 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)

Modified code to get the Map which throws Exception

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

Solution

Looks like the pipeline doesn't close after exec() call. So when you try to reuse the same Jedis object after returnResource it still contains pipelinedResponses from previous operation.

Try to do this way:

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

sync() call should close the pipeline.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top