码迷,mamicode.com
首页 > 其他好文 > 详细

JedisPool工具类

时间:2018-08-28 20:09:56      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:ini   stat   ram   password   最小   redis   说明   空闲   conf   

对于redis没有设置密码情况,直接去掉类中的密码即可
package com.practice.utils;

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

import java.util.ResourceBundle;

/**

  • 〈说明〉
  • 〈〉
  • @author lenovo
  • @since 1.0.0
    */
    public class JedisPoolUtils {
    /** 定义最大空闲数 */
    private static int MAX_IDLE;

    /** 定义最小空闲数 */
    private static int MIN_IDLE;

    /** 定义最大连接数 */
    private static int MAX_TOTAL;

    /** 定义远程IP地址 */
    private static String URL;

    /** 定义连接端口号 */
    private static int PORT;

    /** 定义redis客户端登录密码 */
    private static String PASSWORD;

    /** 定义连接超时时间 */
    private static int TIME_OUT;

    /** 创建JedisPool对象,定义为空,等待初始化 */
    private static JedisPool pool = null;

    /**
    • 静态代码块
    • 初始化连接池
      */
      static {
      JedisPoolConfig poolConfig = init();
      //初始化连接池对象
      pool = new JedisPool(poolConfig, URL, PORT, TIME_OUT, PASSWORD);
      }
    /**
    • 静态方法获取Jedis对象
    • 获取前进行非空判断
    • pool如果为空,则返回null
    • @return
      */
      public synchronized static Jedis getJedis() {
      if (pool != null) {
      return pool.getResource();
      }
      return null;
      }
    /**
    • 回收Jedis
    • 回收前进行非空判断
    • final Jedis jedis确保传进来的Jedis对象与returnJedis(Jedis jedis)方法内Jedis对象一致
    • @param jedis
    • @throws Exception
      */
      public static void returnJedis(final Jedis jedis) {
      if (jedis != null) {
      pool.returnResourceObject(jedis);
      }
      }
    /**
    • 静态方法init()用于初始化配置参数
    • 私有方法置于最后
    • @return
      */
      private static JedisPoolConfig init() {
      //读取并加载初始化参数
      ResourceBundle bundle = ResourceBundle.getBundle("redis");
      MAX_IDLE = Integer.valueOf(bundle.getObject("redis.maxIdle").toString());
      MIN_IDLE = Integer.valueOf(bundle.getObject("redis.minIdle").toString());
      MAX_TOTAL = Integer.valueOf(bundle.getObject("redis.maxTotal").toString());
      URL = bundle.getString("redis.url");
      PORT = Integer.valueOf(bundle.getObject("redis.port").toString());
      PASSWORD = bundle.getString("redis.password");
      TIME_OUT = Integer.valueOf(bundle.getObject("redis.timeOut").toString());

      //创建连接池配置对象
      JedisPoolConfig poolConfig = new JedisPoolConfig();
      poolConfig.setMaxIdle(MAX_IDLE);
      poolConfig.setMinIdle(MIN_IDLE);
      poolConfig.setMaxTotal(MAX_TOTAL);
      return poolConfig;
      }
      }

JedisPool工具类

标签:ini   stat   ram   password   最小   redis   说明   空闲   conf   

原文地址:https://www.cnblogs.com/jay8576/p/9550122.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!