加入db选择后的redis连接池配置代码
public class redispoolconfigure {
//redis服务器ip
private string addr ;
//redis的端口号
private int port ;
//可用连接实例的最大数目
private int max_active ;
//pool中的idle jedis实例数
private int max_idle ;
//等待可用连接的最大时间,单位毫秒
private int max_wait ;
//超时时间,单位毫秒
private int time_out ;
//设置的逐出策略类名, 默认defaultevictionpolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
private string eviction_policy_class_name ;
//连接耗尽时是否阻塞, false报异常,ture阻塞直到超时
private boolean block_when_exhausted;
//是否启用pool的jmx管理功能, 默认true
private boolean jmx_enabled;
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
private boolean test_on_borrow ;
//服务器密码
private string redis_pass;
//redis选择数据库db
private int redis_db;
private string luasha;
private map<string, string> configure = null;
/**
* 根据配置文件,将redispool连接配置初始化
*/
public redispoolconfigure(){
try {
configure = new configurereader().readproperties("redis.properties");
} catch (ioexception e) {
e.printstacktrace();
}
this.addr = configure.get("redis.addr");
this.luasha = configure.get("redis.lua_hash");
this.eviction_policy_class_name = configure.get("redis.eviction_policy_class_name");
this.block_when_exhausted = boolean.parseboolean(configure.get("redis.block_when_exhausted"));
this.jmx_enabled = boolean.parseboolean(configure.get("redis.jmx_enabled"));
this.test_on_borrow = boolean.parseboolean(configure.get("redis.test_on_borrow"));
this.redis_pass=configure.get("redis.pass");
if(typecheck()){
this.port = new integer(configure.get("redis.port"));
this.max_active = new integer(configure.get("redis.max_active"));
this.max_idle = new integer(configure.get("redis.max_idle"));
this.max_wait = new integer(configure.get("redis.max_wait"));
this.redis_db=new integer(configure.get("redis.db"));
}else{
system.out.println("error");
}
}
/**
* 辅助工具,检查map中数据的类型
* @return
*/
private boolean typecheck() {
if (isnumeric(configure.get("redis.port"))
&& isnumeric(configure.get("redis.max_active"))
&& isnumeric(configure.get("redis.max_idle"))
&& isnumeric(configure.get("redis.max_wait"))
&& isnumeric(configure.get("redis.db"))) {
return true;
}
return false;
}
public string getaddr() {
return addr;
}
public int getport() {
return port;
}
public int getmax_active() {
return max_active;
}
public int getmax_idle() {
return max_idle;
}
public int getmax_wait() {
return max_wait;
}
public int gettime_out() {
return time_out;
}
public boolean istest_on_borrow() {
return test_on_borrow;
}
public string geteviction_policy_class_name() {
return eviction_policy_class_name;
}
public boolean isblock_when_exhausted() {
return block_when_exhausted;
}
public boolean isjmx_enabled() {
return jmx_enabled;
}
/**
* 判断传入的数据是否为纯数字构成
* @param str
* @return
*/
public boolean isnumeric(string str) {
if(str==null || "".equals(str)){
return false;
}
for (int i = 0; i < str.length(); i++) {
if (!character.isdigit(str.charat(i))) {
return false;
}
}
return true;
}
public string getluasha() {
return luasha;
}
public void setluasha(string luasha) {
luasha = luasha;
}
public string getredis_pass() {
return redis_pass;
}
public void setredis_pass(string redis_pass) {
redis_pass = redis_pass;
}
public int getredis_db() {
return redis_db;
}
public void setredis_db(int redis_db) {
redis_db = redis_db;
}
}
redis连接池初始化、获取jedis实例和释放jedis实例
/**
* jedis的连接池,返回未封装的jedis对象
* 一般只有在rediscache类提供的操作粒度不足使用时才使用此类提供的原生jedis方法
* @author hector
*
*/
public class redispool {
private static jedispool jedispool = null;
/**
* 初始化redis连接池
*/
static {
try {
redispoolconfigure configure = new redispoolconfigure();
jedispoolconfig config = new jedispoolconfig();
config.setblockwhenexhausted(configure.isblock_when_exhausted());
config.setevictionpolicyclassname(configure.geteviction_policy_class_name());
config.setjmxenabled(configure.isjmx_enabled());
config.setmaxidle(configure.getmax_idle());
config.setmaxtotal(configure.getmax_active());
config.setmaxwaitmillis(configure.getmax_wait());
config.settestonborrow(configure.istest_on_borrow());
jedispool = new jedispool(config, configure.getaddr(), configure.getport(), configure.gettime_out(),configure.getredis_pass(),configure.getredis_db());
} catch (exception e) {
e.printstacktrace();
}
}
/**
* 获取jedis实例
* @return
*/
public synchronized static jedis getjedis() {
jedis resource=null;
try {
if (jedispool != null) {
resource = jedispool.getresource();
return resource;
} else {
return null;
}
} catch (exception e) {
e.printstacktrace();
return null;
}
}
/**
* 释放jedis资源
* @param jedis
*/
public static void close(final jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
public static jedispool getjedispool() {
return jedispool;
}
}
到此这篇关于redis连接池配置及初始化实现的文章就介绍到这了,更多相关redis连接池配置内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!