jedis.propertise 注意以前版本的maxAcitve和maxWait有所改变,JVM根据系统环境变量ServerType中的值 取不同的配置,实现多环境(测试环境、生产环境)集成。

redis.pool.maxTotal=redis.pool.maxActive.${ServerType}
redis.pool.maxIdle=redis.pool.maxIdle.${ServerType}
redis.pool.maxWaitMillis=redis.pool.maxWait.${ServerType}
redis.pool.minIdle=redis.pool.minIdle.${ServerType}
redis.host=redis.host.${ServerType}
redis.port=redis.port.${ServerType}
redis.password=redis.password.${ServerType}
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
redis.timeout=1000
#C
#REL
redis.pool.maxActive.REL=200
redis.pool.maxIdle.REL=50
redis.pool.minIdle.REL=10
redis.pool.maxWait.REL=300
redis.host.REL=192.168.0.201
redis.port.REL=8989
redis.password.REL=beidou123 #VRF
redis.pool.maxActive.VRF=200
redis.pool.maxIdle.VRF=50
redis.pool.minIdle.VRF=10
redis.pool.maxWait.VRF=300
redis.host.VRF=192.168.0.201
redis.port.VRF=8989
redis.password.VRF=beidou123

config的包装便于后面xml的注入

/**
* redis连接池配置文件包装类
*
* @author daxiong
* date: 2017/03/07 11:51
*/
public class GenericObjectPoolConfigWrapper extends GenericObjectPoolConfig { public GenericObjectPoolConfig getConfig() {
return this;
} }

spring配置

<!--redis连接池配置-->
<context:property-placeholder location="classpath:jedis.properties" ignore-unresolvable="true"/>
<bean id="jedisPoolConfig" class="smm.mvc.CacheDb.GenericObjectPoolConfigWrapper">
<!--最大连接数-->
<property name="maxTotal" value="${${redis.pool.maxTotal}}" />
<!--最大空闲连接数-->
<property name="maxIdle" value="${${redis.pool.maxIdle}}" />
<!--初始化连接数-->
<property name="minIdle" value="${${redis.pool.minIdle}}"/>
<!--最大等待时间-->
<property name="maxWaitMillis" value="${${redis.pool.maxWaitMillis}}" />
<!--对拿到的connection进行validateObject校验-->
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
<!--在进行returnObject对返回的connection进行validateObject校验-->
<property name="testOnReturn" value="${redis.pool.testOnReturn}" />
<!--定时对线程池中空闲的链接进行validateObject校验-->
<property name="testWhileIdle" value="true" />
</bean> <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy">
<constructor-arg index="0">
<bean factory-bean="jedisPoolConfig" factory-method="getConfig"/>
</constructor-arg>
<constructor-arg index="1" value="${${redis.host}}"/>
<constructor-arg index="2" value="${${redis.port}}"/>
<!--timeout-->
<constructor-arg index="3" value="${redis.timeout}"/>
<constructor-arg index="4" value="${${redis.password}}"/>
</bean>

jedis工具类

ublic abstract class JCacheTools {
public abstract int getDBIndex();
/**
* 默认日志打印logger_default
*/
public static Logger logger_default = Logger.getLogger("logger_jCache_default");
/**
* 失败日志logger,用于定期del指定的key
*/
public static Logger logger_failure = Logger.getLogger("logger_jCache_failure"); @Resource
protected JedisPool jedisPool; protected Jedis getJedis() throws JedisException {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
} catch (JedisException e) {
LogContext.instance().warn(logger_failure, "failed:jedisPool getResource.", e);
if(jedis!=null){
jedisPool.returnBrokenResource(jedis);
}
throw e;
}
return jedis;
} protected void release(Jedis jedis, boolean isBroken) {
if (jedis != null) {
if (isBroken) {
jedisPool.returnBrokenResource(jedis);
} else {
jedisPool.returnResource(jedis);
}
}
} protected String addStringToJedis(String key, String value, int cacheSeconds, String methodName) {
Jedis jedis = null;
boolean isBroken = false;
String lastVal = null;
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
lastVal = jedis.getSet(key, value);
if(cacheSeconds!=0){
jedis.expire(key,cacheSeconds);
}
LogContext.instance().debug(logger_default, "succeed:" + methodName, key, value);
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, key, value, e);
} finally {
release(jedis, isBroken);
}
return lastVal;
} protected void addStringToJedis(Map<String,String> batchData, int cacheSeconds, String methodName) {
Jedis jedis = null;
boolean isBroken = false;
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
Pipeline pipeline = jedis.pipelined();
for(Map.Entry<String,String> element:batchData.entrySet()){
if(cacheSeconds!=0){
pipeline.setex(element.getKey(),cacheSeconds,element.getValue());
}else{
pipeline.set(element.getKey(),element.getValue());
}
}
pipeline.sync();
LogContext.instance().debug(logger_default, "succeed:" + methodName,batchData.size());
} catch (Exception e) {
isBroken = true;
e.printStackTrace();
} finally {
release(jedis, isBroken);
}
} protected void addListToJedis(String key, List<String> list, int cacheSeconds, String methodName) {
if (list != null && list.size() > 0) {
Jedis jedis = null;
boolean isBroken = false;
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
if (jedis.exists(key)) {
jedis.del(key);
}
for (String aList : list) {
jedis.rpush(key, aList);
}
if(cacheSeconds!=0){
jedis.expire(key, cacheSeconds);
}
LogContext.instance().debug(logger_default, "succeed:" + methodName, key, list.size());
} catch (JedisException e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, key, list.size(), e);
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, key, list.size(), e);
} finally {
release(jedis, isBroken);
}
}
} protected void addToSetJedis(String key, String[] value, String methodName) {
Jedis jedis = null;
boolean isBroken = false;
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
jedis.sadd(key,value);
LogContext.instance().debug(logger_default, "succeed:" + methodName, key, value);
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, key, value, e);
} finally {
release(jedis, isBroken);
}
} protected void removeSetJedis(String key,String value, String methodName) {
Jedis jedis = null;
boolean isBroken = false;
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
jedis.srem(key,value);
LogContext.instance().debug(logger_default, "succeed:" + methodName, key, value);
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, key, value, e);
} finally {
release(jedis, isBroken);
}
} protected void pushDataToListJedis(String key, String data, int cacheSeconds, String methodName) {
Jedis jedis = null;
boolean isBroken = false;
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
jedis.rpush(key, data);
if(cacheSeconds!=0){
jedis.expire(key,cacheSeconds);
}
LogContext.instance().debug(logger_default, "succeed:" + methodName, key, data);
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, key, data, e);
} finally {
release(jedis, isBroken);
}
}
protected void pushDataToListJedis(String key,List<String> batchData, int cacheSeconds, String methodName) {
Jedis jedis = null;
boolean isBroken = false;
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
jedis.del(key);
jedis.lpush(key,batchData.toArray(new String[batchData.size()]));
if(cacheSeconds!=0)
jedis.expire(key,cacheSeconds);
LogContext.instance().debug(logger_default, "succeed:" + methodName,batchData!=null?batchData.size():0);
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, batchData!=null?batchData.size():0, e);
} finally {
release(jedis, isBroken);
}
} /**
* 删除list中的元素
* @param key
* @param values
* @param methodName
*/
protected void deleteDataFromListJedis(String key,List<String> values, String methodName) {
Jedis jedis = null;
boolean isBroken = false;
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
Pipeline pipeline = jedis.pipelined();
if(values!=null && !values.isEmpty()){
for (String val:values){
pipeline.lrem(key,0,val);
}
}
pipeline.sync();
LogContext.instance().debug(logger_default, "succeed:" + methodName,values!=null?values.size():0);
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, values!=null?values.size():0, e);
} finally {
release(jedis, isBroken);
}
} protected void addHashMapToJedis(String key, Map<String, String> map, int cacheSeconds, boolean isModified, String methodName) {
boolean isBroken = false;
Jedis jedis = null;
if (map != null && map.size() > 0) {
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
jedis.hmset(key, map);
if (cacheSeconds >= 0)
jedis.expire(key, cacheSeconds);
LogContext.instance().debug(logger_default, "succeed:" + methodName, key, map.size());
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, key, map.size(), e);
} finally {
release(jedis, isBroken);
}
}
} protected void addHashMapToJedis(String key, String field, String value, int cacheSeconds, String methodName) {
boolean isBroken = false;
Jedis jedis = null;
try {
jedis = this.getJedis();
if (jedis != null) {
jedis.select(getDBIndex());
jedis.hset(key, field, value);
jedis.expire(key, cacheSeconds);
LogContext.instance().debug(logger_default, "succeed:" + methodName, key, field, value);
}
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, key, field, value, e);
}finally {
release(jedis, isBroken);
}
} protected void updateHashMapToJedis(String key, String incrementField, long incrementValue, String dateField, String dateValue, String methodName) {
boolean isBroken = false;
Jedis jedis = null;
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
jedis.hincrBy(key, incrementField, incrementValue);
jedis.hset(key, dateField, dateValue);
LogContext.instance().debug(logger_default, "succeed:" + methodName, key, incrementField, incrementValue);
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, key, incrementField, incrementValue, e);
}finally {
release(jedis, isBroken);
}
} public String getStringFromJedis(String key, String methodName) {
String value = null;
Jedis jedis = null;
boolean isBroken = false;
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
if (jedis.exists(key)) {
value = jedis.get(key);
value = StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value)?value:null;
LogContext.instance().debug(logger_default, "succeed:" + methodName, key, value);
}
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, key, value, e);
} finally {
release(jedis, isBroken);
}
return value;
} public List<String> getStringFromJedis(String[] keys, String methodName) {
Jedis jedis = null;
boolean isBroken = false;
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
return jedis.mget(keys);
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, Arrays.toString(keys), e);
} finally {
release(jedis, isBroken);
}
return null;
} protected List<String> getListFromJedis(String key, String methodName) throws JMSCacheException {
List<String> list = null;
boolean isBroken = false;
Jedis jedis = null;
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
if (jedis.exists(key)) {
list = jedis.lrange(key, 0, -1);
LogContext.instance().debug(logger_default, "succeed:" + methodName, key, list != null ? list.size() : 0);
}
} catch (JedisException e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, key, list != null ? list.size() : 0, e);
} finally {
release(jedis, isBroken);
}
return list;
} protected Set<String> getSetFromJedis(String key, String methodName) throws JMSCacheException {
Set<String> list = null;
boolean isBroken = false;
Jedis jedis = null;
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
if (jedis.exists(key)) {
list = jedis.smembers(key);
LogContext.instance().debug(logger_default, "succeed:" + methodName, key, list != null ? list.size() : 0);
}
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, key, list != null ? list.size() : 0, e);
} finally {
release(jedis, isBroken);
}
return list;
} protected Map<String, String> getHashMapFromJedis(String key, String methodName) {
Map<String, String> hashMap = null;
boolean isBroken = false;
Jedis jedis = null;
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
hashMap = jedis.hgetAll(key);
LogContext.instance().debug(logger_default, "succeed:" + methodName, key, hashMap != null ? hashMap.size() : 0);
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, key, hashMap != null ? hashMap.size() : 0, e);
} finally {
release(jedis, isBroken);
}
return hashMap;
} protected String getHashMapValueFromJedis(String key, String field, String methodName) {
String value = null;
boolean isBroken = false;
Jedis jedis = null;
try {
jedis = this.getJedis();
if (jedis != null) {
jedis.select(getDBIndex());
if (jedis.exists(key)) {
value = jedis.hget(key, field);
LogContext.instance().debug(logger_default, "succeed:" + methodName, key, field, value);
}
}
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, key, field, value, e);
} finally {
release(jedis, isBroken);
}
return value;
} public Long getIdentifyId(String identifyName ,String methodName) {
boolean isBroken = false;
Jedis jedis = null;
Long identify=null;
try {
jedis = this.getJedis();
if (jedis != null) {
jedis.select(getDBIndex());
identify = jedis.incr(identifyName);
if(identify==0){
return jedis.incr(identifyName);
}else {
return identify;
}
}
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:" + methodName, identifyName, "", identify, e);
} finally {
release(jedis, isBroken);
}
return null;
} /**
* 删除某db的某个key值
* @param key
* @return
*/
public Long delKeyFromJedis(String key) {
boolean isBroken = false;
Jedis jedis = null;
long result = 0;
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
LogContext.instance().debug(logger_default, "succeed:delKeyFromJedis");
return jedis.del(key);
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:delKeyFromJedis", e);
} finally {
release(jedis, isBroken);
}
return result;
} /**
* 根据dbIndex flushDB每个shard
*
* @param dbIndex
*/
public void flushDBFromJedis(int dbIndex) {
Jedis jedis = null;
boolean isBroken = false;
try {
jedis = this.getJedis();
jedis.select(dbIndex);
jedis.flushDB();
LogContext.instance().debug(logger_default, "succeed:flushDBFromJedis");
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:flushDBFromJedis", e);
} finally {
release(jedis, isBroken);
}
} public boolean existKey(String key, String methodName) {
Jedis jedis = null;
boolean isBroken = false;
try {
jedis = this.getJedis();
jedis.select(getDBIndex());
LogContext.instance().debug(logger_default, "succeed:"+methodName);
return jedis.exists(key);
} catch (Exception e) {
isBroken = true;
LogContext.instance().warn(logger_failure, "failed:"+methodName, e);
} finally {
release(jedis, isBroken);
}
return false;
} }

关于jedis2.4以上版本的连接池配置,及工具类的更多相关文章

  1. 【知了堂学习心得】浅谈c3p0连接池和dbutils工具类的使用

    1. C3P0概述 C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展.目前使用它的开源项目有Hibernate,Spring等. 2. C3P ...

  2. mysql连接池的使用工具类代码示例

    mysql连接池代码工具示例(scala): import java.sql.{Connection,PreparedStatement,ResultSet} import org.apache.co ...

  3. [JavaEE] Hibernate连接池配置测试

    转载自51CTO http://developer.51cto.com/art/200906/129914.htm Hibernate支持第三方的连接池,官方推荐的连接池是C3P0,Proxool,以 ...

  4. hibernate+mysql的连接池配置

    1:连接池的必知概念    首先,我们还是老套的讲讲连接池的基本概念,概念理解清楚了,我们也知道后面是怎么回事了. 以前我们程序连接数据库的时候,每一次连接数据库都要一个连接,用完后再释放.如果频繁的 ...

  5. Java Mysql连接池配置和案例分析--超时异常和处理

    前言: 最近在开发服务的时候, 发现服务只要一段时间不用, 下次首次访问总是失败. 该问题影响虽不大, 但终究影响用户体验. 观察日志后发现, mysql连接因长时间空闲而被关闭, 使用时没有死链检测 ...

  6. c3p0、dbcp、tomcat jdbc pool 连接池配置简介及常用数据库的driverClass和驱动包

    [-] DBCP连接池配置 dbcp jar包 c3p0连接池配置 c3p0 jar包 jdbc-pool连接池配置 jdbc-pool jar包 常用数据库的driverClass和jdbcUrl ...

  7. web 连接池配置

    TOMCAT J2EE项目连接池配置 web 项目的 web.xml <web-app> <resource-ref> <description>DB Connec ...

  8. 6.13-C3p0连接池配置,DBUtils使用

    DBCP连接池 一.C3p0连接池配置 开源的JDBC连接池 使用连接池的好处: 减轻数据库服务器压力 数据源: ComboPooledDataSource ComboPooledDataSource ...

  9. 06-spring常见的连接池配置

    1 准备工作 首先,我们准备jdbc属性文件 database.properties,用于保存连接数据库的信息,利于我们在配置文件中使用. 只要在applicationContext.xml(Spri ...

随机推荐

  1. iterm2切换显示屏vim乱行解决

    http://note.youdao.com/noteshare?id=5aec9d82cc3a95b6909e9966b4aa3227

  2. css基础--常用css属性02

    上篇地址:css基础--常用css属性01 本文参考菜鸟教程和w3school 1  浮动和清除浮动 在上篇的第十一节--定位中说道: CSS 有三种基本的定位机制:普通流.浮动和绝对定位. 普通流和 ...

  3. 逻辑回归原理_挑战者飞船事故和乳腺癌案例_Python和R_信用评分卡(AAA推荐)

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  4. oracle表结构和数据导出时的一些勾选项说明

    使用pl/sql developer导出oracle数据库的表结构和表数据时,有一些勾选项供用户选择,需要用户根据实际情况进行勾选或取消. 导出方法如下:一.只导出表结构1.使用pl/sql deve ...

  5. 调用weka模拟实现 “主动学习“ 算法

    主动学习: 主动学习的过程:需要分类器与标记专家进行交互.一个典型的过程: (1)基于少量已标记样本构建模型 (2)从未标记样本中选出信息量最大的样本,交给专家进行标记 (3)将这些样本与之前样本进行 ...

  6. RHEL-7.0重置root密码

    RHCE考试第一个环节就是重置root密码,然而7系列与6系列又存在着很大的不同.以下为RHEL-7.0系统对root密码重置的步骤! 1.开机出现引导菜单时按下e键    2.找到linux16行, ...

  7. crontab定时任务_net

    2017年2月25日, 星期六 crontab定时任务 19. crontab 定时任务 通过crontab 命令,我们可以在固定的间隔时间执行指定的系统指令或 shell script脚本.时间间隔 ...

  8. Java并发编程原理与实战二十六:闭锁 CountDownLatch

    关于闭锁 CountDownLatch 之前在网上看到过一篇举例非常形象的例子,但不记得是出自哪里了,所以这里就当自己再重新写一篇吧: 例子如下: 我们每天起早贪黑的上班,父母每天也要上班,有一天定了 ...

  9. Python练习-跨目录调用模块

    层级结构: dir1 ---hello.py dir2 ---main.py 其中,hello.py: def add(x,y): return x+y main.py如何能调用到hello.py中的 ...

  10. Python练习-面向过程编程-模拟Grep命令

    其实这个面向过程编写程序,是编写程序的基础,所以一定要好好掌握 此程序涉及知识点:装饰器,生成器,协程器应用 # 编辑者:闫龙 import os Distinct = [] #定义一个列表用于判断重 ...