PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 5, 2022

[FIXED] How to config JedisPoolConfig with redis.clients.jedis.UnifiedJedis

 September 05, 2022     apache-commons, java, jedis, redis     No comments   

Issue

I have seen follwing example.

  final JedisPoolConfig poolConfig = buildPoolConfig();
JedisPool jedisPool = new JedisPool(poolConfig, "localhost");

private JedisPoolConfig buildPoolConfig() {
    final JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(128);
    poolConfig.setMaxIdle(128);
    poolConfig.setMinIdle(16);
    poolConfig.setTestOnBorrow(true);
    poolConfig.setTestOnReturn(true);
    poolConfig.setTestWhileIdle(true);
    poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis());
    poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis());
    poolConfig.setNumTestsPerEvictionRun(3);
    poolConfig.setBlockWhenExhausted(true);
    return poolConfig;
}

try (Jedis jedis = jedisPool.getResource()) {
    // do operations with jedis resource
}

It returns only redis.clients.jedis.Jedis client, but I am using "jedis-4.0.0.jar" and I want use redis.clients.jedis.UnifiedJedis client with pooling mechanism is there any way to use this.


Solution

Instead of JedisPoolConfig you need ConnectionPoolConfig.

Also, since Jedis 4.0 you can't just send host in the respective constructor. So use any other alternate.

WARNING: This constructor only accepts a uri string as url.

final ConnectionPoolConfig poolConfig = buildPoolConfig();
UnifiedJedis jedis = new JedisPooled(poolConfig, "localhost", 6379);

private ConnectionPoolConfig buildPoolConfig() {
    final ConnectionPoolConfig poolConfig = new ConnectionPoolConfig();
    // change poolConfig as you want
    return poolConfig;
}

// do operations with (unified) jedis object


Answered By - sazzad
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing