Spring data Redis. How to know number of Active, Idle Connection?

3.3k Views Asked by At

In Spring, I have a jedisConnFactory and a jedisPoolConfig bean like this

    <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="127.0.0.1" p:port="6379" p:poolConfig-ref="jedisPoolConfig" />

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.pool.maxTotal}" />
        <property name="maxIdle" value="${redis.pool.maxIdle}" />
        <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
    </bean>

Sending data to server is OK. But I do not know how to verify if the JedisPoll is working.

How can I check the number of Active, Idle connection?

2

There are 2 best solutions below

0
On

Make sure, that JMX is enabled in your pool configuration. Note that GenericObjectPoolConfig can also be passed, which gives you full control on your object pool. Then, you can connect to your application through JMX with jconsole and you can track the actual state of your connection pool.

0
On

we can get the metrics by Java reflection:

      Field poolField = JedisConnectionFactory.class.getDeclaredField("pool");
  poolField.setAccessible(true);
  Pool<Jedis> jedisPool = (Pool<Jedis>)poolField.get(connectionFactory);
  int activeNum = jedisPool.getNumActive();
  int idleNum = jedisPool.getNumIdle();
  int waitNum = jedisPool.getNumWaiters();
  long maxBorrowWaitMs = jedisPool.getMaxBorrowWaitTimeMillis();
  long meanBorrowWaitMs = jedisPool.getMeanBorrowWaitTimeMillis();

output:

pool monitor - activeNum=0,idleNum=1,waitNum=0, maxBorrowWaitMs=7, meanBorrowWaitMs=0