6.如何使用jedis的線程池

Basic usage example

using Jedis in a multithreaded environment

You shouldn't use the same instance from different threads because you'll have strange errors. And sometimes creating lots of Jedis instances is not good enough because it means lots of sockets and connections, which leads to strange errors as well. A single Jedis instance is not threadsafe!To avoid these problems, you should use JedisPool, which is a threadsafe pool of network connections. You can use the pool to reliably create several Jedis instances, given you return the Jedis instance to the pool when done. This way you can overcome those strange errors and achieve great performance.html

To use it, init a pool:java

JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");

You can store the pool somewhere statically, it is thread-safe.git

JedisPoolConfig includes a number of helpful Redis-specific connection pooling defaults. JedisPool is based on Commons Pool 2, so you may want to have a look at Commons Pool's configuration. Please see http://commons.apache.org/proper/commons-pool/apidocs/org/apache/commons/pool2/impl/GenericObjectPoolConfig.html for more details.github

You use it by:redis

/// Jedis implements Closable. Hence, the jedis instance will be auto-closed after the last statement.
try (Jedis jedis = pool.getResource()) {
  /// ... do stuff here ... for example
  jedis.set("foo", "bar");
  String foobar = jedis.get("foo");
  jedis.zadd("sose", 0, "car"); jedis.zadd("sose", 0, "bike"); 
  Set<String> sose = jedis.zrange("sose", 0, -1);
}
/// ... when closing your application:
pool.destroy();

If you can't use try-with-resource, you can still enjoy with Jedis.close().apache

Jedis jedis = null;
try {
  jedis = pool.getResource();
  /// ... do stuff here ... for example
  jedis.set("foo", "bar");
  String foobar = jedis.get("foo");
  jedis.zadd("sose", 0, "car"); jedis.zadd("sose", 0, "bike"); 
  Set<String> sose = jedis.zrange("sose", 0, -1);
} finally {
  if (jedis != null) {
    jedis.close();
  }
}
/// ... when closing your application:
pool.destroy();

If Jedis was borrowed from pool, it will be returned to pool with proper method since it already determines there was JedisConnectionException occurred. If Jedis wasn't borrowed from pool, it will be disconnected and closed.api

 

使用實例:app

package com.scut.emos.nziot_base.redis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
 * Created by CMQ on 2017/3/9.
 */
public class JedisPoolHelper {
    private static JedisPool pool = null;
    public static JedisPool getPool(JedisPoolConfig config,String host,int port) {
        if (pool == null) {
            pool = new JedisPool(config, host, port);
        }
        return pool;
    }
    public void destroyPool(){
        pool.destroy();
    }
}
package com.scut.emos.nziot_base;
import com.scut.emos.nziot_base.redis.JedisPoolHelper;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
 * 測試類
 */
public class TestClass {
    private static JedisPool pool = null;
    public TestClass(){
        pool = JedisPoolHelper.getPool(getConfig(),"222.201.145.215",6379);
    }
    public static JedisPoolConfig getConfig(){
        JedisPoolConfig config = new JedisPoolConfig();
        //控制一個pool可分配多少個jedis實例,經過pool.getResource()來獲取;
        //若是賦值爲-1,則表示不限制;若是pool已經分配了maxActive個jedis實例,則此時pool的狀態爲exhausted(耗盡)。
        config.setMaxTotal(500);
        //控制一個pool最多有多少個狀態爲idle(空閒的)的jedis實例。
        config.setMaxIdle(5);
        //表示當borrow(引入)一個jedis實例時,最大的等待時間,若是超過等待時間,則直接拋出JedisConnectionException;
        config.setMaxWaitMillis(1000 * 100);
        //在borrow一個jedis實例時,是否提早進行validate操做;若是爲true,則獲得的jedis實例均是可用的;
        config.setTestOnBorrow(true);
        return config;
    }
    public void sampleUseJedis(){
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.set("cdaf","dashuaiguola");
            String str = jedis.get("cai");
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
    public static void main(String args[]){

        new TestClass().sampleUseJedis();
        // 1. create log
        Logger log = Logger.getLogger(TestClass.class);
        // 2. get log config file
        PropertyConfigurator.configure("log4j.properties");
        // 3. start log
        log.info("sdfa");

    }
}
相關文章
相關標籤/搜索