rediscluster本质是连接池吗?使用 apache-common-pool2 创建自己的rediscluster资源池
下面是我写的代码,如有问题请指出
package test;
import java.util.LinkedHashSet;
import java.util.Set;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
public class LpPoolFactory implements PooledObjectFactory<JedisCluster> {
/**
* 功能描述:激活资源对象
*
* 什么时候会调用此方法
* 1:从资源池中获取资源的时候
* 2:资源回收线程,回收资源的时候,根据配置的 testWhileIdle 参数,判断 是否执行 factory.activateObject()方法,true 执行,false 不执行
* @param arg0
*/
public void activateObject(PooledObject<JedisCluster> arg0) throws Exception {
System.out.println("activate Object");
}
public void destroyObject(PooledObject<JedisCluster> arg0) throws Exception {
System.out.println("destroy Object");
JedisCluster JedisCluster = arg0.getObject();
JedisCluster = null;
}
public PooledObject<JedisCluster> makeObject() throws Exception {
System.out.println("make Object");
JedisPoolConfig poolConfig = new JedisPoolConfig();
// 最大连接数
poolConfig.setMaxTotal(300);
// 最大空闲数
poolConfig.setMaxIdle(10);
// 最大允许等待时间,如果超过这个时间还未获取到连接,则会报JedisException异常:
// Could not get a resource from the pool
poolConfig.setMaxWaitMillis(1000);
Set<HostAndPort> nodes = new LinkedHashSet<HostAndPort>();
nodes.add(new HostAndPort("192.168.88.140", 7000));
nodes.add(new HostAndPort("192.168.88.140", 7001));
nodes.add(new HostAndPort("192.168.88.140", 7002));
nodes.add(new HostAndPort("192.168.88.140", 7003));
nodes.add(new HostAndPort("192.168.88.140", 7004));
nodes.add(new HostAndPort("192.168.88.140", 7005));
JedisCluster JedisCluster = new JedisCluster(nodes, poolConfig);
return new DefaultPooledObject<JedisCluster>(JedisCluster);
}
/**
* 功能描述:钝化资源对象
*
* 什么时候会调用此方法
* 1:将资源返还给资源池时,调用此方法。
*/
public void passivateObject(PooledObject<JedisCluster> arg0) throws Exception {
System.out.println("passivate Object");
}
/**
* 功能描述:判断资源对象是否有效,有效返回 true,无效返回 false
*
* 什么时候会调用此方法
* 1:从资源池中获取资源的时候,参数 testOnBorrow 或者 testOnCreate 中有一个 配置 为 true 时,则调用 factory.validateObject() 方法
* 2:将资源返还给资源池的时候,参数 testOnReturn,配置为 true 时,调用此方法
* 3:资源回收线程,回收资源的时候,参数 testWhileIdle,配置为 true 时,调用此方法
*/
public boolean validateObject(PooledObject<JedisCluster> arg0) {
System.out.println("validate Object");
return true;
}
}
连接池测试类
package test;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.JedisCluster;
public class testpool {
public static void main(String[] args) throws Exception{
//工厂
LpPoolFactory factory = new LpPoolFactory();
//资源池配置
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMinIdle(2);
//创建资源池
GenericObjectPool<JedisCluster> objectPool = new GenericObjectPool<JedisCluster>(factory,poolConfig);
for(int i=0; i<3; i++) {
Thread.sleep(1000);
//获取资源对象
JedisCluster jedisCluster = objectPool.borrowObject();
//将获取的资源对象,返还给资源池
objectPool.returnObject(jedisCluster);
//输出资源对象
System.out.println(jedisCluster);
System.out.println();
}
}
}
不知道这样是不是可以实现rediscluster的连接池