使用springdataredis访问redis连接池

rjjhvcjd  于 2021-06-08  发布在  Redis
关注(0)|答案(1)|浏览(435)

我希望监视并定期记录有关redis连接池使用情况的信息。
我通过spring数据redis redistemplate对象使用redis。
有办法进入游泳池吗?

vnjpjtjt

vnjpjtjt1#

我可以使用反射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();
    }
  }

相关问题