4、redis之使用commons-pool
增加池的配置文件redis-pool.properties:
#最大能够保持idel状态的对象数
redis.pool.maxIdle=200
#当池内没有返回对象时,最大等待时间
redis.pool.maxWait=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
#当调用return Object方法时,是否进行有效性检查
redis.pool.testOnReturn=true
#IP
redis.ip=127.0.0.1
#Port
redis.port=6379
RedisApp.java
package com.yzl; import java.util.ResourceBundle; import org.apache.log4j.Logger; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; /**
* Reids之jedis的CRUD操作
*
* @author yangzhilong
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
public class RedisApp {
public Logger log = Logger.getLogger(this.getClass()); private static JedisPool pool;
private static ResourceBundle bundle; static{
bundle = ResourceBundle.getBundle("redis-pool");
if(bundle == null){
//假设直接抛出Exception咋必须进行处理,不然编译不会通过
throw new IllegalArgumentException("redis-pool.properties file is not found");
}
JedisPoolConfig config = new JedisPoolConfig();
//设置pool的一些参数,可选,详细配置项参见GenericObjectPoolConfig类
config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait")));
config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));
config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn"))); //使用默认配置时可以使用如下方法初始化池
//pool = new JedisPool(bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")));
pool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")));
} /**
*
* 功能描述: <br>
* CRUD操作之hello world
*
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public void crudFromRedisWidthSimple(){
Jedis jedis = new Jedis(bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")));
log.info("get connection with simple ");
crudMethod(jedis);
//关闭连接
jedis.close();
} /**
*
* 使用common-pool操作redis
*
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public void crudFromRedisWidthPool(){
Jedis jedis = pool.getResource();
log.info("get connection from pool , obect is:" + jedis);
crudMethod(jedis);
//释放链接
pool.returnResourceObject(jedis);
} /**
*
* crud基本操作单元
*
* @param jedis
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
private void crudMethod(Jedis jedis){
log.info("insert value to redis~~~");
jedis.set("name", "hello jedis"); log.info("get value from redis, value:" + jedis.get("name")); log.info("delete key from redis~~~");
jedis.del("name"); log.info("get value from redis, value:" + jedis.get("name"));
}
}
RedisAppTest.java
package com.yzl; import org.junit.Test; /**
* RedisApp的测试类
*
* @author yangzhilong
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
public class RedisAppTest { @Test
public void crudFromRedisWidthSimpleTest(){
RedisApp app = new RedisApp();
app.crudFromRedisWidthSimple();
} @Test
public void crudFromRedisWidthPoolTest(){
RedisApp app = new RedisApp();
app.crudFromRedisWidthPool();
}
}
运行单元测试的结果:
2015-08-12 16:03:24,449 [com.yzl.RedisApp]-[INFO] get connection from pool , obect is:redis.clients.jedis.Jedis@cee47f1
2015-08-12 16:03:24,449 [com.yzl.RedisApp]-[INFO] insert value to redis~~~
2015-08-12 16:03:24,449 [com.yzl.RedisApp]-[INFO] get value from redis, value:hello jedis
2015-08-12 16:03:24,449 [com.yzl.RedisApp]-[INFO] delete key from redis~~~
2015-08-12 16:03:24,449 [com.yzl.RedisApp]-[INFO] get value from redis, value:null
下一篇:5、redis之使用spring集成commons-pool
4、redis之使用commons-pool的更多相关文章
- Apache Commons Pool 故事一则
Apache Commons Pool 故事一则 最近工作中遇到一个由于对commons-pool的使用不当而引发的问题,习得正确的使用姿势后,写下这个简单的故事,帮助理解Apache Commons ...
- 池化 - Apache Commons Pool
对于那些创建耗时较长,或者资源占用较多的对象,比如网络连接,线程之类的资源,通常使用池化来管理这些对象,从而达到提高性能的目的.比如数据库连接池(c3p0, dbcp), java的线程池 Execu ...
- JedisCluster中应用的Apache Commons Pool对象池技术
对象池技术在服务器开发上应用广泛.在各种对象池的实现中,尤其以数据库的连接池最为明显,可以说是每个服务器必须实现的部分. apache common pool 官方文档可以参考:https://c ...
- Apache Commons Pool 故事一则 专题
Apache Commons Pool 故事一则 最近工作中遇到一个由于对commons-pool的使用不当而引发的问题,习得正确的使用姿势后,写下这个简单的故事,帮助理解Apache Commons ...
- Tomcat 开发web项目报Illegal access: this web application instance has been stopped already. Could not load [org.apache.commons.pool.impl.CursorableLinkedList$Cursor]. 错误
开发Java web项目,在tomcat运行后报如下错误: Illegal access: this web application instance has been stopped already ...
- NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
错误:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/pool/impl ...
- Spring + Tomcat 启动报错java.lang.ClassNotFoundException: org.apache.commons.pool.impl.GenericObjectPool
错误如下: -- ::,-[TS] INFO http-- org.springframework.beans.factory.support.DefaultListableBeanFactory - ...
- org/apache/commons/pool/impl/GenericObjectPool异常的解决办法
org/apache/commons/pool/impl/GenericObjectPool异常的解决办法 webwork+spring+hibernate框架的集成, 一启动Tomcat服务器就出了 ...
- 对象池化技术 org.apache.commons.pool
恰当地使用对象池化技术,可以有效地减少对象生成和初始化时的消耗,提高系统的运行效率.Jakarta Commons Pool组件提供了一整套用于实现对象池化的框架,以及若干种各具特色的对象池实现,可以 ...
- Cache Lucene IndexReader with Apache Commons Pool
IndexReaderFactory.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2 ...
随机推荐
- Just-In-Time Debugging in Visual Studio 禁止VS在服务器上调试
To disable Just-In-Time debugging by editing the registry On the Start menu, search for and run rege ...
- 不知道哪里alert undefined 用下面的语句是js报错.F12能提示报错的地方window.alert=function(aa){ if (typeof (aa)"undefined"){ throw "就是这";}};
不知道哪里alert undefined 用下面的语句是js报错.F12能提示报错的地方 var oldalert=window.alert; window.alert=function(aa){ i ...
- JavaScript:Array 对象
ylbtech-JavaScript:Array 对象 1. 返回顶部 Array 对象 Array 对象用于在单个的变量中存储多个值. 创建 Array 对象的语法: new Array(); ne ...
- 奇怪吸引子---DequanLi
奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...
- 怎么选择软件许可证,Apache, MIT, BSD, GPL, Mozilla, LGPL
- FastReport.Net 入门
任何一门编程技术入门体验都是以“Hello World”开始的,但我想再复杂一点的“Hello World”,才能算真正的入门. FastReport.Net V1.2.76 ,vs2008 在 ...
- NodeJS错误-throw er; // Unhandled 'error' event
第一眼看以为Express版本出现问题,因为本地已经存在另外一个运行的Node项目,端口重复,修改一下端口号即可,错误提示如下: events.js:85 throw er; // Unhandled ...
- 汉字转拼音 pinyin4j 字符串 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 不用IDE写C#的Hello World
用Visual Studio等IDE写C#的Hello World非常简单,但脱离了IDE你能不能打印出Hello World呢?这不是说工作时脱离IDE,而是学习一下CLR的执行模型. Hello ...
- Kalman滤波器从原理到实现
Kalman滤波器的历史渊源 We are like dwarfs on the shoulders of giants, by whose grace we see farther than the ...