RedisHandler实例

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

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


public class RedisHandler {

	private Logger logger = Logger.getLogger(RedisHandler.class);

	private static String redisHost = PropertiesUtils.getProperties("redisHost");

	private static int redisPort = Integer.valueOf(PropertiesUtils.getProperties("redisPort"));

	private static int redisMaxTotal = Integer.valueOf(PropertiesUtils.getProperties("redisMaxTotal"));

	private static int redisMaxIdle = Integer.valueOf(PropertiesUtils.getProperties("redisMaxIdle"));

	private static int redisMaxWaitMillis = Integer.valueOf(PropertiesUtils.getProperties("redisMaxWaitMillis"));

	private static int redisTimeOut = Integer.valueOf(PropertiesUtils.getProperties("redisTimeOut"));

	private static int redisRetryNum = Integer.valueOf(PropertiesUtils.getProperties("redisRetryNum"));

	/**
	 * 构造器私有
	 */
	private RedisHandler() {
	}

	private static Map<String, JedisPool> maps = new HashMap<String, JedisPool>();

	private static JedisPool getPool(String ip, int port) {

		String key = ip + ":" + port;
		JedisPool pool = null;
		if (!maps.containsKey(key)) {
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxTotal(RedisHandler.redisMaxTotal);
			config.setMaxIdle(RedisHandler.redisMaxIdle);
			config.setMaxWaitMillis(RedisHandler.redisMaxWaitMillis);
			config.setTestOnBorrow(true);
			config.setTestOnReturn(true);
			try {
				
				pool = new JedisPool(config, ip, port, RedisHandler.redisTimeOut);
				maps.put(key, pool);
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		} else {
			
			pool = maps.get(key);
		}
		return pool;
	}

	private static class RedisHandlerHolder {
		/**
		 * 静态初始化器,由JVM来保证线程安全
		 */
		private static RedisHandler instance = new RedisHandler();
	}

	public static RedisHandler getInstance() {

		return RedisHandlerHolder.instance;
	}

	/**
	 * 获取Redis默认实例.
	 * 
	 * @return Redis工具类实例
	 */
	public Jedis getDefaultJedis() {

		String ip = RedisHandler.redisHost;
		int port = RedisHandler.redisPort;
		Jedis jedis = null;
		int count = 0;
		do {
			try {
				jedis = getPool(ip, port).getResource();
			} catch (Exception e) {
				logger.error("获取redis缓存的对象数据时----》", e);
				// 销毁对象
				getPool(ip, port).returnBrokenResource(jedis);
			}
			count++;
		} while (jedis == null && count < RedisHandler.redisRetryNum);

		return jedis;
	}

	protected Jedis getJdeis(String ip, int port) {

		Jedis jedis = null;
		int count = 0;
		do {
			try {
				jedis = getPool(ip, port).getResource();
			} catch (Exception e) {
				logger.error("获取redis缓存的对象数据时----》", e);
				// 销毁对象
				getPool(ip, port).returnBrokenResource(jedis);
			}
			count++;
		} while (jedis == null && count < RedisHandler.redisRetryNum);

		return jedis;
	}

	/**
	 * 
	 * @Title
	 * @Description 释放redis实例到连接池
	 * @param jedis
	 * @param ip
	 * @param port
	 */
	protected void closeJedis(Jedis jedis, String ip, int port) {

		if (jedis != null) {

			getPool(ip, port).returnResource(jedis);
		}
	}

	/**
	 * 
	 * @Title
	 * @Description 释放系统默认redis实例到连接池
	 * @param jedis
	 */
	public void closeDefaultJedis(Jedis jedis) {

		if (jedis != null) {

			getPool(RedisHandler.redisHost, RedisHandler.redisPort).returnResource(jedis);
		}
	}
	
	/**
	 * 
	 * @Title 
	 * @Description 异常是返回连接池
	 * @param jedis
	 */
	public void closeDefaultBrokenResource(Jedis jedis){
		
		if(jedis != null){
			
			getPool(RedisHandler.redisHost, RedisHandler.redisPort).returnBrokenResource(jedis);
		}
	}

}

你可能感兴趣的:(java,redis,jdeis)