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 :强制创建新的链接资源

二.程序实例

  1. public class PoolTestMain {  
  2.     public static void main(String[] args) {  
  3.         JedisPoolConfig config = new JedisPoolConfig();  
  4.         config.setMaxActive(32);  
  5.         config.setMaxIdle(6);  
  6.         config.setMinIdle(0);  
  7.         config.setMaxWait(15000);  
  8.         config.setMinEvictableIdleTimeMillis(300000);  
  9.         config.setSoftMinEvictableIdleTimeMillis(-1);  
  10.         config.setNumTestsPerEvictionRun(3);  
  11.         config.setTestOnBorrow(false);  
  12.         config.setTestOnReturn(false);  
  13.         config.setTestWhileIdle(false);  
  14.         config.setTimeBetweenEvictionRunsMillis(60000);//一分钟  
  15.         config.setWhenExhaustedAction((byte)1);  
  16.         JedisPool pool = new JedisPool(config,"127.0.0.1",6379,15000,"0123456",12);  
  17.         Jedis client = pool.getResource();    //从pool中获取资源  
  18.         try{  
  19.             client.select(0);  
  20.             client.set("k1", "v1");  
  21.             System.out.println(client.get("k1"));  
  22.         }catch(Exception e){  
  23.             e.printStackTrace();  
  24.         }finally{  
  25.             pool.returnResource(client);    //向连接池“归还”资源,千万不要忘记。  
  26.         }  
  27.     }  
  28. }  
 

三.Spring与Jedis连接池

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

  1. <beans xmlns="http://www.springframework.org/schema/beans"   
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName">  
  4.   
  5.     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
  6.         <property name="maxActive" value="32"></property>  
  7.         <property name="maxIdle" value="6"></property>  
  8.         <property name="maxWait" value="15000"></property>  
  9.         <property name="minEvictableIdleTimeMillis" value="300000"></property>  
  10.         <property name="numTestsPerEvictionRun" value="3"></property>  
  11.         <property name="timeBetweenEvictionRunsMillis" value="60000"></property>  
  12.         <property name="whenExhaustedAction" value="1"></property>  
  13.     </bean>  
  14.     <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy">  
  15.         <!-- config -->  
  16.         <constructor-arg ref="jedisPoolConfig"></constructor-arg>  
  17.         <!-- host -->  
  18.         <constructor-arg value="127.0.0.1"></constructor-arg>  
  19.         <!-- port -->  
  20.         <constructor-arg value="6379"></constructor-arg>  
  21.         <!-- timeout -->  
  22.         <constructor-arg value="15000"></constructor-arg>  
  23.         <!-- password -->  
  24.         <constructor-arg value="0123456"></constructor-arg>  
  25.         <!-- database index -->  
  26.         <constructor-arg value="12"></constructor-arg>        
  27.     </bean>  
  28. </beans>  

<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环境中,只需要“注入”即可.

  1. public static void main(String[] args) {  
  2.     //resources/beans.xml
  3.     ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");  
  4.     JedisPool jedisPool = (JedisPool)context.getBean("jedisPool");  
  5.     Jedis client = jedisPool.getResource();  
  6.     try{  
  7.         client.select(0);  
  8.         client.set("k1", "v1");  
  9.         System.out.println(client.get("k1"));  
  10.     }catch(Exception e){  
  11.         e.printStackTrace();  
  12.     }finally{  
  13.         jedisPool.returnResource(client);//must be  
  14.     }  
  15. }  

Jedis编程设计:连接池的更多相关文章

  1. Jedis分片Sentinel连接池实验

    Jedis分片Sentinel连接池实验 1.起因 众所周知,Redis官方HA工具Sentinel已经问世很久了,但令人费解的是,Jedis官方却迟迟没有更新它的连接池.到目前Maven库中最新的2 ...

  2. 简单是Jedis实例(相对连接池而言)

    在引入相关jar包后,只要new一个Jedis对象,就能做redis相关操作了.以下是一个简单的jedis实例: package com.pptv.redis; import java.util.Ar ...

  3. Jedis连接池对Redis数据操作

    [效果图] [前言] Redis是常用于缓存的非关系型数据库,感觉更像加强型的HashMap的用法,依靠Key和Value保存数据.官方推荐用Jedis来操作Redis数据库,使用和JDBC差不多,一 ...

  4. Java与redis交互、Jedis连接池JedisPool

    Java与redis交互比较常用的是Jedis. 先导入jar包: commons-pool2-2.3.jar jedis-2.7.0.jar 基本使用: public class RedisTest ...

  5. jedis的连接池

    1.需要先打开虚拟机,并开启Linux系统的端口号:6379: 其中,第一行代码为修改字符编码格式,解决SSH中文乱码问题. 2.开启redis: 3.利用连接池实现数据的存取: (1)代码实现: i ...

  6. Linux编程之内存池的设计与实现(C++98)

    假设服务器的硬件资源"充裕",那么提高服务器性能的一个很直接的方法就是空间换时间,即"浪费"服务器的硬件资源,以换取其运行效率.提升服务器性能的一个重要方法就是 ...

  7. jedis连接池详解(Redis)

    转自:http://tianxingzhe.blog.51cto.com/3390077/1684306 原子性(atomicity): 一个事务是一个不可分割的最小工作单位,事务中包括的诸操作要么都 ...

  8. Jedis连接池使用

    构建redis连接池,返还到连接池 private static JedisPool jedisPool = null; private static Jedis jedis; static { je ...

  9. java客户端Jedis操作Redis Sentinel 连接池

    pom配置: <dependency> <groupId>org.springframework.data</groupId> <artifactId> ...

随机推荐

  1. subprocess module

    subprocess 主要用于执行外部命令和程序, 极大的增强了Python的功能. 比如你要用bowtie, 你可以在python中调用这个程序. 运行python时,我们都是在创建并运行一个进程, ...

  2. 2015GitWebRTC编译实录4

    2015.07.17 libg711 编译通过[422/1600 ] CC obj /webrtc/modules/audio_coding/codecs/g711/g711.g711.o[423/1 ...

  3. Selenium WebDriver对cookie进行处理绕过登录验证码

    现在几乎所有登录页面都会带一个验证码,做起自动化这块比较麻烦, 所以要绕过网站的验证码. 首先需要手动登录一次你的测试网站,去chrome的F12里获取这个网站的cookie信息,找到对应的保存登录信 ...

  4. mysql最大连接数

    通常,mysql的最大连接数默认是100, 最大可以达到16384.1.查看最大连接数:show variables like '%max_connections%';2.修改最大连接数方法一:修改配 ...

  5. Jlink V7在MDK下使用Cortex-M3-Jlink模式开发STM32的说明

    Jlink V7在MDK下使用Cortex-M3-Jlink模式开发STM32的说明   开发环境:STM32F103RB(128K Flash 20K RAM)+MDK3.50+JLINK V7(v ...

  6. leetcode 113 Path Sum II ----- java

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  7. IOS请求H5页面、要求自定义agent判断是电脑、安卓还是iPhone登录

    //自定制的userAgent- (void)createMyAgent{        NSString *userAgent = [[[UIWebView alloc]init]stringByE ...

  8. px em rem在WEB前端开发中的区别

    我们都知道基于像素的字体大小所用的单位是px,但是随着响应式设计的不断火热,基于相对字体大小的单位em变开始流行起来.当然,rem也在Web前端开发人员讨论如何更好设置字体大小的讨论话题之列.是不是需 ...

  9. click 绑定(一)无参数的click 事件绑定

    目的 click绑定在DOM元素上添加事件句柄以便元素被点击的时候执行定义的JavaScript 函数.大部分是用在button,input和连接a上,但是可以在任意元素上使用.   例子 <d ...

  10. (转) Deep Learning in a Nutshell: Core Concepts

    Deep Learning in a Nutshell: Core Concepts Share:   Posted on November 3, 2015by Tim Dettmers 7 Comm ...