Java Code Examples for redis.clients.jedis.Pipeline#exec()

The following examples show how to use redis.clients.jedis.Pipeline#exec() . 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: PipeliningTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void multiWithMassiveRequests() {
  Pipeline p = jedis.pipelined();
  p.multi();

  List<Response<?>> responseList = new ArrayList<Response<?>>();
  for (int i = 0; i < 100000; i++) {
    // any operation should be ok, but shouldn't forget about timeout
    responseList.add(p.setbit("test", 1, true));
  }

  Response<List<Object>> exec = p.exec();
  p.sync();

  // we don't need to check return value
  // if below codes run without throwing Exception, we're ok
  exec.get();

  for (Response<?> resp : responseList) {
    resp.get();
  }
}
 
Example 2
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void multiWithSync() {
  jedis.set("foo", "314");
  jedis.set("bar", "foo");
  jedis.set("hello", "world");
  Pipeline p = jedis.pipelined();
  Response<String> r1 = p.get("bar");
  p.multi();
  Response<String> r2 = p.get("foo");
  p.exec();
  Response<String> r3 = p.get("hello");
  p.sync();

  // before multi
  assertEquals("foo", r1.get());
  // It should be readable whether exec's response was built or not
  assertEquals("314", r2.get());
  // after multi
  assertEquals("world", r3.get());
}
 
Example 3
Source File: RedisExample.java    From java-platform with Apache License 2.0 6 votes vote down vote up
public void testCombPipelineTrans() {// 0.099秒
	Jedis jedis = new Jedis("120.25.241.144", 6379);
	jedis.auth("b840fc02d52404542994");

	long start = System.currentTimeMillis();
	Pipeline pipeline = jedis.pipelined();
	pipeline.multi();
	for (int i = 0; i < 1000; i++) {
		pipeline.set("n" + i, "n" + i);
		System.out.println(i);
	}
	pipeline.exec();
	pipeline.syncAndReturnAll();
	long end = System.currentTimeMillis();
	System.out.println("共花费:" + (end - start) / 1000.0 + "秒");

	jedis.disconnect();
	try {
		Closeables.close(jedis, true);
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example 4
Source File: JedisUtil.java    From gameserver with Apache License 2.0 6 votes vote down vote up
/**
 * Pop a ready room from "room_ready_set_<N>".
 */
public static final Tuple popKeyFromZset(String zsetName) {
	Pipeline pipeline = JedisFactory.getJedis().pipelined();
	
	try {
		pipeline.watch(zsetName);
		Response<Set<Tuple>> results = pipeline.zrangeWithScores(zsetName, 0, 0);
		pipeline.multi();
		pipeline.zremrangeByRank(zsetName, 0, 0);
		pipeline.exec();
		pipeline.sync();
		
		Set<Tuple> values = results.get();
		if (values.size() > 0) {
			return values.iterator().next();
		}
	} catch (Exception e) {
		try {
			pipeline.discard();
		} catch (Exception e1) {
		}
		e.printStackTrace();
	}
	return null;
}
 
Example 5
Source File: SomeOperate.java    From Redis_Learning with Apache License 2.0 5 votes vote down vote up
public static void PipelineTransactions(Jedis jedis, JedisPool jedisPool) {
	try {
		Pipeline pipeLine = jedis.pipelined();
		pipeLine.set("value", "100");
		pipeLine.watch("value");
		pipeLine.multi();// ��������
		pipeLine.incrBy("value", 10);// ����10
		// �Դ������������ʹ���˲�֧�ֵIJ���
		// pipeLine.lpush("value", "error");//ִ�д���IJ���lpush
		pipeLine.incrBy("value", 10);// �ٴε���10
		// ִ��exec����,��ȡ"δ��"�ķ��ؽ��
		Response<List<Object>> listResponse = pipeLine.exec();
		pipeLine.sync();// ����pipeling
		List<Object> result = listResponse.get();
		if (result != null && result.size() > 0) {
			for (Object o : result)
				System.out.println(o.toString());
		}
		// ��Ȼ�����еڶ�������ʧ����,����Ӱ��value��ֵ
		System.out.println("\nvalue is " + jedis.get("value"));
	} catch (Exception e) {
		// jedisPool.returnBrokenResource(jedis);
		e.printStackTrace();
	} finally {
		jedisPool.returnResource(jedis);
	}
}
 
Example 6
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void multi() {
  Pipeline p = jedis.pipelined();
  p.multi();
  Response<Long> r1 = p.hincrBy("a", "f1", -1);
  Response<Long> r2 = p.hincrBy("a", "f1", -2);
  Response<List<Object>> r3 = p.exec();
  List<Object> result = p.syncAndReturnAll();

  assertEquals(new Long(-1), r1.get());
  assertEquals(new Long(-3), r2.get());

  assertEquals(4, result.size());

  assertEquals("OK", result.get(0));
  assertEquals("QUEUED", result.get(1));
  assertEquals("QUEUED", result.get(2));

  // 4th result is a list with the results from the multi
  @SuppressWarnings("unchecked")
  List<Object> multiResult = (List<Object>) result.get(3);
  assertEquals(new Long(-1), multiResult.get(0));
  assertEquals(new Long(-3), multiResult.get(1));

  assertEquals(new Long(-1), r3.get().get(0));
  assertEquals(new Long(-3), r3.get().get(1));

}
 
Example 7
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testCloseableWithMulti() throws IOException {
  // we need to test with fresh instance of Jedis
  Jedis jedis2 = new Jedis(hnp.getHost(), hnp.getPort(), 500);
  jedis2.auth("foobared");

  Pipeline pipeline = jedis2.pipelined();
  Response<String> retFuture1 = pipeline.set("a", "1");
  Response<String> retFuture2 = pipeline.set("b", "2");

  pipeline.multi();

  pipeline.set("a", "a");
  pipeline.set("b", "b");

  pipeline.close();

  try {
    pipeline.exec();
    fail("close should discard transaction");
  } catch (JedisDataException e) {
    assertTrue(e.getMessage().contains("EXEC without MULTI"));
    // pass
  }

  // it shouldn't meet any exception
  retFuture1.get();
  retFuture2.get();
}
 
Example 8
Source File: RedisEasyTest.java    From javabase with Apache License 2.0 4 votes vote down vote up
private static void pipleWithTransation(Pipeline pipeline) {
    for (int i = 0; i < KEY_COUNT; i++) {
        pipeline.set("pipletransation" + i, i + "");
    }
    pipeline.exec();
}
 
Example 9
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 4 votes vote down vote up
@Test(expected = JedisDataException.class)
public void pipelineExecShoudThrowJedisDataExceptionWhenNotInMulti() {
  Pipeline pipeline = jedis.pipelined();
  pipeline.exec();
}
 
Example 10
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 4 votes vote down vote up
@Test
public void testPipelinedTransactionResponse() {

  String key1 = "key1";
  String val1 = "val1";

  String key2 = "key2";
  String val2 = "val2";

  String key3 = "key3";
  String field1 = "field1";
  String field2 = "field2";
  String field3 = "field3";
  String field4 = "field4";

  String value1 = "value1";
  String value2 = "value2";
  String value3 = "value3";
  String value4 = "value4";

  Map<String, String> hashMap = new HashMap<String, String>();
  hashMap.put(field1, value1);
  hashMap.put(field2, value2);

  String key4 = "key4";
  Map<String, String> hashMap1 = new HashMap<String, String>();
  hashMap1.put(field3, value3);
  hashMap1.put(field4, value4);

  jedis.set(key1, val1);
  jedis.set(key2, val2);
  jedis.hmset(key3, hashMap);
  jedis.hmset(key4, hashMap1);

  Pipeline pipeline = jedis.pipelined();
  pipeline.multi();

  pipeline.get(key1);
  pipeline.hgetAll(key2);
  pipeline.hgetAll(key3);
  pipeline.get(key4);

  Response<List<Object>> response = pipeline.exec();
  pipeline.sync();

  List<Object> result = response.get();

  assertEquals(4, result.size());

  assertEquals("val1", result.get(0));

  assertTrue(result.get(1) instanceof JedisDataException);

  Map<String, String> hashMapReceived = (Map<String, String>) result.get(2);
  Iterator<String> iterator = hashMapReceived.keySet().iterator();
  String mapKey1 = iterator.next();
  String mapKey2 = iterator.next();
  assertFalse(iterator.hasNext());
  verifyHasBothValues(mapKey1, mapKey2, field1, field2);
  String mapValue1 = hashMapReceived.get(mapKey1);
  String mapValue2 = hashMapReceived.get(mapKey2);
  verifyHasBothValues(mapValue1, mapValue2, value1, value2);

  assertTrue(result.get(3) instanceof JedisDataException);
}