com.ny.apps.executor.jedis.SimpleJedisCacheTools.java Source code

Java tutorial

Introduction

Here is the source code for com.ny.apps.executor.jedis.SimpleJedisCacheTools.java

Source

/**
 * 
 * @Title SimpleJedisCacheTools.java
 * @Description TODO
 * Copyright: Copyright (c) 2013, Smiletony and/or its affiliates. All rights reserved.
 * SMILETONY PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @author NY
 * @date 2013-12-13 ?2:43:16
 * 
 */
package com.ny.apps.executor.jedis;

import java.io.IOException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

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

/**
 *
 * @Description ?Jedis?
 * @author <a href="mailto:mynameisny@qq.com">Tony Joseph</a>
 * @version 1.0
 *
 */
public class SimpleJedisCacheTools extends AbstractJedisCacheTools {
    public static Logger logger = LoggerFactory.getLogger(SimpleJedisCacheTools.class);

    /**
     * Redis?0~15
     */
    private int dbIndex;

    /**
     * pool??jedispool.getResource()??-1??pool??maxActivejedispool?exhausted()
     */
    @Value("#{jedisPropertiesReader['maxActiveNumber']}")
    private int maxActiveNumber;

    /**
     * pool?idle()jedis
     */
    @Value("#{jedisPropertiesReader['maxIdleNumber']}")
    private int maxIdleNumber;

    /**
     * borrow()jedisJedisConnectionException
     */
    @Value("#{jedisPropertiesReader['maxWaitSecond']}")
    private int maxWaitSecond;

    /**
     * borrowjedis????validate?true??jedis??
     */
    @Value("#{jedisPropertiesReader['isTestOnBorrow']}")
    private boolean isTestOnBorrow;

    /**
     * Redis?IP?
     */
    @Value("#{jedisPropertiesReader['host']}")
    private String host;

    /**
     * Redis??
     */
    @Value("#{jedisPropertiesReader['port']}")
    private int port;

    public int getDbIndex() {
        return dbIndex;
    }

    public void setDbIndex(int dbIndex) {
        this.dbIndex = dbIndex;
    }

    public int getMaxActiveNumber() {
        return maxActiveNumber;
    }

    public void setMaxActiveNumber(int maxActiveNumber) {
        this.maxActiveNumber = maxActiveNumber;
    }

    public int getMaxIdleNumber() {
        return maxIdleNumber;
    }

    public void setMaxIdleNumber(int maxIdleNumber) {
        this.maxIdleNumber = maxIdleNumber;
    }

    public int getMaxWaitSecond() {
        return maxWaitSecond;
    }

    public void setMaxWaitSecond(int maxWaitSecond) {
        this.maxWaitSecond = maxWaitSecond;
    }

    public boolean isTestOnBorrow() {
        return isTestOnBorrow;
    }

    public void setTestOnBorrow(boolean isTestOnBorrow) {
        this.isTestOnBorrow = isTestOnBorrow;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public SimpleJedisCacheTools() {
        Resource resource = new ClassPathResource("/jedis.properties");
        try {
            Properties props = PropertiesLoaderUtils.loadProperties(resource);
            this.maxActiveNumber = Integer.parseInt((String) props.get("maxActiveNumber"));
            this.maxIdleNumber = Integer.parseInt((String) props.get("maxIdleNumber"));
            this.maxWaitSecond = Integer.parseInt((String) props.get("maxWaitSecond"));
            this.isTestOnBorrow = Boolean.parseBoolean((String) props.get("isTestOnBorrow"));
            this.host = (String) props.get("host");
            this.port = Integer.parseInt((String) props.get("port"));
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        if (this.jedisPool == null) {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxActive(maxActiveNumber);
            config.setMaxIdle(maxIdleNumber);
            config.setMaxWait(1000 * maxWaitSecond);
            config.setTestOnBorrow(isTestOnBorrow);

            jedisPool = new JedisPool(config, host, port);
        }
    }

    /* ? Javadoc
     * @see com.ny.apps.executor.jedis.AbstractJedisCacheTools#getDBIndex()
     */
    @Override
    public int getDBIndex() {
        return dbIndex;
    }

}