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

来源:互联网 发布:淘宝店铺推广流程 编辑:程序博客网 时间:2024/06/07 07:02

流程

【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
原创粉丝点击