org.darkphoenixs.pool.redis.RedisShardedConnPoolOld Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of connectionpool-client Show documentation
Show all versions of connectionpool-client Show documentation
A simple multi-purpose connection pool client (Kafka & Hbase & Redis & RMDB & Socket & Http)
The newest version!
/*
* Copyright 2015-2016 Dark Phoenixs (Open-Source Organization).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.darkphoenixs.pool.redis;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.darkphoenixs.pool.ConnectionFactory;
import org.darkphoenixs.pool.ConnectionPool;
import org.darkphoenixs.pool.PoolBase;
import org.darkphoenixs.pool.PoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.util.Hashing;
import java.util.List;
import java.util.regex.Pattern;
/**
* Title: RedisShardedConnPoolOld
* Description: Redis分片连接池
*
* @author Victor
* @version 1.0
* @see PoolBase
* @see ConnectionPool
* @since 2015年9月19日
*/
@Deprecated
public class RedisShardedConnPoolOld extends PoolBase implements
ConnectionPool {
/**
* serialVersionUID
*/
private static final long serialVersionUID = -273019562990831722L;
/**
* Title: RedisShardedConnPoolOld
* Description: 构造方法
*
* @param poolConfig 池配置
* @param shards 分片列表
*/
public RedisShardedConnPoolOld(final PoolConfig poolConfig,
List shards) {
this(poolConfig, shards, Hashing.MURMUR_HASH);
}
/**
* Title: RedisShardedConnPoolOld
* Description: 构造方法
*
* @param poolConfig 池配置
* @param shards 分片列表
* @param algo 数字哈希
*/
public RedisShardedConnPoolOld(final PoolConfig poolConfig,
List shards, Hashing algo) {
this(poolConfig, shards, algo, null);
}
/**
* Title: RedisShardedConnPoolOld
* Description: 构造方法
*
* @param poolConfig 池配置
* @param shards 分片列表
* @param keyTagPattern 键表达式
*/
public RedisShardedConnPoolOld(final PoolConfig poolConfig,
List shards, Pattern keyTagPattern) {
this(poolConfig, shards, Hashing.MURMUR_HASH, keyTagPattern);
}
/**
* Title: RedisShardedConnPoolOld
* Description: 构造方法
*
* @param poolConfig 池配置
* @param shards 分片列表
* @param algo 数字哈希
* @param keyTagPattern 键表达式
*/
public RedisShardedConnPoolOld(final PoolConfig poolConfig,
List shards, Hashing algo, Pattern keyTagPattern) {
initPool(poolConfig, new RedisShardedConnFactory(shards, algo,
keyTagPattern));
}
@Override
public ShardedJedis getConnection() {
return super.getResource();
}
@Override
public void returnConnection(ShardedJedis conn) {
boolean broken = false;
for (Jedis jedis : conn.getAllShards())
if (jedis.getClient().isBroken()) {
broken = true;
break;
}
if (broken)
super.invalidateResource(conn);
else
super.returnResource(conn);
}
@Override
public void invalidateConnection(ShardedJedis conn) {
super.invalidateResource(conn);
}
/**
* Title: RedisShardedConnFactory
* Description: Redis分片连接工厂
*
* @author Victor
* @version 1.0
* @see ConnectionFactory
* @since 2015年9月19日
*/
protected class RedisShardedConnFactory implements
ConnectionFactory {
/**
* serialVersionUID
*/
private static final long serialVersionUID = -8620587542840002906L;
/**
* shards
*/
private List shards;
/**
* algo
*/
private Hashing algo;
/**
* keyTagPattern
*/
private Pattern keyTagPattern;
/**
* Title: RedisShardedConnFactory
* Description: 构造方法
*
* @param shards 分片列表
* @param algo 数字哈希
* @param keyTagPattern 键表达式
*/
public RedisShardedConnFactory(List shards,
Hashing algo, Pattern keyTagPattern) {
this.shards = shards;
this.algo = algo;
this.keyTagPattern = keyTagPattern;
}
@Override
public PooledObject makeObject() throws Exception {
ShardedJedis jedis = this.createConnection();
return new DefaultPooledObject(jedis);
}
@Override
public void destroyObject(PooledObject pooledShardedJedis)
throws Exception {
final ShardedJedis shardedJedis = pooledShardedJedis.getObject();
for (Jedis jedis : shardedJedis.getAllShards()) {
try {
try {
jedis.quit();
} catch (Exception e) {
}
jedis.disconnect();
} catch (Exception e) {
}
}
}
@Override
public boolean validateObject(
PooledObject pooledShardedJedis) {
try {
ShardedJedis jedis = pooledShardedJedis.getObject();
for (Jedis shard : jedis.getAllShards()) {
if (!shard.ping().equals("PONG")) {
return false;
}
}
return true;
} catch (Exception ex) {
return false;
}
}
@Override
public void activateObject(PooledObject p)
throws Exception {
}
@Override
public void passivateObject(PooledObject p)
throws Exception {
}
@Override
public ShardedJedis createConnection() throws Exception {
ShardedJedis jedis = new ShardedJedis(shards, algo, keyTagPattern);
return jedis;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy