maven

 <properties>
<jedis.version>3.0.1</jedis.version>
<junit.verion>4.12</junit.verion>
<log4j.verison>2.12.1</log4j.verison>
<slf4j.version>1.7.28</slf4j.version>
<!--<commons-pool2.version>2.6.2</commons-pool2.version>-->
</properties> <dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.verion}</version>
<scope>compile</scope>
</dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.verison}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>

redis.properties

 #最大闲置数
redis.maxIdle=30
#最小闲置数
#redis.minIdle=10
#最大连接数
redis.maxTotal=100
#连接安装redis服务器的IP地址
redis.host=192.168.101.101
#redis的默认端口
redis.port=6379

JedisUtil

 public class JedisUtil {
private static Properties properties = new Properties();
private static JedisPoolConfig config = new JedisPoolConfig();
private static JedisPool pool; private JedisUtil() { } static {
InputStream inputStream = JedisUtil.class.getClassLoader().getResourceAsStream("redis.properties");
try {
properties.load(inputStream);
config.setMaxTotal(Integer.parseInt(properties.getProperty("redis.maxTotal")));
config.setMaxIdle(Integer.parseInt(properties.getProperty("redis.maxIdle")));
pool = new JedisPool(config, properties.getProperty("redis.host"), Integer.parseInt(properties.getProperty("redis.port")));
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
} } public static Jedis getResource() {
return pool.getResource();
} public static void close(Jedis jedis) {
if (jedis != null) {
jedis.close();
}
} public static void shutdown() {
if (pool != null) {
if (pool.getNumActive() == 0) {
pool.close();
}
}
}
}

Test

         Jedis resource = JedisUtil.getResource();
System.out.println( resource.get("name"));
JedisUtil.close(resource);
JedisUtil.shutdown();

jedis 连接池工具类的更多相关文章

  1. Druid连接池工具类

    package cn.zmh.PingCe; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSo ...

  2. C3P0连接池工具类实现步骤及方法

    C3P0连接池的工具类 使用C3P0获得连接对象连接池有一个规范接口 javax.sal.DataSourse 接口定义了一个从连接池中获得连接的方法getConnection(); 步骤导入jar包 ...

  3. C3P0连接池工具类使用

    c3p0的基本连接配置文件 c3p0-config.xml <c3p0-config> <default-config> <property name="dri ...

  4. 004-C3P0连接池工具类模板

    package ${enclosing_package}; import java.sql.Connection; import java.sql.ResultSet; import java.sql ...

  5. Oracle连接池工具类OJDBCUtils

    Oraclejdbc.properties driverClassName=oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@127.0.0. ...

  6. DBCP连接池工具类模板

    package ${enclosing_package}; import java.io.InputStream; import java.sql.Connection; import java.sq ...

  7. Jedis连接池

    jedis是官方首选的java客户端开发包 Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如java.C.C#.C++.php.Node.js.Go等. 在官方网站里列一些Ja ...

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

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

  9. 为什么要用Jedis连接池+浅谈jedis连接池使用

    为什么要使用Jedis连接池 Redis作为缓存数据库理论上和MySQL一样需要客户端和服务端建立起来连接进行相关操作,使用MySQL的时候相信大家都会使用一款开源的连接池,例如C3P0.因为直连会消 ...

随机推荐

  1. 小爬爬4:selenium操作

    1.selenium是什么? selenium: - 概念:是一个基于浏览器自动化的模块. - 和爬虫之间的关联? - 帮我我们便捷的爬取到页面中动态加载出来的数据 - 实现模拟登陆 - 基本使用流程 ...

  2. APP UI设计趋势:为好设计而动

    http://www.cocoachina.com/design/20150703/12029.html 作者:bone9 善心悦目的动效已然成为一个app的必备,作为设计师自然要跟随趋势学习.APP ...

  3. 64位Linux编译C代码,crt1.o文件格式不对的问题

    今天在某台64位LInux下编译一个简单的hello world的C程序,报错: /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../crt1.o: cou ...

  4. @codeforces - 1153F@ Serval and Bonus Problem

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 从一条长度为 l 的线段中随机选择 n 条线段,共 2*n 个线 ...

  5. oracle函数 exp(y)

    [功能]返回e的y次幂(e为数学常量) [参数]y,数字型表达式 [返回]数字 [示例] select exp(3),exp(0),exp(-3) from dual; 返回:20.0855369,1 ...

  6. tinyhttpd简介

    一:简介: tinyhttpd是由J. DavidBlackstone在1999年编写的,实现了一个很简单的web服务器.支持GET和POST方法,总代码量也就在500行左右,可以用来学习HTTP协议 ...

  7. windows环境下安装nodeJS和express,一直提示command not found-配置环境变量

    1.安装NodeJS后,使用npm指令安装express框架,使用 npm install -g express npm install -g express-generator 安装了大半天的时间, ...

  8. ifram子页面与父页面的方法相互调用

    parent.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  9. 原生js用div实现简单的轮播图

    文章地址 https://www.cnblogs.com/sandraryan/ 原生js实现轮播图. 打开页面图片自动轮播,点击prev next按钮切换到上/下一张图片,点击1-5切换到对应图片. ...

  10. 深入理解String、StringBuffer、StringBuilder(转)

    文章系转载,非原创,原地址: http://www.cnblogs.com/dolphin0520/p/3778589.html 相信String这个类是Java中使用得最频繁的类之一,并且又是各大公 ...