获取连接池,通常连接池为单例,这里使用 双端检测机制保证只有一个实例

public class JedisPoolUtil {

    private static volatile JedisPool jedisPool = null;

    private JedisPoolUtil() {
} public static JedisPool getJedisPoolInstance() {
if (null == jedisPool) {
synchronized (JedisPoolUtil.class) { //这里使用双端检测设计模式
if (null == jedisPool) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
//控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;如果赋值为-1,则表示不限制,
//如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted
poolConfig.setMaxActive(1000);
poolConfig.setMaxIdle(32); //设置剩余连接各数,如果小于这个就会抛异常
//表示当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛JedisConnectionException
poolConfig.setMaxWait(100*1000);
//获得一个jedis实例的时候是否检查连接可用性(ping()),如果为true,则得到的jedis实例均是可用的
poolConfig.setTestOnBorrow(true);
jedisPool = new JedisPool(poolConfig, "192.168.1.201", 6379);
}
}
}
return jedisPool;
}
/**
* 释放
* @param jedisPool 释放哪个池中
* @param jedis 的哪个对象
*/
public static void release(JedisPool jedisPool,Jedis jedis){
if(null != jedis){
jedisPool.returnResourceObject(jedis);
}
} }

测试代码:

public class TestJedisPool {

    public static void main(String[] args) {
JedisPool jedisPool = JedisPoolUtil.getJedisPoolInstance(); Jedis jedis = null; try {
jedis = jedisPool.getResource(); jedis.set("aa", "bb");
} catch (Exception e) {
e.printStackTrace();
} finally {
JedisPoolUtil.release(jedisPool, jedis);
}
} }

127.0.0.1:6379> get aa
"bb"
127.0.0.1:6379>

JedisPoolCinfig

Redis JedisPool的更多相关文章

  1. redis linux 安装及jedis连接测试

    一.安装配置 1:下载redis下载地址 http://code.google.com/p/redis/downloads/list推荐下载redis-1.2.6.tar.gz,之前这个版本同事已经有 ...

  2. jedisPool.returnBrokenResource 弃用

    for (int i = 0; i < 1000000 ; i++) { //使用Pool的方式 调用Redis JedisPool jedisPool = SpringContextHolde ...

  3. redis工具

    pom.xml添加 <!--jedis redis客户端--> <dependency> <groupId>redis.clients</groupId> ...

  4. 七、springboot(四)配置redis

    1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  5. 使用Redis实现锁(支持分布式应用)(整理网络资料)

    使用Redis实现锁(支持分布式应用) 1.    简介 使用Redis指令setnx.expire.getset等操作实现互斥资源的访问 本文内容来着网络整理,参考: http://www.linu ...

  6. Spring boot 基于Spring MVC的Web应用和REST服务开发

    Spring Boot利用JavaConfig配置模式以及"约定优于配置"理念,极大简化了基于Spring MVC的Web应用和REST服务开发. Servlet: package ...

  7. Spring Boot 示例项目

    Spring Boot 基于注解式开发 maven REST 示例项目    项目地址:https://github.com/windwant/spring-boot-service    项目地址: ...

  8. Java秒杀系统方案优化 高性能高并发实战(已完成)

    1:商品列表 2:商品详情判断是否可以开始秒杀,未开始不显示秒杀按钮显示倒计时,开始显示秒杀按钮,同时会显示验证码输入框以及验证码图片,当点击秒杀按钮的时候会首先判断验证码是否正确,如果正确会返回一个 ...

  9. spring boot redis缓存JedisPool使用

    spring boot redis缓存JedisPool使用 添加依赖pom.xml中添加如下依赖 <!-- Spring Boot Redis --> <dependency> ...

随机推荐

  1. 2-C程序结构

    一.代码分析 打开项目中的main.c文件(C程序的源文件拓展名为.c),可以发现它是第一个C程序中的唯一一个源文件,代码如下: #include <stdio.h> #include & ...

  2. 敏捷开发与jira

    项目背景 项目是基于一套公司自主研发的平台做企业信息化的项目管理业务,经过两个里程碑的交付,已经在客户现场使用,每次版本都能按期交付,延迟较少,客户满意度也高. 项目开发过程采用的敏捷的方法,用类Sc ...

  3. Android layout_weight理解

    layout_weight属性只能用于LinearLayout布局,不能用于RelativeLayout等其他布局: layout_weight属性如果不设定,默认值为0: layout_weight ...

  4. [翻译]——SQL Server使用链接服务器的5个性能杀手

    前言: 本文是对博客http://www.dbnewsfeed.com/2012/09/08/5-performance-killers-when-working-with-linked-server ...

  5. sql优化阶段性总结以及反思

    Sql优化思路阶段性心得: 这段时间的优化做了好几个案例,其实有很多的类似点,都是好几张大表的相互连接,然后执行长达好几个小时,甚至都跑不出来. 自己差不多的思路就是Parallel full tab ...

  6. Flume组件source,channel,sink源码分析

    LifeCycleState: IDLE, START, STOP, ERROR [Source]: org.apache.flume.Source 继承LifeCycleAware{stop() + ...

  7. 从零自学Hadoop(13):Hadoop命令下

    阅读目录 序 MapReduce Commands User Commands Administration Commands YARN Commands User Commands Administ ...

  8. 基于Ajax+div的“左边菜单、右边内容”页面效果实现

    效果演示: ①默认页面(index.jsp): ②:点击左侧 用户管理 标签下的 用户列表 选项后,右边默认页面内容更新为用户列表页(userList.jsp)的内容 : ③:同理,点击 产品管理.订 ...

  9. 分布式搜索引擎Elasticsearch的查询与过滤

    一.写入 先来一个简单的官方例子,插入的参数为-XPUT,插入一条记录. curl -XPUT 'http://localhost:9200/test/users/1' -d '{ "use ...

  10. HTML入门篇

    HTML HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏 ...