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 ...
随机推荐
- Reloading Java Classes 301: Classloaders in Web Development — Tomcat, GlassFish, OSGi, Tapestry 5 and so on Translation
The Original link : http://zeroturnaround.com/rebellabs/rjc301/ Copyright reserved by Rebel Inc In t ...
- 从头认识Spring-2.1 自己主动装配(2)-byType(2)
为了解决配置文件中面出现多个同类型的Bean而byType无法匹配的问题.引入了primary和autowire-candidate属性. 1.primary 因为全部bean默认的primary都是 ...
- [2014亚马逊amazon] 在线笔试题 大于非负整数N的第一个回文数 Symmetric Number
1.题目 如标题,求大于整数N(N>=0)的第一个回文数的字符串表示形式. 这个题目也是当时笔试第一次见到,花了一个小时才做出了.慢慢总结还是挺简单的. 2.分析 分析如下: (1)一位数N(9 ...
- caffe添加python数据层
caffe添加python数据层(ImageData) 在caffe中添加自定义层时,必须要实现这四个函数,在C++中是(LayerSetUp,Reshape,Forward_cpu,Backward ...
- Word Ladder leetcode java
题目: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...
- Count and Say leetcode java
题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11 ...
- C++_友元函数总结(转)
原文地址:http://blog.csdn.net/insistgogo/article/details/6608672 1.为什么要引入友元函数:在实现类之间数据共享时,减少系统开销,提高效率 ...
- ML、DL相关资源
1. http://x-algo.cn/index.php/category/nlp/
- easyui权限系统改造备忘
修改list.js文件 //// 如果所有操作按钮隐藏,则list-toolbar隐藏 //if ($(".list-toolbar").length > 0) { // v ...
- Mac OS X /home 目录权限修改
Mac OS X /home 目录权限修改 http://ju.outofmemory.cn/entry/283070 sudo vi /etc/auto_master # 注释掉 /home那一行 ...