jedis 连接池工具类
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 连接池工具类的更多相关文章
- Druid连接池工具类
package cn.zmh.PingCe; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSo ...
- C3P0连接池工具类实现步骤及方法
C3P0连接池的工具类 使用C3P0获得连接对象连接池有一个规范接口 javax.sal.DataSourse 接口定义了一个从连接池中获得连接的方法getConnection(); 步骤导入jar包 ...
- C3P0连接池工具类使用
c3p0的基本连接配置文件 c3p0-config.xml <c3p0-config> <default-config> <property name="dri ...
- 004-C3P0连接池工具类模板
package ${enclosing_package}; import java.sql.Connection; import java.sql.ResultSet; import java.sql ...
- Oracle连接池工具类OJDBCUtils
Oraclejdbc.properties driverClassName=oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@127.0.0. ...
- DBCP连接池工具类模板
package ${enclosing_package}; import java.io.InputStream; import java.sql.Connection; import java.sq ...
- Jedis连接池
jedis是官方首选的java客户端开发包 Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如java.C.C#.C++.php.Node.js.Go等. 在官方网站里列一些Ja ...
- Java与redis交互、Jedis连接池JedisPool
Java与redis交互比较常用的是Jedis. 先导入jar包: commons-pool2-2.3.jar jedis-2.7.0.jar 基本使用: public class RedisTest ...
- 为什么要用Jedis连接池+浅谈jedis连接池使用
为什么要使用Jedis连接池 Redis作为缓存数据库理论上和MySQL一样需要客户端和服务端建立起来连接进行相关操作,使用MySQL的时候相信大家都会使用一款开源的连接池,例如C3P0.因为直连会消 ...
随机推荐
- 散列表(Hash Table)
散列表(hash table): 也称为哈希表. 根据wikipedia的定义:是根据关键字(Key value)而直接访问在内存存储位置的数据结构.也就是说,它通过把键值通过一个函数的计算,映射到表 ...
- 2017校赛 C: 不爱学习的小W【模拟】
题目描述 “叮铃铃”上课了,同学们都及时到了教室坐到了座位上,教室里有n行m列的座位而且刚好坐满.既然是上课,那老师叫学生回答问题就是再正常不过的事了,同样地,教室里也就有爱学习和不爱学习的学生了,爱 ...
- springboot web开发【转】【补】
pom.xml引入webjars的官网 https://www.webjars.org/ https://www.thymeleaf.org/doc/tutorials/3.0/usingthymel ...
- framework7日期插件使用
1.引入框架文件 <link rel="stylesheet" href="framework7.ios.min.css"> <link re ...
- oralce使用INSERT语句向表中插入数据
INSERT INTO table[ (column [, column. . .])] VALUES (value [,value . . .]); v 插入的数据 ...
- UIView 判断是否visible
if(self.view == [(MyAppDelegate *)[[UIApplication sharedApplication] delegate].window.subviews objec ...
- 微信小程序入门到实战(1)-基础知识
1.微信小程序介绍 微信小程序,简称小程序,英文名Mini Program,是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或搜一下即可打开应用. 1.1. 为什么是微信 ...
- 关于IOS 微信浏览器 点击输入框自动放大问题
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0& ...
- 远程监控JVM
设置tomcat中catalina.sh设置JAVA_OPTS= JAVA_OPTS="-server -Xms595M -Xmx595M -Xmn223M -XX:SurvivorRati ...
- 第三期 行为规划——11.在C ++中实现第二个成本函数
在大多数情况下,单一成本函数不足以产生复杂的车辆行为.在这个测验中,我们希望您在C ++中实现更多的成本函数.我们稍后会在本课中使用这两个C ++成本函数.这个测验的目标是创建一个成本函数,使车辆在最 ...