昆明java培训
达内昆明广州春城路

18487146383

热门课程

JedisPool的实现

  • 时间:2016-08-31
  • 发布:昆明Java培训
  • 来源:达内新闻

昆明Java培训机构的老师带大家看JedisPool的实现,由于JedisPool继承Pool<T>,所以我们主要看Pool<T>的部分代码:

public abstract class Pool<T> implements Closeable {

protected GenericObjectPool<T> internalPool;

public Pool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {

initPool(poolConfig, factory);

}

public boolean isClosed() {

return this.internalPool.isClosed();

}

public void initPool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {

if (this.internalPool != null) {

try {

closeInternalPool();

} catch (Exception e) {

}

}

this.internalPool = new GenericObjectPool<T>(factory, poolConfig);

}

public T getResource() {

try {

return internalPool.borrowObject();

} catch (NoSuchElementException nse) {

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);

}

}

protected void returnResourceObject(final T resource) {

if (resource == null) {

return;

}

try {

internalPool.returnObject(resource);

} catch (Exception e) {

throw new JedisException("Could not return the resource to the pool", e);

}

}

public void addObjects(int count) {

try {

for (int i = 0; i < count; i++) {

this.internalPool.addObject();

}

} catch (Exception e) {

throw new JedisException("Error trying to add idle objects", e);

}

}

}

JedisPool通过内部引用GenericObjectPool,包装其接口的装饰者模式,相比继承来说这种模式更加灵活。JedisPool的构造方法需要将JedisFactory以及JedisPoolConfig创建标准的ObjectPool作为自己的成员变量。所以pool.getResource()方法的背后还是调用PoolObject.borrowObject()。

了解详情请登陆昆明达内Java培训官网(km.Java.tedu.cn)!

上一篇:edisFactory的实现
下一篇:预初始化参数

腾讯游戏Switch独立销量领先——昆明达内

达内java语言编程学以致用

苹果技术:A11芯片上新菜【达内培训】

达内培训之国产手机vivo领跑,小米再上榜

选择城市和中心
贵州省

广西省

海南省