• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

maxw-obj

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

编写jedis连接池工具类

配置文件:

**jedis.properties**

jedis.host=localhost
jedis.port=6379
jedis.maxTotal=30
jedis.maxIdle=10

代码实现:

package com.stu.utils;

import java.util.ResourceBundle;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisUtils {

private static JedisPoolConfig poolConfig = null;
private static JedisPool jedisPool = null;

private static Integer maxTotal = null;
private static Integer maxIdle = null;
private static String host = null;
private static Integer port = null;

static{

//读取配置文件 获得参数值
ResourceBundle rb = ResourceBundle.getBundle("jedis");
maxTotal = Integer.parseInt(rb.getString("jedis.maxTotal"));
maxIdle = Integer.parseInt(rb.getString("jedis.maxIdle"));
port = Integer.parseInt(rb.getString("jedis.port"));
host = rb.getString("jedis.host");

poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(maxTotal);
poolConfig.setMaxIdle(maxIdle);
jedisPool = new JedisPool(poolConfig,host,port);
}

public static Jedis getJedis(){
Jedis jedis = jedisPool.getResource();
return jedis;
}

}

posted on 2025-12-24 17:25  知识札记  阅读(2)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3