当前位置: 移动技术网 > IT编程>开发语言>Java > JedisPool工具类

JedisPool工具类

2018年08月29日  | 移动技术网IT编程  | 我要评论

对于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;
      }
      }

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网