Search code examples
javaredisspring-data-redisjedis

Access Redis connection pool using Spring Data Redis


I want to monitor and periodically log information about the Redis Connection Pool usage.

I use Redis through spring-data-redis RedisTemplate object.

Is there any way to access pool?


Solution

  • I was able to access internal pool using reflection API.

      private GenericObjectPool<Jedis> jedisPool() {
        try {
          Field pool = JedisConnectionFactory.class.getDeclaredField("pool");
          pool.setAccessible(true);
          Pool<Jedis> jedisPool = (Pool<Jedis>) pool.get(jedisConnectionFactory());
          Field internalPool = Pool.class.getDeclaredField("internalPool");
          internalPool.setAccessible(true);
          return (GenericObjectPool<Jedis>) internalPool.get(jedisPool);
        } catch (NoSuchFieldException | IllegalAccessException e) {
          e.printStackTrace();
        }
      }