国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

JedisPool使用原理及源代碼

newtrek / 2710人閱讀

摘要:,的使用連接池的配置信息說(shuō)明一個(gè)可以有多少個(gè)實(shí)例最大最小獲得一個(gè)實(shí)例的時(shí)候是否檢查連接可用性一個(gè)實(shí)例給時(shí),是否檢查連接可用性狀態(tài)監(jiān)測(cè)用異步線程進(jìn)行檢查,一次最多的里的實(shí)例個(gè)數(shù)

1,JedisPool的使用






















//WHEN_EXHAUSTED_FAIL = 0; 直接拋出異常throw new NoSuchElementException("Pool exhausted");
//WHEN_EXHAUSTED_BLOCK = 1;borrowObject()將會(huì)阻塞,直到有可用新的或者空閑的object為止,或者如果配置了maxWait,
//如果請(qǐng)求阻塞超時(shí),將拋出NoSuchElementException.如果maxWait為負(fù)數(shù),請(qǐng)求將會(huì)無(wú)限制的阻
//塞下去,默認(rèn)配置。
//WHEN_EXHAUSTED_GROW = 2;borrowObject()將會(huì)繼續(xù)創(chuàng)建新的對(duì)象,并返回,因此,pool維護(hù)的對(duì)像數(shù)將超出maxActive;
//

public String set(String key, String value) {
Jedis jedis = null;
boolean success = true;
try {
jedis = this.pool.getResource();
return jedis.set(key, value);
}catch (JedisException e) {
success = false;
if(jedis != null){
pool.returnBrokenResource(jedis);
}
throw e;
}finally{
if(success && jedis != null){
this.pool.returnResource(jedis);
}
}
}
獲取Jedis

pool.getResource();
這個(gè)可以直接看Pool的getResource方法,

最終還是GenericObjectPool的borrowObject()方法借用對(duì)象

@SuppressWarnings("unchecked")
public T getResource() {
try {
return (T) internalPool.borrowObject();
} catch (Exception e) {
throw new JedisConnectionException(
"Could not get a resource from the pool", e);
}
}
用完歸還,調(diào)用的是GenericObjectPool的returnObject()方法

pool.returnResource(jedis)
//JedisPool.java
public void returnResource(final BinaryJedis resource) {
returnResourceObject(resource);
}
//Pool.java
public void returnResourceObject(final Object resource) {
try {
internalPool.returnObject(resource);
} catch (Exception e) {
throw new JedisException(
"Could not return the resource to the pool", e);
}
}
出錯(cuò),調(diào)用的是GenericObjectPool的invalidateObject()方法

最后在JedisFactory的destroyObject()中調(diào)用jedis.quit()請(qǐng)求Server關(guān)閉連接

pool.returnBrokenResource(jedis)
//JedisPool.java
public void returnBrokenResource(final BinaryJedis resource) {
returnBrokenResourceObject(resource);
}
//Pool.java
protected void returnBrokenResourceObject(final Object resource) {
try {
//失效
internalPool.invalidateObject(resource);
} catch (Exception e) {
throw new JedisException(
"Could not return the resource to the pool", e);
}
}
//GenericObjectPool
public void invalidateObject(Object obj)
throws Exception
{
try
{
if (this._factory != null)
this._factory.destroyObject(obj);
}
finally {
synchronized (this) {
this._numActive -= 1;
allocate();
}
}
}
//JedisFactory
public void destroyObject(final Object obj) throws Exception {
if (obj instanceof Jedis) {
final Jedis jedis = (Jedis) obj;
if (jedis.isConnected()) {
try {
try {
jedis.quit();
} catch (Exception e) {
}
jedis.disconnect();
} catch (Exception e) {

        }
    }
}

}
JedisPool源代碼

package redis.clients.jedis;

import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool.Config;

import redis.clients.util.Pool;

public class JedisPool extends Pool {

public JedisPool(final Config poolConfig, final String host) {
    this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
}

public JedisPool(String host, int port) {
    this(new Config(), host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
}

public JedisPool(final String host) {
    this(host, Protocol.DEFAULT_PORT);
}

public JedisPool(final Config poolConfig, final String host, int port,
        int timeout, final String password) {
    this(poolConfig, host, port, timeout, password, Protocol.DEFAULT_DATABASE);
}

public JedisPool(final Config poolConfig, final String host, final int port) {
    this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
}

public JedisPool(final Config poolConfig, final String host, final int port, final int timeout) {
    this(poolConfig, host, port, timeout, null, Protocol.DEFAULT_DATABASE);
}

public JedisPool(final Config poolConfig, final String host, int port, int timeout, final String password,
                 final int database) {
    super(poolConfig, new JedisFactory(host, port, timeout, password, database));
}


public void returnBrokenResource(final BinaryJedis resource) {
    returnBrokenResourceObject(resource);
}

public void returnResource(final BinaryJedis resource) {
    returnResourceObject(resource);
}

/**
 * PoolableObjectFactory custom impl.
 */
private static class JedisFactory extends BasePoolableObjectFactory {
    private final String host;
    private final int port;
    private final int timeout;
    private final String password;
    private final int database;

    public JedisFactory(final String host, final int port,
            final int timeout, final String password, final int database) {
        super();
        this.host = host;
        this.port = port;
        this.timeout = timeout;
        this.password = password;
        this.database = database;
    }

    public Object makeObject() throws Exception {
        final Jedis jedis = new Jedis(this.host, this.port, this.timeout);

        jedis.connect();
        if (null != this.password) {
            jedis.auth(this.password);
        }
        if( database != 0 ) {
            jedis.select(database);
        }

        return jedis;
    }

    public void destroyObject(final Object obj) throws Exception {
        if (obj instanceof Jedis) {
            final Jedis jedis = (Jedis) obj;
            if (jedis.isConnected()) {
                try {
                    try {
                        jedis.quit();
                    } catch (Exception e) {
                    }
                    jedis.disconnect();
                } catch (Exception e) {

                }
            }
        }
    }

    public boolean validateObject(final Object obj) {
        if (obj instanceof Jedis) {
            final Jedis jedis = (Jedis) obj;
            try {
                return jedis.isConnected();/* && jedis.ping().equals("PONG");*/
            } catch (final Exception e) {
                return false;
            }
        } else {
            return false;
        }
    }
}

}
其中JedisFactory繼承自BasePoolableObjectFactory,只實(shí)現(xiàn)了3個(gè)方法

makeObject(),連接,new Socket()

destroyObject()--斷開連接,

validateObject()--ping

Pool源代碼

package redis.clients.util;

import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;

import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisException;

public abstract class Pool {
private final GenericObjectPool internalPool;

public Pool(final GenericObjectPool.Config poolConfig,
        PoolableObjectFactory factory) {
    this.internalPool = new GenericObjectPool(factory, poolConfig);
}

@SuppressWarnings("unchecked")
public T getResource() {
    try {
        return (T) internalPool.borrowObject();
    } catch (Exception e) {
        throw new JedisConnectionException(
                "Could not get a resource from the pool", e);
    }
}

public void returnResourceObject(final Object resource) {
    try {
        internalPool.returnObject(resource);
    } catch (Exception e) {
        throw new JedisException(
                "Could not return the resource to the pool", e);
    }
}

public void returnBrokenResource(final T resource) {
    returnBrokenResourceObject(resource);
}

public void returnResource(final T resource) {
    returnResourceObject(resource);
}

protected void returnBrokenResourceObject(final Object resource) {
    try {
        //失效
        internalPool.invalidateObject(resource);
    } catch (Exception e) {
        throw new JedisException(
                "Could not return the resource to the pool", e);
    }
}

public void destroy() {
    try {
        internalPool.close();
    } catch (Exception e) {
        throw new JedisException("Could not destroy the pool", e);
    }
}

}
JedisPoolConfig源代碼

public class JedisPoolConfig extends Config {
public JedisPoolConfig() {
// defaults to make your life with connection pool easier :)
setTestWhileIdle(true);
setMinEvictableIdleTimeMillis(60000);
setTimeBetweenEvictionRunsMillis(30000);
setNumTestsPerEvictionRun(-1);
}

public int getMaxIdle() {
    return maxIdle;
}

public void setMaxIdle(int maxIdle) {
    this.maxIdle = maxIdle;
}

public int getMinIdle() {
    return minIdle;
}

public void setMinIdle(int minIdle) {
    this.minIdle = minIdle;
}

public int getMaxActive() {
    return maxActive;
}

public void setMaxActive(int maxActive) {
    this.maxActive = maxActive;
}

public long getMaxWait() {
    return maxWait;
}

public void setMaxWait(long maxWait) {
    this.maxWait = maxWait;
}

public byte getWhenExhaustedAction() {
    return whenExhaustedAction;
}

public void setWhenExhaustedAction(byte whenExhaustedAction) {
    this.whenExhaustedAction = whenExhaustedAction;
}

public boolean isTestOnBorrow() {
    return testOnBorrow;
}

public void setTestOnBorrow(boolean testOnBorrow) {
    this.testOnBorrow = testOnBorrow;
}

public boolean isTestOnReturn() {
    return testOnReturn;
}

public void setTestOnReturn(boolean testOnReturn) {
    this.testOnReturn = testOnReturn;
}

public boolean isTestWhileIdle() {
    return testWhileIdle;
}

public void setTestWhileIdle(boolean testWhileIdle) {
    this.testWhileIdle = testWhileIdle;
}

public long getTimeBetweenEvictionRunsMillis() {
    return timeBetweenEvictionRunsMillis;
}

public void setTimeBetweenEvictionRunsMillis(
        long timeBetweenEvictionRunsMillis) {
    this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
}

public int getNumTestsPerEvictionRun() {
    return numTestsPerEvictionRun;
}

public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
    this.numTestsPerEvictionRun = numTestsPerEvictionRun;
}

public long getMinEvictableIdleTimeMillis() {
    return minEvictableIdleTimeMillis;
}

public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
    this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
}

public long getSoftMinEvictableIdleTimeMillis() {
    return softMinEvictableIdleTimeMillis;
}

public void setSoftMinEvictableIdleTimeMillis(
        long softMinEvictableIdleTimeMillis) {
    this.softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis;
}

}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/64044.html

相關(guān)文章

  • 一文輕松搞懂redis集群原理搭建與使用

    摘要:使用操作輸出內(nèi)容歡迎關(guān)注微信公眾號(hào)面試通關(guān)手冊(cè)關(guān)閉連接,每次使用完畢后關(guān)閉連接。測(cè)試集群版創(chuàng)建一個(gè)對(duì)象。 今天早上由于zookeeper和redis集群不在同一虛擬機(jī)導(dǎo)致出了點(diǎn)很小錯(cuò)誤(人為),所以這里總結(jié)一下redis集群的搭建以便日后所需同時(shí)也希望能對(duì)你有所幫助。筆主這里使用的是Centos7.如果你碰到任何問(wèn)題都可以來(lái)問(wèn)我,留言或者加我微信:bwcx9393. 關(guān)于Linux的一...

    jiekechoo 評(píng)論0 收藏0
  • 自定義注解@RedisLock分布式鎖用法原理

    摘要:前言最近開發(fā)公司的項(xiàng)目,遇到了分布式的場(chǎng)景,即,同一條數(shù)據(jù)可能被多臺(tái)服務(wù)器或者說(shuō)多個(gè)線程同時(shí)修改,此時(shí)可能會(huì)出現(xiàn)分布式事務(wù)的問(wèn)題,隨即封裝了分布式鎖的注解。 前言 最近開發(fā)公司的項(xiàng)目,遇到了分布式的場(chǎng)景,即,同一條數(shù)據(jù)可能被多臺(tái)服務(wù)器或者說(shuō)多個(gè)線程同時(shí)修改,此時(shí)可能會(huì)出現(xiàn)分布式事務(wù)的問(wèn)題,隨即封裝了redis分布式鎖的注解。 場(chǎng)景分析 前提:我的銀行卡有0元錢,現(xiàn)在有A,B兩個(gè)人,想分...

    oujie 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<