Java Code Examples for redis.clients.jedis.JedisPool#destroy()

The following examples show how to use redis.clients.jedis.JedisPool#destroy() . 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: InstallControll.java    From rebuild with GNU General Public License v3.0 6 votes vote down vote up
@RequestMapping("test-cache")
public void testCache(HttpServletRequest request, HttpServletResponse response) throws IOException {
    JSONObject cacheProps = (JSONObject) ServletUtils.getRequestJson(request);

    JedisPool pool = new JedisPool(new JedisPoolConfig(),
            StringUtils.defaultIfBlank(cacheProps.getString("CacheHost"), "127.0.0.1"),
            ObjectUtils.toInt(cacheProps.getString("CachePort"), 6379),
            3000,
            StringUtils.defaultIfBlank(cacheProps.getString("CachePassword"), null));
    try (Jedis client = pool.getResource()) {
        String info = client.info("server");
        if (info.length() > 80) {
            info = info.substring(0, 80) + "...";
        }
        pool.destroy();
        writeSuccess(response, "连接成功 : " + info);
    } catch (Exception ex) {
        writeFailure(response, "连接失败 : " + ThrowableUtils.getRootCause(ex).getLocalizedMessage());
    }
}
 
Example 2
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void checkJedisIsReusedWhenReturned() {

  JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort());
  Jedis jedis = pool.getResource();
  jedis.auth("foobared");
  jedis.set("foo", "0");
  jedis.close();

  jedis = pool.getResource();
  jedis.auth("foobared");
  jedis.incr("foo");
  jedis.close();
  pool.destroy();
  assertTrue(pool.isClosed());
}
 
Example 3
Source File: RedisPoolManager.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
private void add(String client,String host,int maxTotal,int maxIdle)
{
	logger.info("Adding Redis pool for "+client+" at "+host+" maxTotal "+maxTotal+" maxIdle "+maxIdle);
	JedisPoolConfig poolConfig = new JedisPoolConfig();
	poolConfig.setMaxTotal(maxTotal); // maximum active connections
	poolConfig.setMaxIdle(maxIdle);  // maximum idle connections

	JedisPool pool = new JedisPool(poolConfig, host);
	JedisPool existing = pools.get(client);
	pools.put(client, pool);
	if (existing != null)
	{
		logger.warn("Attempting to close previous pool for "+client);
		existing.destroy();
	}
}
 
Example 4
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void nonDefaultDatabase() {
  JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000,
      "foobared");
  Jedis jedis0 = pool0.getResource();
  jedis0.set("foo", "bar");
  assertEquals("bar", jedis0.get("foo"));
  jedis0.close();
  pool0.destroy();
  assertTrue(pool0.isClosed());

  JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000,
      "foobared", 1);
  Jedis jedis1 = pool1.getResource();
  assertNull(jedis1.get("foo"));
  jedis1.close();
  pool1.destroy();
  assertTrue(pool1.isClosed());
}
 
Example 5
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void selectDatabaseOnActivation() {
  JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000,
      "foobared");

  Jedis jedis0 = pool.getResource();
  assertEquals(0, jedis0.getDB());

  jedis0.select(1);
  assertEquals(1, jedis0.getDB());

  jedis0.close();

  Jedis jedis1 = pool.getResource();
  assertTrue("Jedis instance was not reused", jedis1 == jedis0);
  assertEquals(0, jedis1.getDB());

  jedis1.close();
  pool.destroy();
  assertTrue(pool.isClosed());
}
 
Example 6
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void getNumActiveReturnsTheCorrectNumber() {
  JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000);
  Jedis jedis = pool.getResource();
  jedis.auth("foobared");
  jedis.set("foo", "bar");
  assertEquals("bar", jedis.get("foo"));

  assertEquals(1, pool.getNumActive());

  Jedis jedis2 = pool.getResource();
  jedis.auth("foobared");
  jedis.set("foo", "bar");

  assertEquals(2, pool.getNumActive());

  jedis.close();
  assertEquals(1, pool.getNumActive());

  jedis2.close();

  assertEquals(0, pool.getNumActive());

  pool.destroy();
}
 
Example 7
Source File: JedisServiceImpl.java    From jframe with Apache License 2.0 6 votes vote down vote up
@Stop
public void stop() {
    LOG.info("JedisServiceImpl stopping");

    Iterator<String> iter = _jedis.keySet().iterator();
    while (iter.hasNext()) {
        try {
            JedisPool j = _jedis.get(iter.next());
            if (j != null) j.destroy();
        } catch (Exception e) {
            LOG.warn(e.getMessage(), e);
        }
    }
    LOG.info("JedisServiceImpl stop successfully");

    close();
}
 
Example 8
Source File: Program.java    From light-task-scheduler with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        final JedisPoolConfig poolConfig = new JedisPoolConfig();
        final JedisPool jedisPool = new JedisPool(poolConfig, "localhost", 6379, 0);
        final Jedis subscriberJedis = jedisPool.getResource();
        final Subscriber subscriber = new Subscriber();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    logger.info("Subscribing to \"commonChannel\". This thread will be blocked.");
                    subscriberJedis.subscribe(subscriber, CHANNEL_NAME);
                    logger.info("Subscription ended.");
                } catch (Exception e) {
                    logger.error("Subscribing failed.", e);
                }
            }
        }).start();

        final Jedis publisherJedis = jedisPool.getResource();

        new Publisher(publisherJedis, CHANNEL_NAME).start();

        subscriber.unsubscribe();
        subscriberJedis.close();
        publisherJedis.close();
        jedisPool.destroy();
    }
 
Example 9
Source File: AppServletContextListener.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void contextDestroyed(ServletContextEvent event) {
  JedisPool jedisPool = (JedisPool) event.getServletContext().getAttribute("jedisPool");
  if (jedisPool != null) {
    jedisPool.destroy();
    event.getServletContext().setAttribute("jedisPool", null);
  }
}
 
Example 10
Source File: RedisPoolManager.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
private void remove(String client)
{
	logger.info("Removing pool for "+client);
	JedisPool pool = pools.remove(client);
	if (pool != null)
	{
		pool.destroy();
	}
}
 
Example 11
Source File: PoolBenchmark.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
private static void withPool() throws Exception {
  final JedisPool pool = new JedisPool(new GenericObjectPoolConfig(), hnp.getHost(),
      hnp.getPort(), 2000, "foobared");
  List<Thread> tds = new ArrayList<Thread>();

  final AtomicInteger ind = new AtomicInteger();
  for (int i = 0; i < 50; i++) {
    Thread hj = new Thread(new Runnable() {
      public void run() {
        for (int i = 0; (i = ind.getAndIncrement()) < TOTAL_OPERATIONS;) {
          try {
            Jedis j = pool.getResource();
            final String key = "foo" + i;
            j.set(key, key);
            j.get(key);
            j.close();
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
    });
    tds.add(hj);
    hj.start();
  }

  for (Thread t : tds)
    t.join();

  pool.destroy();

}
 
Example 12
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddObject() {
  JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000);
  pool.addObjects(1);
  assertEquals(pool.getNumIdle(), 1);
  pool.destroy();

}
 
Example 13
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void returnResourceShouldResetState() {
  GenericObjectPoolConfig config = new GenericObjectPoolConfig();
  config.setMaxTotal(1);
  config.setBlockWhenExhausted(false);
  JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");

  Jedis jedis = pool.getResource();
  try {
    jedis.set("hello", "jedis");
    Transaction t = jedis.multi();
    t.set("hello", "world");
  } finally {
    jedis.close();
  }

  Jedis jedis2 = pool.getResource();
  try {
    assertTrue(jedis == jedis2);
    assertEquals("jedis", jedis2.get("hello"));
  } finally {
    jedis2.close();
  }

  pool.destroy();
  assertTrue(pool.isClosed());
}
 
Example 14
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void customClientName() {
  JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000,
      "foobared", 0, "my_shiny_client_name");

  Jedis jedis = pool0.getResource();

  assertEquals("my_shiny_client_name", jedis.clientGetname());

  jedis.close();
  pool0.destroy();
  assertTrue(pool0.isClosed());
}
 
Example 15
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void securePool() {
  JedisPoolConfig config = new JedisPoolConfig();
  config.setTestOnBorrow(true);
  JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");
  Jedis jedis = pool.getResource();
  jedis.set("foo", "bar");
  jedis.close();
  pool.destroy();
  assertTrue(pool.isClosed());
}
 
Example 16
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void checkPoolRepairedWhenJedisIsBroken() {
  JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort());
  Jedis jedis = pool.getResource();
  jedis.auth("foobared");
  jedis.quit();
  jedis.close();

  jedis = pool.getResource();
  jedis.auth("foobared");
  jedis.incr("foo");
  jedis.close();
  pool.destroy();
  assertTrue(pool.isClosed());
}
 
Example 17
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void checkConnectionWithDefaultPort() {
  JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort());
  Jedis jedis = pool.getResource();
  jedis.auth("foobared");
  jedis.set("foo", "bar");
  assertEquals("bar", jedis.get("foo"));
  jedis.close();
  pool.destroy();
  assertTrue(pool.isClosed());
}
 
Example 18
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void checkConnections() {
  JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000);
  Jedis jedis = pool.getResource();
  jedis.auth("foobared");
  jedis.set("foo", "bar");
  assertEquals("bar", jedis.get("foo"));
  jedis.close();
  pool.destroy();
  assertTrue(pool.isClosed());
}
 
Example 19
Source File: JedisClientHelper.java    From pinlater with Apache License 2.0 4 votes vote down vote up
public void destroyClient(JedisPool jedisPool) {
  jedisPool.destroy();
}
 
Example 20
Source File: RedisProtocolTest.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
@Test
public void testAuthRedis() {
    // default db.index=0
    Invoker<IDemoService> refer = protocol.refer(IDemoService.class,
            registryUrl
                    .addParameter("max.idle", 10)
                    .addParameter("max.active", 20));
    IDemoService demoService = this.proxy.getProxy(refer);

    String value = demoService.get("key");
    assertThat(value, is(nullValue()));

    demoService.set("key", "newValue");
    value = demoService.get("key");
    assertThat(value, is("newValue"));

    demoService.delete("key");
    value = demoService.get("key");
    assertThat(value, is(nullValue()));

    refer.destroy();

    //change db.index=1
    String password = "123456";
    int database = 1;
    this.registryUrl = this.registryUrl.setPassword(password).addParameter("db.index", database);
    refer = protocol.refer(IDemoService.class,
            registryUrl
                    .addParameter("max.idle", 10)
                    .addParameter("max.active", 20));
    demoService = this.proxy.getProxy(refer);

    demoService.set("key", "newValue");
    value = demoService.get("key");
    assertThat(value, is("newValue"));

    // jedis gets the result comparison
    JedisPool pool = new JedisPool(new GenericObjectPoolConfig(), "localhost", registryUrl.getPort(), 2000, password, database, (String)null);
    Jedis jedis = null;
    try {
        jedis = pool.getResource();
        byte[] valueByte = jedis.get("key".getBytes());
        Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(this.registryUrl.getParameter(Constants.SERIALIZATION_KEY, "java"));
        ObjectInput oin = serialization.deserialize(this.registryUrl, new ByteArrayInputStream(valueByte));
        String actual = (String) oin.readObject();
        assertThat(value, is(actual));
    } catch(Exception e) {
        Assert.fail("jedis gets the result comparison is error!");
    } finally {
        if (jedis != null) {
            jedis.close();
        }
        pool.destroy();
    }

    demoService.delete("key");
    value = demoService.get("key");
    assertThat(value, is(nullValue()));

    refer.destroy();
}