jedis連接池對commons-pool的封裝示例詳解
序
文本主要研究一下jedis連接池對commons-pool的封裝
JedisPoolConfig
jedis-3.8.0-sources.jar!/redis/clients/jedis/JedisPoolConfig.java
public class JedisPoolConfig extends GenericObjectPoolConfig<Jedis> {
public JedisPoolConfig() {
// defaults to make your life with connection pool easier :)
setTestWhileIdle(true);
setMinEvictableIdleTimeMillis(60000);
setTimeBetweenEvictionRunsMillis(30000);
setNumTestsPerEvictionRun(-1);
}
}JedisPoolConfig繼承了GenericObjectPoolConfig,在構(gòu)造器里頭設(shè)置了默認(rèn)的參數(shù),即testWhileIdle為true,minEvictableIdleTime為60s,timeBetweenEvictionRuns為30s,numTestsPerEvictionRun為-1
JedisFactory
見上一篇文章聊聊JedisFactory
Pool
jedis-3.8.0-sources.jar!/redis/clients/jedis/util/Pool.java
public abstract class Pool<T> implements Closeable {
/**
* @deprecated This will be private in future.
*/
@Deprecated
protected GenericObjectPool<T> internalPool;
public Pool(final GenericObjectPoolConfig<T> poolConfig, PooledObjectFactory<T> factory) {
initPool(poolConfig, factory);
}
/**
* @param poolConfig
* @param factory
* @deprecated This method will be private in future.
*/
@Deprecated
public void initPool(final GenericObjectPoolConfig<T> poolConfig, PooledObjectFactory<T> factory) {
if (this.internalPool != null) {
try {
closeInternalPool();
} catch (Exception e) {
}
}
this.internalPool = new GenericObjectPool<>(factory, poolConfig);
}
public T getResource() {
try {
return internalPool.borrowObject();
} catch (NoSuchElementException nse) {
if (null == nse.getCause()) { // The exception was caused by an exhausted pool
throw new JedisExhaustedPoolException(
"Could not get a resource since the pool is exhausted", nse);
}
// Otherwise, the exception was caused by the implemented activateObject() or ValidateObject()
throw new JedisException("Could not get a resource from the pool", nse);
} catch (Exception e) {
throw new JedisConnectionException("Could not get a resource from the pool", e);
}
}
public void returnResource(final T resource) {
if (resource != null) {
returnResourceObject(resource);
}
}
/**
* @param resource
* @deprecated This will be removed in next major release. Use {@link Pool#returnResource(java.lang.Object)}.
*/
@Deprecated
protected void returnResourceObject(final T resource) {
try {
internalPool.returnObject(resource);
} catch (RuntimeException e) {
throw new JedisException("Could not return the resource to the pool", e);
}
}
public void destroy() {
closeInternalPool();
}
/**
* @deprecated This will be removed in next major release. Use {@link Pool#destroy()}.
*/
@Deprecated
protected void closeInternalPool() {
try {
internalPool.close();
} catch (RuntimeException e) {
throw new JedisException("Could not destroy the pool", e);
}
}
/**
* @param resource
* @deprecated This will be removed in next major release. Use {@link Pool#returnBrokenResource(java.lang.Object)}.
*/
@Deprecated
protected void returnBrokenResourceObject(final T resource) {
try {
internalPool.invalidateObject(resource);
} catch (Exception e) {
throw new JedisException("Could not return the broken resource to the pool", e);
}
}
//......
}Pool聲明實(shí)現(xiàn)Closeable接口,它的構(gòu)造器根據(jù)GenericObjectPoolConfig和PooledObjectFactory來創(chuàng)建GenericObjectPool,它的getResource、returnResource、returnBrokenResourceObject、destroy方法內(nèi)部都是委托給了GenericObjectPool
它有一個實(shí)現(xiàn)類JedisPoolAbstract,而JedisPoolAbstract還有兩個子類,分別是JedisPool、JedisSentinelPool
JedisPoolAbstract
jedis-3.8.0-sources.jar!/redis/clients/jedis/JedisPoolAbstract.java
/**
* @deprecated This class will be removed in future. If you are directly manipulating this class,
* you are suggested to change your code to use {@link Pool Pool<Jedis>} instead.
*/
@Deprecated
public class JedisPoolAbstract extends Pool<Jedis> {
/**
* Using this constructor means you have to set and initialize the internalPool yourself.
*
* @deprecated This constructor will be removed in future.
*/
@Deprecated
public JedisPoolAbstract() {
super();
}
public JedisPoolAbstract(GenericObjectPoolConfig<Jedis> poolConfig,
PooledObjectFactory<Jedis> factory) {
super(poolConfig, factory);
}
}這個類未來將要被廢棄,它主要是設(shè)置了Pool的泛型為Jedis
JedisPool
jedis-3.8.0-sources.jar!/redis/clients/jedis/JedisPool.java
public class JedisPool extends JedisPoolAbstract {
//......
@Override
public Jedis getResource() {
Jedis jedis = super.getResource();
jedis.setDataSource(this);
return jedis;
}
@Override
public void returnResource(final Jedis resource) {
if (resource != null) {
try {
resource.resetState();
returnResourceObject(resource);
} catch (RuntimeException e) {
returnBrokenResource(resource);
log.warn("Resource is returned to the pool as broken", e);
}
}
}
}JedisPool覆蓋了getResource和returnResource方法,其中g(shù)etResource新增了設(shè)置dataSource給jedis;returnResource方法新增了jedis的resetState操作,return有異常的話會執(zhí)行returnBrokenResource
JedisSentinelPool
jedis-3.8.0-sources.jar!/redis/clients/jedis/JedisSentinelPool.java
public class JedisSentinelPool extends JedisPoolAbstract {
@Override
public Jedis getResource() {
while (true) {
Jedis jedis = super.getResource();
jedis.setDataSource(this);
// get a reference because it can change concurrently
final HostAndPort master = currentHostMaster;
final HostAndPort connection = new HostAndPort(jedis.getClient().getHost(), jedis.getClient()
.getPort());
if (master.equals(connection)) {
// connected to the correct master
return jedis;
} else {
returnBrokenResource(jedis);
}
}
}
@Override
public void returnResource(final Jedis resource) {
if (resource != null) {
try {
resource.resetState();
returnResourceObject(resource);
} catch (RuntimeException e) {
returnBrokenResource(resource);
log.debug("Resource is returned to the pool as broken", e);
}
}
}
//......
}JedisSentinelPool覆蓋了getResource和returnResource方法,其中g(shù)etResource新增了設(shè)置dataSource給jedis,然后判斷master;returnResource方法新增了jedis的resetState操作,return有異常的話會執(zhí)行returnBrokenResource
小結(jié)
jedis主要有三個對象對commons-pool進(jìn)行包裝,分別是JedisPoolConfig(繼承了GenericObjectPoolConfig),JedisFactory(實(shí)現(xiàn)了PooledObjectFactory<Jedis>接口)、Pool(提供了get和return方法,內(nèi)部委托給GenericObjectPool)
JedisPoolConfig繼承了GenericObjectPoolConfig,在構(gòu)造器里頭設(shè)置了默認(rèn)的參數(shù),即testWhileIdle為true,minEvictableIdleTime為60s,timeBetweenEvictionRuns為30s,numTestsPerEvictionRun為-1
以上就是jedis連接池對commons-pool的封裝示例詳解的詳細(xì)內(nèi)容,更多關(guān)于jedis連接池封裝commons-pool的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決rror updating database.Cause:java.sql.SQLSyntaxE
這篇文章主要介紹了解決rror updating database.Cause:java.sql.SQLSyntaxErrorException問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
Java ArrayList如何實(shí)現(xiàn)生成不重復(fù)隨機(jī)數(shù)
這篇文章主要介紹了Java ArrayList如何實(shí)現(xiàn)生成不重復(fù)隨機(jī)數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
SpringBoot中日志切面實(shí)現(xiàn)小結(jié)
本文介紹了SpringBoot中日志切面實(shí)現(xiàn)小結(jié),通過定義一個自定義注解和創(chuàng)建一個日志切面類,為方法添加日志記錄功能,感興趣的可以了解一下2024-11-11
spring boot環(huán)境抽象的實(shí)現(xiàn)方法
在實(shí)際開發(fā)中,開發(fā)人員在編寫springboot的時候通常要在本地環(huán)境測試然后再部署到Production環(huán)境,這兩種環(huán)境一般來講是不同的,最主要的區(qū)別就是數(shù)據(jù)源的不同。本文主要介紹了這兩種,感興趣的可以了解一下2019-04-04
spring mvc DispatcherServlet之前端控制器架構(gòu)詳解
這篇文章主要為大家詳細(xì)介紹了spring mvc DispatcherServlet之前端控制器架構(gòu),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
SpringBoot獲取ApplicationContext的3種方式
這篇文章主要為大家詳細(xì)介紹了SpringBoot獲取ApplicationContext的3種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09
SpringMVC框架搭建idea2021.3.2操作數(shù)據(jù)庫的示例詳解
這篇文章主要介紹了SpringMVC框架搭建idea2021.3.2操作數(shù)據(jù)庫,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04

