Java Code Examples for redis.clients.jedis.ShardedJedisPool#getResource()
The following examples show how to use
redis.clients.jedis.ShardedJedisPool#getResource() .
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: JedisShartClient.java From howsun-javaee-framework with Apache License 2.0 | 6 votes |
/** * @param args */ public static void main(String[] args) { List<JedisShardInfo> list = new LinkedList<JedisShardInfo>(); JedisShardInfo jedisShardInfo1 = new JedisShardInfo(ip1, port); jedisShardInfo1.setPassword(JedisConstant.password); list.add(jedisShardInfo1); JedisShardInfo jedisShardInfo2 = new JedisShardInfo(ip2, port); jedisShardInfo2.setPassword(JedisConstant.password); list.add(jedisShardInfo2); ShardedJedisPool pool = new ShardedJedisPool(config, list); for (int i = 0; i < 2000; i++) { ShardedJedis jedis = pool.getResource(); String key = "howsun_" + i; //jedis.set(key, UUID.randomUUID().toString()); System.out.println(key + "\t" + jedis.get(key) + "\t" + jedis.toString()); pool.returnResource(jedis); } }
Example 2
Source File: JedisApiTest.java From easyooo-framework with Apache License 2.0 | 6 votes |
/** * 分布式连接池异步调用 * 0.452 seconds * 0.43 seconds */ @Test @Ignore public void testShardPipelinedPool() { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("localhost",6379), new JedisShardInfo("localhost",6379)); ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards); ShardedJedis one = pool.getResource(); ShardedJedisPipeline pipeline = one.pipelined(); long start = System.currentTimeMillis(); for (int i = 0; i < COUNTER; i++) { pipeline.set("sppn" + i, "n" + i); } List<Object> results = pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); pool.returnResource(one); logger.info("Pipelined@Pool SET: " + ((end - start)/1000.0) + " seconds"); pool.destroy(); }
Example 3
Source File: JedisApiTest.java From easyooo-framework with Apache License 2.0 | 6 votes |
/** * 分布式连接池同步调用 * 1.288 seconds * 1.291 seconds */ @Test public void testShardSimplePool() { List<JedisShardInfo> shards = Arrays.asList(new JedisShardInfo( "localhost", 6379), new JedisShardInfo("localhost", 6379)); ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards); ShardedJedis one = pool.getResource(); long start = System.currentTimeMillis(); for (int i = 0; i < COUNTER; i++) { String result = one.set("spn" + i, "n" + i); } long end = System.currentTimeMillis(); pool.returnResource(one); logger.info("Simple@Pool SET: " + ((end - start) / 1000.0) + " seconds"); pool.destroy(); }
Example 4
Source File: RedisClientPool.java From nano-framework with Apache License 2.0 | 6 votes |
public ShardedJedis getJedis(final String poolName) { Assert.hasText(poolName); ShardedJedis shardedJedis = null; try { final ShardedJedisPool pool = jedisPool.get(poolName); if (pool != null) { shardedJedis = pool.getResource(); } Assert.notNull(shardedJedis, "Not found ShardedJedis."); return shardedJedis; } catch (final Throwable e) { close(shardedJedis); throw new RedisClientException(e.getMessage(), e); } }
Example 5
Source File: ShardedJedisPoolTest.java From cachecloud with Apache License 2.0 | 6 votes |
@Test public void checkResourceIsCloseable() throws URISyntaxException { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6380"))); shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6379"))); ShardedJedisPool pool = new ShardedJedisPool(config, shards); ShardedJedis jedis = pool.getResource(); try { jedis.set("hello", "jedis"); } finally { jedis.close(); } ShardedJedis jedis2 = pool.getResource(); try { assertEquals(jedis, jedis2); } finally { jedis2.close(); } }
Example 6
Source File: ShardedJedisPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void returnResourceShouldResetState() throws URISyntaxException { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6380"))); shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6379"))); ShardedJedisPool pool = new ShardedJedisPool(config, shards); ShardedJedis jedis = pool.getResource(); jedis.set("pipelined", String.valueOf(0)); jedis.set("pipelined2", String.valueOf(0)); ShardedJedisPipeline pipeline = jedis.pipelined(); pipeline.incr("pipelined"); pipeline.incr("pipelined2"); jedis.resetState(); pipeline = jedis.pipelined(); pipeline.incr("pipelined"); pipeline.incr("pipelined2"); List<Object> results = pipeline.syncAndReturnAll(); assertEquals(2, results.size()); jedis.close(); pool.destroy(); }
Example 7
Source File: JedisDemo.java From JavaTutorial with Apache License 2.0 | 5 votes |
/** * 多机分布式+连接池。 */ private static void shardPool() { // 生成多机连接信息列表 List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); shards.add( new JedisShardInfo("127.0.0.1", 6379) ); shards.add( new JedisShardInfo("192.168.56.102", 6379) ); // 生成连接池配置信息 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(10); config.setMaxTotal(30); config.setMaxWaitMillis(3*1000); // 在应用初始化的时候生成连接池 ShardedJedisPool pool = new ShardedJedisPool(config, shards); // 在业务操作时,从连接池获取连接 ShardedJedis client = pool.getResource(); try { // 执行指令 String result = client.set("key-string", "Hello, Redis!"); System.out.println( String.format("set指令执行结果:%s", result) ); String value = client.get("key-string"); System.out.println( String.format("get指令执行结果:%s", value) ); } catch (Exception e) { // TODO: handle exception } finally { // 业务操作完成,将连接返回给连接池 if (null != client) { pool.returnResource(client); } } // end of try block // 应用关闭时,释放连接池资源 pool.destroy(); }
Example 8
Source File: RedisExample.java From java-platform with Apache License 2.0 | 5 votes |
public void testShardPipelinnedPool() {// 0.124秒 JedisShardInfo jedis = new JedisShardInfo("120.25.241.144", 6379); jedis.setPassword("b840fc02d52404542994"); List<JedisShardInfo> shards = Arrays.asList(jedis); ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards); ShardedJedis sharding = pool.getResource(); ShardedJedisPipeline pipeline = sharding.pipelined(); long start = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) { pipeline.set("n" + i, "n" + i); System.out.println(i); } pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); System.out.println("共花费:" + (end - start) / 1000.0 + "秒"); sharding.disconnect(); pool.destroy(); try { Closeables.close(sharding, true); Closeables.close(pool, true); } catch (IOException e) { e.printStackTrace(); } }
Example 9
Source File: RedisExample.java From java-platform with Apache License 2.0 | 5 votes |
public void testShardSimplePool() {// 12.642秒 JedisShardInfo jedis = new JedisShardInfo("120.25.241.144", 6379); jedis.setPassword("b840fc02d52404542994"); List<JedisShardInfo> shards = Arrays.asList(jedis); ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards); ShardedJedis sharding = pool.getResource(); long start = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) { sharding.set("n" + i, "n" + i); System.out.println(i); } long end = System.currentTimeMillis(); System.out.println("共花费:" + (end - start) / 1000.0 + "秒"); sharding.disconnect(); pool.destroy(); try { Closeables.close(sharding, true); Closeables.close(pool, true); } catch (IOException e) { e.printStackTrace(); } }
Example 10
Source File: ShardedJedisPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void checkConnections() { ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards); ShardedJedis jedis = pool.getResource(); jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); jedis.close(); pool.destroy(); }
Example 11
Source File: ShardedJedisPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void checkFailedJedisServer() { ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards); ShardedJedis jedis = pool.getResource(); jedis.incr("foo"); jedis.close(); pool.destroy(); }
Example 12
Source File: ShardedJedisPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void shouldNotShareInstances() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(2); ShardedJedisPool pool = new ShardedJedisPool(config, shards); ShardedJedis j1 = pool.getResource(); ShardedJedis j2 = pool.getResource(); assertNotSame(j1.getShard("foo"), j2.getShard("foo")); }
Example 13
Source File: ShardedJedisPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test(expected = JedisConnectionException.class) public void checkPoolOverflow() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(1); config.setBlockWhenExhausted(false); ShardedJedisPool pool = new ShardedJedisPool(config, shards); ShardedJedis jedis = pool.getResource(); jedis.set("foo", "0"); ShardedJedis newJedis = pool.getResource(); newJedis.incr("foo"); }
Example 14
Source File: ShardedJedisPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void checkPoolRepairedWhenJedisIsBroken() { ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards); ShardedJedis jedis = pool.getResource(); jedis.disconnect(); jedis.close(); jedis = pool.getResource(); jedis.incr("foo"); jedis.close(); pool.destroy(); }
Example 15
Source File: ShardedJedisPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void checkJedisIsReusedWhenReturned() { ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards); ShardedJedis jedis = pool.getResource(); jedis.set("foo", "0"); jedis.close(); jedis = pool.getResource(); jedis.incr("foo"); jedis.close(); pool.destroy(); }
Example 16
Source File: ShardedJedisPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void checkConnectionWithDefaultPort() { ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards); ShardedJedis jedis = pool.getResource(); jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); jedis.close(); pool.destroy(); }
Example 17
Source File: ShardedJedisPoolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void checkCloseableConnections() throws Exception { ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards); ShardedJedis jedis = pool.getResource(); jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); jedis.close(); pool.close(); assertTrue(pool.isClosed()); }