Skip to content
Advertisement

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?

Advertisement

Answer

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();
    }
  }
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement