6

I have the following code snippet which for getting the RedisTemplate.

@Bean
public JedisConnectionFactory getJedisConnectionFactory() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
    redisStandaloneConfiguration.setHostName(host);
    if (!StringUtils.isEmpty(password)) {
        redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
    }
    redisStandaloneConfiguration.setPort(port);
    return new JedisConnectionFactory(redisStandaloneConfiguration, getJedisClientConfiguration());
}

@Bean
public RedisTemplate redisTemplate() {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
    redisTemplate.setConnectionFactory(getJedisConnectionFactory());
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    return redisTemplate;

}

My question how sprint-boot will understand the Connection pooling because I have not provided any information in my factory about the connection pool. My application properties file has the following properties.

redis.host=<redis-host>
redis.port=<port>
redis.password=<password>
redi.jedis.pool.max.total=16
redi.jedis.pool.max.idle=8
redi.jedis.pool.min.idle=4
Parveen
  • 149
  • 1
  • 12
  • Can you share details for this `getJedisClientConfiguration()` as well. – lucid May 09 '20 at 19:37
  • 2
    @Bean public JedisClientConfiguration getJedisClientConfiguration() { JedisClientConfiguration.JedisClientConfigurationBuilder builder = (JedisClientConfiguration.JedisClientConfigurationBuilder) JedisClientConfiguration .builder(); GenericObjectPoolConfig GenericObjectPoolConfig = new GenericObjectPoolConfig(); GenericObjectPoolConfig.setMaxTotal(maxConnection); GenericObjectPoolConfig.setMaxIdle(maxConnectionIdle); GenericObjectPoolConfig.setMinIdle(minConnectionIdle); return builder.usePooling().poolConfig(GenericObjectPoolConfig).build(); } – Parveen May 09 '20 at 19:39
  • 1
    What are the default values set if no pooling values are specified? – Fernando Nov 21 '20 at 02:54

1 Answers1

5

When you create JedisClientConfiguration using the builder

JedisClientConfigurationBuilder builder = JedisClientConfiguration .builder()

this will internally call default constructor on JedisClientConfiguration which looks like something like this.

private DefaultJedisClientConfigurationBuilder() {
  this.poolConfig = new JedisPoolConfig();
  // other configs
}

JedisPoolConfig further extends GenericObjectPoolConfig which has default values as below. (which would be default values if not overridden manually)

maxTotal = 8;
maxIdle = 8;
minIdle = 0;

In your case, as you have overridden config with GenericObjectPoolConfig, it will pick values from there.

GenericObjectPoolConfig.setMaxTotal(maxConnection); 
GenericObjectPoolConfig.setMaxIdle(maxConnectionIdle); 
GenericObjectPoolConfig.setMinIdle(minConnectionIdle);

As you are specifying usePooling() and poolConfig(genericObjectPoolConfig), your app will use these config for connection pooling.

I hope this helps.

lucid
  • 2,722
  • 1
  • 12
  • 24
  • Thank you Lucid for the detailed explanation. Can you point me to some document where I can read it completely and understand it better in future – Parveen May 10 '20 at 10:11
  • 1
    @Parveen you can check the official documentation here https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#redis:connectors. Along with you can also go through source code. You can also go through jedis https://github.com/xetorthio/jedis – lucid May 10 '20 at 12:14