spring配置jedisPool

JedisCache
import java.io.IOException;
import java.util.Set;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

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

import com.my.utils.serialization.KryoSerializationUtils;

public class JedisCache implements InitializingBean {

    private JedisPool jedisPool;

    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    public void putObject(String key, Object object) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key.getBytes(), KryoSerializationUtils.serialize(object));
        }
        catch (Exception ex) {
            handleException(ex, jedisPool, jedis);
        }
        finally {
            if (jedis != null && jedis.isConnected()) {
                jedisPool.returnResource(jedis);
            }
        }
    }

    public void putObject(String key, Object object, int expirationInSeconds) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.setex(key.getBytes(), expirationInSeconds, KryoSerializationUtils.serialize(object));
        }
        catch (Exception ex) {
            handleException(ex, jedisPool, jedis);
        }
        finally {
            if (jedis != null && jedis.isConnected()) {
                jedisPool.returnResource(jedis);
            }
        }
    }

    public Object getObject(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return (Object) KryoSerializationUtils.deserialize(jedis.get(key.getBytes()));
        }
        catch (Exception ex) {
            handleException(ex, jedisPool, jedis);
        }
        finally {
            if (jedis != null && jedis.isConnected()) {
                jedisPool.returnResource(jedis);
            }
        }
        return null;
    }

    public void removeObject(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.del(key.getBytes());
        }
        catch (Exception ex) {
            handleException(ex, jedisPool, jedis);
        }
        finally {
            if (jedis != null && jedis.isConnected()) {
                jedisPool.returnResource(jedis);
            }
        }
    }

    public boolean exists(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.exists(key.getBytes());
        }
        catch (Exception ex) {
            handleException(ex, jedisPool, jedis);
        }
        finally {
            if (jedis != null && jedis.isConnected()) {
                jedisPool.returnResource(jedis);
            }
        }
        return false;
    }

    public long expire(String key, int expirationInSeconds) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.expire(key.getBytes(), expirationInSeconds);
        }
        catch (Exception ex) {
            handleException(ex, jedisPool, jedis);
        }
        finally {
            if (jedis != null && jedis.isConnected()) {
                jedisPool.returnResource(jedis);
            }
        }
        return 0L;
    }

    public Set<String> keys(final String pattern) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.keys(pattern);
        }
        catch (Exception ex) {
            handleException(ex, jedisPool, jedis);
        }
        finally {
            if (jedis != null && jedis.isConnected()) {
                jedisPool.returnResource(jedis);
            }
        }
        return null;
    }

    /**
     * 运行时异常,IO异常,销毁jedis对象
     *
     * @param ex
     * @param jedisPool
     * @param jedis
     */
    private void handleException(Exception ex, JedisPool jedisPool, Jedis jedis) {
        if (jedis == null)
            throw new NullPointerException("jedis is null, please check the redis server.");
        if (ex instanceof IOException) {
            jedisPool.returnBrokenResource(jedis); //销毁该对象
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.notNull(jedisPool);
    }
}




application-bean.xml

<util:properties id="redisProps" location="#{T(java.lang.System).getProperty('config.local') != null ? 'classpath:redis.properties' : 'file:////nfs/toc/redis.properties'}"/>
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="#{redisProps['${profiles.active}.redis.maxTotal']}"/> <!-- 控制一个pool可分配多少个jedis实例 -->
        <property name="maxIdle" value="#{redisProps['${profiles.active}.redis.maxIdle']}" />   <!-- 控制一个pool最多有多少个状态为idle(空闲)的jedis实例 -->
        <property name="maxWaitMillis" value="#{redisProps['${profiles.active}.redis.maxWaitMillis']}" />  <!-- 表示当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException -->
        <property name="testOnBorrow" value="#{redisProps['${profiles.active}.redis.testOnBorrow']}" /> <!-- 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的 -->
    </bean>
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy">
        <constructor-arg name="poolConfig" ref="jedisPoolConfig" />  
        <constructor-arg name="host" value="#{redisProps['${profiles.active}.redis.server.host']}" />  
        <constructor-arg name="port" value="#{redisProps['${profiles.active}.redis.server.port']}" />  
    </bean>
    <bean id="jedisCache" class="com.my.cache.redis.JedisCache">
        <property name="jedisPool" ref="jedisPool"/>
    </bean>

你可能感兴趣的:(spring配置jedisPool)