Redis+linux的Bundle池化(四)

来源:互联网 发布:经济学书籍推荐 知乎 编辑:程序博客网 时间:2024/06/06 02:02

使用Bundle的池化的好处,是它底层帮我们封装了读取,
redis.properties文件的方法,我们直接调用就可以。
跟前面的案列一样,导入包,然后利用Bundle。
注意:
读取文件时不要加后缀名了。
最后有个池的释放,以后加了spring框架就不需要了。
代码:

import java.util.ResourceBundle;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;public class Test1 {    private static JedisPool pool;    static {        ResourceBundle bundle = ResourceBundle.getBundle("redis");        if (bundle == null) {            throw new IllegalArgumentException("[redis.properties] is not found!");        }        JedisPoolConfig config = new JedisPoolConfig();        config.setMaxTotal(Integer.valueOf(bundle.getString("redis.pool.maxActive")));        config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));        config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait")));        config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));        config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));        pool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")));    }    public static void main(String[] args) {        // Jedis jedis=new Jedis("192.168.15.215");        Jedis jedis = pool.getResource();        String keys = "name";        jedis.del(keys);        jedis.set(keys, "yjl");        String value = jedis.get(keys);        System.out.println(value);        pool.returnBrokenResource(jedis);//回收池连接    }}
0 0
原创粉丝点击