REDIS (15) JedisPool的参数testoncreate,testWhileIdle,testonborrow和testonreturn

6 篇文章 0 订阅

流程

【testonreturn参数】    JedisPool.returnResource(jedis)->pool.returnObject(jedis)->pool.getTestOnReturn()
                                                   |
                                                             |          
                                                           |                
                                                                     |  
 【testOnborrow参数】 JedisPool.getResource()->pool.borrowObject()    |   
                                                      |              |
                                                         \           |
                                                           \         |
                                                             \       |
                                                              _\|   \|/                
                                              PooledObjectFactory.validateObject(PooledObject<T>)
                                                                  |
                                                                  |--->JedisFactory.validateObject(PooledObject<Jedis>)-->ping通,不通销毁
                                                                  |
                                                                  |-->ShardedJedisFactory.validateObject(ooledObject<Jedis>)->集群所有节点ping通,不通销毁

org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(long borrowMaxWaitMillis) throws Exception

public T borrowObject(long borrowMaxWaitMillis) throws Exception {
        assertOpen();

        AbandonedConfig ac = this.abandonedConfig;
        if (ac != null && ac.getRemoveAbandonedOnBorrow() &&
                (getNumIdle() < 2) &&
                (getNumActive() > getMaxTotal() - 3) ) {
            removeAbandoned(ac);
        }

        PooledObject<T> p = null;

        // Get local copy of current config so it is consistent for entire
        // method execution
        boolean blockWhenExhausted = getBlockWhenExhausted();

        boolean create;
        long waitTime = System.currentTimeMillis();

        while (p == null) {
            create = false;
            if (blockWhenExhausted) {
                p = idleObjects.pollFirst();
                if (p == null) {
                    p = create();
                    if (p != null) {
                        create = true;
                    }
                }
                if (p == null) {
                    if (borrowMaxWaitMillis < 0) {
                        p = idleObjects.takeFirst();
                    } else {
                        p = idleObjects.pollFirst(borrowMaxWaitMillis,
                                TimeUnit.MILLISECONDS);
                    }
                }
                if (p == null) {
                    throw new NoSuchElementException(
                            "Timeout waiting for idle object");
                }
                if (!p.allocate()) {
                    p = null;
                }
            } else {
                p = idleObjects.pollFirst();
                if (p == null) {
                    p = create();
                    if (p != null) {
                        create = true;
                    }
                }
                if (p == null) {
                    throw new NoSuchElementException("Pool exhausted");
                }
                if (!p.allocate()) {
                    p = null;
                }
            }

            if (p != null) {
                try {
                    factory.activateObject(p);
                } catch (Exception e) {
                    try {
                        destroy(p);
                    } catch (Exception e1) {
                        // Ignore - activation failure is more important
                    }
                    p = null;
                    if (create) {
                        NoSuchElementException nsee = new NoSuchElementException(
                                "Unable to activate object");
                        nsee.initCause(e);
                        throw nsee;
                    }
                }
                if (p != null && (getTestOnBorrow() || create && getTestOnCreate())) {
                    boolean validate = false;
                    Throwable validationThrowable = null;
                    try {
                        validate = factory.validateObject(p);
                    } catch (Throwable t) {
                        PoolUtils.checkRethrow(t);
                        validationThrowable = t;
                    }
                    if (!validate) {
                        try {
                            destroy(p);
                            destroyedByBorrowValidationCount.incrementAndGet();
                        } catch (Exception e) {
                            // Ignore - validation failure is more important
                        }
                        p = null;
                        if (create) {
                            NoSuchElementException nsee = new NoSuchElementException(
                                    "Unable to validate object");
                            nsee.initCause(validationThrowable);
                            throw nsee;
                        }
                    }
                }
            }
        }

        updateStatsBorrow(p, System.currentTimeMillis() - waitTime);

        return p.getObject();
    }

···

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用JedisPool来管理Redis连接池,可以按照以下步骤操作: 1. 首先,确保你已经将Jedis库添加到你的项目中。你可以在Maven或Gradle的配置文件中添加以下依赖: ```xml <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>版本号</version> </dependency> ``` 2. 在代码中导入JedisJedisPool相关的类: ```java import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; ``` 3. 创建JedisPoolConfig对象,并设置连接池的相关属性(如最大连接数、最大空闲连接数等): ```java JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(100); // 设置最大连接数 jedisPoolConfig.setMaxIdle(10); // 设置最大空闲连接数 // 可以根据需要设置其他属性,比如最小空闲连接数、连接超时时间等 ``` 4. 创建JedisPool对象,传入Redis服务器的主机名、端口号、连接超时时间和密码(如果有的话): ```java JedisPool jedisPool = new JedisPool(jedisPoolConfig, "localhost", 6379, 10000, "password"); ``` 注意:如果Redis服务器没有设置密码,可以省略密码参数。 5. 从连接池中获取Jedis对象,并使用Jedis对象进行Redis操作: ```java try (Jedis jedis = jedisPool.getResource()) { // 执行Redis操作,如存储键值对 jedis.set("key", "value"); String value = jedis.get("key"); System.out.println(value); } catch (Exception e) { // 异常处理 } ``` 6. 当不再需要使用Redis连接时,记得将Jedis对象还回连接池: ```java jedis.close(); ``` 这就是使用JedisPool来管理Redis连接池的基本步骤。通过合理配置连接池的属性,可以提高Redis操作的性能和效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值