英文:
Access Redis connection pool using Spring Data Redis
问题
我想监控并定期记录关于Redis连接池使用情况的信息。
我通过spring-data-redis的RedisTemplate对象使用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?
答案1
得分: 0
我能够使用反射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();
  }
}
英文:
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();
    }
  }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论