Jedis作为redis的最佳客户端,它提供了连接池的特性,“连接池”在通常情况下可以有效的提高应用的通信能力,并且这是一种良好的设计模式。Jedis的连接池设计基于apache commons-pool原生库,仅作了简单的封装;在本文中,我将介绍如何使用jedisPool进行程序设计。

一.连接池基本参数详解

    1. maxActive: 链接池中最大连接数,默认为8.
    2. maxIdle: 链接池中最大空闲的连接数,默认为8.
    3. minIdle: 连接池中最少空闲的连接数,默认为0.
    4. maxWait: 当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时.
    5. minEvictableIdleTimeMillis: 连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除。
    6. softMinEvictableIdleTimeMillis: 连接空闲的最小时间,达到此值后空闲链接将会被移除,且保留“minIdle”个空闲连接数。默认为-1.
    7. numTestsPerEvictionRun: 对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3.
    8. testOnBorrow: 向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值.
    9. testOnReturn:  向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值.
    10. testWhileIdle:  向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值.
    11. timeBetweenEvictionRunsMillis:  “空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1.
    12. whenExhaustedAction: 当“连接池”中active数量达到阀值时,即“链接”资源耗尽时,连接池需要采取的手段, 默认为1:
       -> 0 : 抛出异常,
       -> 1 : 阻塞,直到有可用链接资源
       -> 2 : 强制创建新的链接资源

二.程序实例

public class PoolTestMain {
public static void main(String[] args) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(32);
config.setMaxIdle(6);
config.setMinIdle(0);
config.setMaxWait(15000);
config.setMinEvictableIdleTimeMillis(300000);
config.setSoftMinEvictableIdleTimeMillis(-1);
config.setNumTestsPerEvictionRun(3);
config.setTestOnBorrow(false);
config.setTestOnReturn(false);
config.setTestWhileIdle(false);
config.setTimeBetweenEvictionRunsMillis(60000);//一分钟
config.setWhenExhaustedAction((byte)1);
JedisPool pool = new JedisPool(config,"127.0.0.1",6379,15000,"0123456",12);
Jedis client = pool.getResource();//从pool中获取资源
try{
client.select(0);
client.set("k1", "v1");
System.out.println(client.get("k1"));
}catch(Exception e){
e.printStackTrace();
}finally{
pool.returnResource(client);//向连接池“归还”资源,千万不要忘记。
} } }

三.Spring与Jedis连接池

    1) beans.xml: 位于/resources/beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName"> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxActive" value="32"></property>
<property name="maxIdle" value="6"></property>
<property name="maxWait" value="15000"></property>
<property name="minEvictableIdleTimeMillis" value="300000"></property>
<property name="numTestsPerEvictionRun" value="3"></property>
<property name="timeBetweenEvictionRunsMillis" value="60000"></property>
<property name="whenExhaustedAction" value="1"></property>
</bean>
<bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy">
<!-- config -->
<constructor-arg ref="jedisPoolConfig"></constructor-arg>
<!-- host -->
<constructor-arg value="127.0.0.1"></constructor-arg>
<!-- port -->
<constructor-arg value="6379"></constructor-arg>
<!-- timeout -->
<constructor-arg value="15000"></constructor-arg>
<!-- password -->
<constructor-arg value="0123456"></constructor-arg>
<!-- database index -->
<constructor-arg value="12"></constructor-arg>
</bean>
</beans>

2) 测试类:不过在实际的spring环境中,只需要“注入”即可.

public static void main(String[] args) {
//resources/beans.xml
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");
JedisPool jedisPool = (JedisPool)context.getBean("jedisPool");
Jedis client = jedisPool.getResource();
try{
client.select(0);
client.set("k1", "v1");
System.out.println(client.get("k1"));
}catch(Exception e){
e.printStackTrace();
}finally{
jedisPool.returnResource(client);//must be
} }

jedis 链接池使用(转)的更多相关文章

  1. jedis的ShardedJedisPool链接池的扩容问题

    回顾上一篇文章jedis连接池的构建. 我们来分析ShardedJedisPool的基于客户端分片所可能带来的问题:扩容 ShardedJedisPool的节点扩容 .ShardedJedisPool ...

  2. JedisPool异常Jedis链接处理

    问题现象(jedis-2.1.0.jar) 基于JedisPool管理Jedis对象,通过get方法获取值,出现key对应的value值错误,例如: K V a a b b Jedis.get(“a” ...

  3. 解决Jedis链接报超时异常和connection reset异常的方法

    一.链接池配置 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" ...

  4. 详解Jedis连接池报错处理

    在使用Jedis连接池模式下,比较常见的报错如下: redis.clients.jedis.exceptions.JedisConnectionException:Could not get a re ...

  5. c3p0链接池

    频繁的链接数据库是非常消耗性能的,所以就采用了将一定量的链接保存在一个池中,这个池我们叫做链接池. 详细请看:http://baike.baidu.com/link?url=dlTW-fTS3N_-j ...

  6. ServiceStack.Redis 使用链接池方法

    PooledRedisClientManager 1.RedisManage.cs public static class RedisManager { private static PooledRe ...

  7. 自定义JDBC链接池

    上篇简单介绍了jdbc链接数据库: 本篇就说一下自定义连接池以及增删改查的测试: 自定义连接池 自定义链接池的原因 JDBC连接中用到Connection   在每次对数据进行增删查改 都要 开启  ...

  8. 数据库链接池c3p0的配置

    由于我看的是远古教程,所以里面各种驱动jar包还有c3p0包都是远古版本,对于最新版本的jdbc已经失去的作用,所以我在这里重写一下! 1.首先是c3p0的位置,package的外面,src的里面 2 ...

  9. c3p0链接池配置使用

    c3p0链接池初步使用:直接上代码 c3p0是开源面粉的连接池,目前使用它的开源项目主要有:Spring,Hibernate等,使用时需要导入相关jar包及配置文件c3p0-config.xml文件 ...

随机推荐

  1. Simple2D-17(音乐播放器)嵌入 ImGui 库

    要把 ImGui 应用到项目中,先拷贝方框中的源文件到项目: 这些文件是 ImGui 的实现源码,可作为第三方库新建一个文件夹进行放置. 接下来是渲染部分的代码,项目可能使用 DirectX 或 Op ...

  2. 前端开发-2-HTML-head标签

    browser英 /'braʊzə/ 美 /'braʊzɚ/ 浏览器 explorer英 /ek'splɔːrə(r)/ 美 /ɪk'splɔrɚ/ 探险者,资源管理器 1.index 2.head标 ...

  3. ios App 文件夹

    转:http://stackoverflow.com/questions/16561779/nssearchpathfordirectoriesindomains-nsuserdomainmask D ...

  4. 修改rabbitmq Web UI 监控页面的端口

    在前几天工作中遇到一个问题,部署服务器,需要用rabbitmq自带的一个web UI监控组件,但是15672的端口没有对外映射.尝试了几种办法.开始修改rabbitmq.config,rabbitmq ...

  5. DateFormat工具类

    import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java. ...

  6. spring 整合 hibernate xml配置

    spring 整合 hibernate: hibernate :对数据库交互 spring: ioc   aop 整合点: 1.sessionFactory对象不再由hibernate生成,交由spr ...

  7. 岭回归和lasso回归(转)

    回归和分类是机器学习算法所要解决的两个主要问题.分类大家都知道,模型的输出值是离散值,对应着相应的类别,通常的简单分类问题模型输出值是二值的,也就是二分类问题.但是回归就稍微复杂一些,回归模型的输出值 ...

  8. 数据预处理之独热编码(One-Hot Encoding)(转载)

    问题由来 在很多机器学习任务中,特征并不总是连续值,而有可能是分类值. 例如,考虑一下的三个特征: ["male", "female"] ["from ...

  9. 疯狂JAVA——第五章 面向对象(上)

    5.1类和对象 构造器是一个类创建对象的根本途径,如果一个类没有构造器,这个类通常无法创建实例.通过new关键字来调用构造器,从而返回该类的实例. 类名:每个单词首字母大写,其他字母小写,单词之间不要 ...

  10. 根据条件决定My97DatePicker日期控件弹出的日期格式

    代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...