简体   繁体   English

如何在多线程环境中使用JedisPool创建多个Jedis实例

[英]How to create several Jedis instances using JedisPool in a multithreaded environment

I tried to use JedisPool to create several Jedis instances for multithreading usage (each thread can have one Jedis instance). 我试图使用JedisPool创建多个Jedis实例以用于多线程(每个线程可以有一个Jedis实例)。 But when I tried to create multiple instances by using JedisPool.getResource(), it always gives me the same Jedis instance. 但是,当我尝试使用JedisPool.getResource()创建多个实例时,它总是为我提供相同的Jedis实例。 And the following code will also give me redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream because of single Jedis instance for multiple threads. 以下代码也将给我redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream由于多个线程的单个Jedis实例redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream

private final static JedisPoolConfig poolConfig = buildPoolConfig();
private static JedisPool jedisPool = new JedisPool(poolConfig, "localhost");

public static void main(String[] args) throws Exception {
    MyThread[] myThreads = new MyThread[4];
    for (int i = 0; i < myThreads.length; i++) {
       try (Jedis jedis = jedisPool.getResource()) {
           System.out.println("jedis " + i  + ": "+ jedis);
           myThreads[i] = new MyThread(jedis);
           myThreads[i].start();
       }
    }
    jedisPool.close();
}

private static JedisPoolConfig buildPoolConfig() {
    final JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(128);
    poolConfig.setMaxIdle(128);
    poolConfig.setMinIdle(16);
    poolConfig.setTestOnBorrow(true);
    poolConfig.setTestOnReturn(true);
    poolConfig.setTestWhileIdle(true);
    poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis());
    poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis());
    poolConfig.setNumTestsPerEvictionRun(3);
    poolConfig.setBlockWhenExhausted(true);
    return poolConfig;
}

Any help would be really appreciated. 任何帮助将非常感激。 Thanks! 谢谢!

You are supposed to use JedisPool in a multi-threaded environment. 您应该在多线程环境中使用JedisPool But, by your implementation, you are actually using Jedis in that situation. 但是,通过实施,您实际上是在这种情况下使用Jedis

To resolve that you can take JedisPool instead of Jedis for MyThread constructor. 要解决此问题,可以将JedisPool代替Jedis作为MyThread构造函数。

public static void main(String[] args) throws Exception {
    MyThread[] myThreads = new MyThread[4];
    for (int i = 0; i < myThreads.length; i++) {
        myThreads[i] = new MyThread(jedisPool);
        myThreads[i].start();
    }
    jedisPool.close();
}

During every operation in MyThread class, take a Jedis object from pool and use it. MyThread类中的每个操作期间,从池中获取Jedis对象并使用它。 For example: 例如:

class MyThread {
    void doSomething() {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.exists(key);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM