redis.clients.jedis.util.Pool Java Examples

The following examples show how to use redis.clients.jedis.util.Pool. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JedisPoolFactory.java    From kork with Apache License 2.0 6 votes vote down vote up
public Pool<Jedis> build(
    String name, JedisDriverProperties properties, GenericObjectPoolConfig objectPoolConfig) {
  if (properties.connection == null || "".equals(properties.connection)) {
    throw new MissingRequiredConfiguration("Jedis client must have a connection defined");
  }

  URI redisConnection = URI.create(properties.connection);

  String host = redisConnection.getHost();
  int port = redisConnection.getPort() == -1 ? Protocol.DEFAULT_PORT : redisConnection.getPort();
  int database = parseDatabase(redisConnection.getPath());
  String password = parsePassword(redisConnection.getUserInfo());
  GenericObjectPoolConfig poolConfig =
      Optional.ofNullable(properties.poolConfig).orElse(objectPoolConfig);
  boolean isSSL = redisConnection.getScheme().equals("rediss");

  return new InstrumentedJedisPool(
      registry,
      // Pool name should always be "null", as setting this is incompat with some SaaS Redis
      // offerings
      new JedisPool(
          poolConfig, host, port, properties.timeoutMs, password, database, null, isSSL),
      name);
}
 
Example #2
Source File: JedisPoolFactory.java    From jetcache with Apache License 2.0 5 votes vote down vote up
@Override
public Pool<Jedis> getObject() throws Exception {
    if (!inited) {
        jedisPool = (Pool<Jedis>) autoConfigureBeans.getCustomContainer().get("jedisPool." + key);
        inited = true;
    }
    return jedisPool;
}
 
Example #3
Source File: RedisAutoConfiguration.java    From jetcache with Apache License 2.0 5 votes vote down vote up
@Override
protected CacheBuilder initCache(ConfigTree ct, String cacheAreaWithPrefix) {
    Pool jedisPool = parsePool(ct);
    Pool[] slavesPool = null;
    int[] slavesPoolWeights = null;
    boolean readFromSlave = Boolean.parseBoolean(ct.getProperty("readFromSlave", "False"));
    ConfigTree slaves = ct.subTree("slaves.");
    Set<String> slaveNames = slaves.directChildrenKeys();
    if (slaveNames.size() > 0) {
        slavesPool = new Pool[slaveNames.size()];
        slavesPoolWeights = new int[slaveNames.size()];
        int i = 0;
        for (String slaveName: slaveNames) {
            ConfigTree slaveConfig = slaves.subTree(slaveName + ".");
            slavesPool[i] = parsePool(slaveConfig);
            slavesPoolWeights[i] = Integer.parseInt(slaveConfig.getProperty("weight","100"));
            i++;
        }
    }

    ExternalCacheBuilder externalCacheBuilder = RedisCacheBuilder.createRedisCacheBuilder()
            .jedisPool(jedisPool)
            .readFromSlave(readFromSlave)
            .jedisSlavePools(slavesPool)
            .slaveReadWeights(slavesPoolWeights);

    parseGeneralConfig(externalCacheBuilder, ct);

    // eg: "jedisPool.remote.default"
    autoConfigureBeans.getCustomContainer().put("jedisPool." + cacheAreaWithPrefix, jedisPool);

    return externalCacheBuilder;
}
 
Example #4
Source File: RedisStarterTest.java    From jetcache with Apache License 2.0 5 votes vote down vote up
public void test() {
    Assert.assertNotNull(c1.unwrap(com.github.benmanes.caffeine.cache.Cache.class));
    EmbeddedCacheConfig cc1 = (EmbeddedCacheConfig) c1.config();
    Assert.assertEquals(200, cc1.getLimit());
    Assert.assertEquals(10000, cc1.getExpireAfterWriteInMillis());
    Assert.assertFalse(cc1.isExpireAfterAccess());
    Assert.assertSame(FastjsonKeyConvertor.INSTANCE, cc1.getKeyConvertor());

    RedisCacheConfig c = (RedisCacheConfig) c2.config();
    Assert.assertFalse(c.isReadFromSlave());
    Pool[] slavePools = c.getJedisSlavePools();
    Assert.assertEquals(2, slavePools.length);
    int[] ws = c.getSlaveReadWeights();
    Assert.assertTrue(Arrays.equals(new int[]{30, 100}, ws) || Arrays.equals(new int[]{100, 30}, ws));
}
 
Example #5
Source File: RedisStarterTest.java    From jetcache with Apache License 2.0 5 votes vote down vote up
@Test
public void tests() throws Exception {
    System.setProperty("spring.profiles.active", "redis");
    context = SpringApplication.run(RedisStarterTest.class);
    doTest();
    A bean = context.getBean(A.class);
    bean.test();

    Pool<Jedis> t1 = (Pool<Jedis>) context.getBean("defaultPool");
    Pool<Jedis> t2 = (Pool<Jedis>) context.getBean("A1Pool");
    Assert.assertNotNull(t1);
    Assert.assertNotNull(t2);
    Assert.assertNotSame(t1, t2);
}
 
Example #6
Source File: JedisPoolFactory.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
public Pool<Jedis> createPool(RedisConnectionConfig connectionConfig) {
    final RedisConnectionConfig.RedisScheme scheme = connectionConfig.getScheme();
    if (scheme == null) {
        throw getUnsupportedSchemeException();
    }
    switch (scheme) {
        case NODE:
            return poolCreator.createJedisPool(connectionConfig);
        case SENTINEL:
            return poolCreator.createJedisSentinelPool(connectionConfig);
        default:
            throw getUnsupportedSchemeException();
    }
}
 
Example #7
Source File: RedisCache.java    From jetcache with Apache License 2.0 5 votes vote down vote up
Pool<Jedis> getReadPool() {
    if (!config.isReadFromSlave()) {
        return config.getJedisPool();
    }
    int[] weights = config.getSlaveReadWeights();
    int index = randomIndex(weights);
    return config.getJedisSlavePools()[index];
}
 
Example #8
Source File: RedisCache.java    From jetcache with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T unwrap(Class<T> clazz) {
    if (Pool.class.isAssignableFrom(clazz)) {
        return (T) config.getJedisPool();
    }
    throw new IllegalArgumentException(clazz.getName());
}
 
Example #9
Source File: JedisHealthIndicatorFactory.java    From kork with Apache License 2.0 5 votes vote down vote up
public static HealthIndicator build(JedisClientDelegate client) {
  try {
    final JedisClientDelegate src = client;
    final Field clientAccess = JedisClientDelegate.class.getDeclaredField("jedisPool");
    clientAccess.setAccessible(true);

    return build((Pool<Jedis>) clientAccess.get(src));
  } catch (IllegalAccessException | NoSuchFieldException e) {
    throw new BeanCreationException("Error creating Redis health indicator", e);
  }
}
 
Example #10
Source File: JedisHealthIndicatorFactory.java    From kork with Apache License 2.0 5 votes vote down vote up
public static HealthIndicator build(Pool<Jedis> jedisPool) {
  try {
    final Pool<Jedis> src = jedisPool;
    final Field poolAccess = Pool.class.getDeclaredField("internalPool");
    poolAccess.setAccessible(true);
    GenericObjectPool<Jedis> internal = (GenericObjectPool<Jedis>) poolAccess.get(jedisPool);
    return () -> {
      Jedis jedis = null;
      Health.Builder health;
      try {
        jedis = src.getResource();
        if ("PONG".equals(jedis.ping())) {
          health = Health.up();
        } else {
          health = Health.down();
        }
      } catch (Exception ex) {
        health = Health.down(ex);
      } finally {
        if (jedis != null) jedis.close();
      }
      health.withDetail("maxIdle", internal.getMaxIdle());
      health.withDetail("minIdle", internal.getMinIdle());
      health.withDetail("numActive", internal.getNumActive());
      health.withDetail("numIdle", internal.getNumIdle());
      health.withDetail("numWaiters", internal.getNumWaiters());

      return health.build();
    };
  } catch (IllegalAccessException | NoSuchFieldException e) {
    throw new BeanCreationException("Error creating Redis health indicator", e);
  }
}
 
Example #11
Source File: Client.java    From JRediSearch with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Create a new client to a RediSearch index
 *
 * @param indexName the name of the index we are connecting to or creating
 * @param pool jedis connection pool to be used
 */
public Client(String indexName, Pool<Jedis> pool) {
  this.indexName = indexName;
  this.endocdedIndexName = SafeEncoder.encode(indexName);
  this.pool = pool;
  this.commands = new Commands.SingleNodeCommands();
}
 
Example #12
Source File: JedisClientDelegate.java    From kork with Apache License 2.0 4 votes vote down vote up
public JedisClientDelegate(Pool<Jedis> jedisPool) {
  this("default", jedisPool);
}
 
Example #13
Source File: BaseIntegrationTest.java    From quartz-redis-jobstore with Apache License 2.0 4 votes vote down vote up
public void setJedisPool(Pool<Jedis> jedisPool) {
    this.jedisPool = jedisPool;
}
 
Example #14
Source File: RedisJobStore.java    From quartz-redis-jobstore with Apache License 2.0 4 votes vote down vote up
public RedisJobStore setJedisPool(Pool<Jedis> jedisPool) {
    this.jedisPool = jedisPool;
    return this;
}
 
Example #15
Source File: JedisClientDelegate.java    From kork with Apache License 2.0 4 votes vote down vote up
public JedisClientDelegate(String name, Pool<Jedis> jedisPool) {
  this.name = name;
  this.jedisPool = jedisPool;
}
 
Example #16
Source File: JedisClientProvider.java    From logback-redis with Apache License 2.0 4 votes vote down vote up
private synchronized Pool<Jedis> getPool() {
    if (pool == null) {
        pool = poolFactory.createPool(connectionConfig);
    }
    return pool;
}
 
Example #17
Source File: JedisLockProvider.java    From ShedLock with Apache License 2.0 4 votes vote down vote up
private JedisPoolTemplate(Pool<Jedis> jedisPool) {
    this.jedisPool = jedisPool;
}
 
Example #18
Source File: JedisLockProvider.java    From ShedLock with Apache License 2.0 4 votes vote down vote up
public JedisLockProvider(@NonNull Pool<Jedis> jedisPool) {
    this(jedisPool, ENV_DEFAULT);
}
 
Example #19
Source File: JedisPoolFactory.java    From jetcache with Apache License 2.0 4 votes vote down vote up
public JedisPoolFactory(String key, Class<? extends Pool<Jedis>> poolClass){
    this.key = key;
    this.poolClass = poolClass;
}
 
Example #20
Source File: RedisCacheTest.java    From jetcache with Apache License 2.0 4 votes vote down vote up
private void testWithPool(Pool<Jedis> pool) throws Exception {
    cache = RedisCacheBuilder.createRedisCacheBuilder()
            .keyConvertor(FastjsonKeyConvertor.INSTANCE)
            .valueEncoder(JavaValueEncoder.INSTANCE)
            .valueDecoder(JavaValueDecoder.INSTANCE)
            .jedisPool(pool)
            .keyPrefix(new Random().nextInt() + "")
            .expireAfterWrite(500, TimeUnit.MILLISECONDS)
            .buildCache();

    Assert.assertSame(pool, cache.unwrap(Pool.class));
    if (pool instanceof JedisPool) {
        Assert.assertSame(pool, cache.unwrap(JedisPool.class));
    } else {
        Assert.assertSame(pool, cache.unwrap(JedisSentinelPool.class));
    }

    baseTest();
    fastjsonKeyCoverterTest();
    expireAfterWriteTest(cache.config().getExpireAfterWriteInMillis());

    LoadingCacheTest.loadingCacheTest(RedisCacheBuilder.createRedisCacheBuilder()
            .keyConvertor(FastjsonKeyConvertor.INSTANCE)
            .valueEncoder(JavaValueEncoder.INSTANCE)
            .valueDecoder(JavaValueDecoder.INSTANCE)
            .jedisPool(pool)
            .keyPrefix(new Random().nextInt() + ""), 0);
    RefreshCacheTest.refreshCacheTest(RedisCacheBuilder.createRedisCacheBuilder()
            .keyConvertor(FastjsonKeyConvertor.INSTANCE)
            .valueEncoder(JavaValueEncoder.INSTANCE)
            .valueDecoder(JavaValueDecoder.INSTANCE)
            .jedisPool(pool)
            .keyPrefix(new Random().nextInt() + ""), 200, 100);


    cache = RedisCacheBuilder.createRedisCacheBuilder()
            .keyConvertor(null)
            .valueEncoder(KryoValueEncoder.INSTANCE)
            .valueDecoder(KryoValueDecoder.INSTANCE)
            .jedisPool(pool)
            .keyPrefix(new Random().nextInt() + "")
            .buildCache();
    nullKeyConvertorTest();

    int thread = 10;
    int time = 3000;
    cache = RedisCacheBuilder.createRedisCacheBuilder()
            .keyConvertor(FastjsonKeyConvertor.INSTANCE)
            .valueEncoder(KryoValueEncoder.INSTANCE)
            .valueDecoder(KryoValueDecoder.INSTANCE)
            .jedisPool(pool)
            .keyPrefix(new Random().nextInt() + "")
            .buildCache();
    concurrentTest(thread, 500, time);
}
 
Example #21
Source File: RedisCacheConfig.java    From jetcache with Apache License 2.0 4 votes vote down vote up
public void setJedisSlavePools(Pool<Jedis>... jedisSlavePools) {
    this.jedisSlavePools = jedisSlavePools;
}
 
Example #22
Source File: RedisCacheConfig.java    From jetcache with Apache License 2.0 4 votes vote down vote up
public Pool<Jedis>[] getJedisSlavePools() {
    return jedisSlavePools;
}
 
Example #23
Source File: RedisCacheConfig.java    From jetcache with Apache License 2.0 4 votes vote down vote up
public void setJedisPool(Pool<Jedis> jedisPool) {
    this.jedisPool = jedisPool;
}
 
Example #24
Source File: RedisCacheConfig.java    From jetcache with Apache License 2.0 4 votes vote down vote up
public Pool<Jedis> getJedisPool() {
    return jedisPool;
}
 
Example #25
Source File: RedisCacheBuilder.java    From jetcache with Apache License 2.0 4 votes vote down vote up
public void setJedisSlavePools(Pool<Jedis>... jedisSlavePools) {
    getConfig().setJedisSlavePools(jedisSlavePools);
}
 
Example #26
Source File: RedisCacheBuilder.java    From jetcache with Apache License 2.0 4 votes vote down vote up
public void setJedisPool(Pool<Jedis> jedisPool) {
    getConfig().setJedisPool(jedisPool);
}
 
Example #27
Source File: RedisCacheBuilder.java    From jetcache with Apache License 2.0 4 votes vote down vote up
public T jedisPool(Pool<Jedis> pool) {
    getConfig().setJedisPool(pool);
    return self();
}
 
Example #28
Source File: JedisPoolFactoryTest.java    From logback-redis with Apache License 2.0 3 votes vote down vote up
@Test
public void create_jedis_pool_on_node_scheme() throws Exception {

    when(redisConnectionConfig.getScheme()).thenReturn(RedisConnectionConfig.RedisScheme.NODE);

    final Pool<Jedis> pool = jedisPoolFactory.createPool(redisConnectionConfig);

    assertThat(pool, is(instanceOf(JedisPool.class)));
}
 
Example #29
Source File: JedisPoolFactoryTest.java    From logback-redis with Apache License 2.0 3 votes vote down vote up
@Test
public void create_sentinel_pool_on_sentinel_scheme() throws Exception {

    when(redisConnectionConfig.getScheme()).thenReturn(RedisConnectionConfig.RedisScheme.SENTINEL);

    final Pool<Jedis> pool = jedisPoolFactory.createPool(redisConnectionConfig);

    assertThat(pool, is(instanceOf(JedisSentinelPool.class)));
}
 
Example #30
Source File: JedisPoolFactoryTest.java    From logback-redis with Apache License 2.0 3 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void exception_on_null_scheme() throws Exception {

    when(redisConnectionConfig.getScheme()).thenReturn(null);

    final Pool<Jedis> pool = jedisPoolFactory.createPool(redisConnectionConfig);

    assertThat(pool, is(instanceOf(JedisPool.class)));
}