本文整理汇总了Java中redis.clients.jedis.Transaction.exec方法的典型用法代码示例。如果您正苦于以下问题:Java Transaction.exec方法的具体用法?Java Transaction.exec怎么用?Java Transaction.exec使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redis.clients.jedis.Transaction
的用法示例。
在下文中一共展示了Transaction.exec方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: hincrby
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
@Test public void hincrby() {
// GIVEN
Transaction tr = this.jedis.multi();
// WHEN
Response<Long> result1 = tr.hincrBy(KEY, FIELD, 3);
Response<Long> result2 = tr.hincrBy(KEY, FIELD, 4);
List<Object> results = tr.exec();
// THEN
String value = this.jedis.hget(KEY, FIELD);
assertEquals("7", value);
assertEquals(2, results.size());
assertEquals(3, (long) results.get(0));
assertEquals(7, (long) results.get(1));
assertEquals(3, (long) result1.get());
assertEquals(7, (long) result2.get());
}
示例2: testResetStateWithFullyExecutedTransaction
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
@Test
public void testResetStateWithFullyExecutedTransaction() {
Jedis jedis2 = new Jedis(jedis.getClient().getHost(), jedis.getClient().getPort());
jedis2.auth("foobared");
Transaction t = jedis2.multi();
t.set("mykey", "foo");
t.get("mykey");
List<Object> resp = t.exec();
assertNotNull(resp);
assertEquals(2, resp.size());
jedis2.resetState();
jedis2.close();
}
示例3: transactionResponseWithError
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
@Test
public void transactionResponseWithError() {
Transaction t = jedis.multi();
t.set("foo", "bar");
Response<Set<String>> error = t.smembers("foo");
Response<String> r = t.get("foo");
List<Object> l = t.exec();
assertEquals(JedisDataException.class, l.get(1).getClass());
try {
error.get();
fail("We expect exception here!");
} catch (JedisDataException e) {
// that is fine we should be here
}
assertEquals(r.get(), "bar");
}
示例4: select
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
@Test
public void select() {
jedis.select(1);
jedis.set("foo", "bar");
jedis.watch("foo");
Transaction t = jedis.multi();
t.select(0);
t.set("bar", "foo");
Jedis jedis2 = createJedis();
jedis2.select(1);
jedis2.set("foo", "bar2");
List<Object> results = t.exec();
if(results.isEmpty()){
results = null;
}
assertNull(results);
}
示例5: testCloseable
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
@Test
public void testCloseable() throws IOException {
// we need to test with fresh instance of Jedis
Jedis jedis2 = new Jedis(hnp.getHost(), hnp.getPort(), 500);
jedis2.auth("foobared");
Transaction transaction = jedis2.multi();
transaction.set("a", "1");
transaction.set("b", "2");
transaction.close();
try {
transaction.exec();
fail("close should discard transaction");
} catch (JedisDataException e) {
assertTrue(e.getMessage().contains("EXEC without MULTI"));
// pass
}
}
示例6: testExec
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
@Test
public void testExec() throws Exception {
int rnd = ThreadLocalRandom.current().nextInt(0, presetElements.size());
String key = presetElementKeys.get(rnd);
String value = String.valueOf(presetElements.get(key));
Transaction transaction = jedis.multi();
Snapshot snapshot = commandTracker.snapshot();
Snapshot discardSnapshot = discardTracker.snapshot();
Snapshot txsnapshot = execTracker.snapshot();
txsnapshot.increment();
Response<Long> added = transaction.sadd(key, value);
transaction.exec();
assertEquals(1, (long) added.get());
txsnapshot.validate();
snapshot.validate();
discardSnapshot.validate();
}
示例7: deleteMulti
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
@Override
public boolean deleteMulti(String... keys) {
Jedis jedis = null;
try {
jedis = pool.getResource();
Transaction trans = jedis.multi();
for (String key : keys) {
trans.del(key.getBytes());
}
trans.exec();
pool.returnResource(jedis);
} catch (Exception e) {
LOGGER.warn("Failed to delete key from cache {0}", keys, e);
pool.returnBrokenResource(jedis);
return false;
}
return true;
}
示例8: getAndRemZRange
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
public Set<String> getAndRemZRange(String key, long score) {
Jedis jedis = null;
try {
jedis = pool.getResource();
Transaction trans = jedis.multi();
trans.zrangeByScore(key.getBytes(), MIN_INF, SafeEncoder.encode(String.valueOf(score)));
trans.zremrangeByScore(key.getBytes(), MIN_INF, SafeEncoder.encode(String.valueOf(score)));
List<Object> response = trans.exec();
Set<byte[]> data = (Set<byte[]>) response.get(0);
Set<String> members = new LinkedHashSet<>(data.size());
for (byte[] d : data) {
members.add(new String(d));
}
pool.returnResource(jedis);
return members;
} catch (Exception e) {
LOGGER.warn("Failed to get zrem keys from cache {0}:{1}", key, score, e);
pool.returnBrokenResource(jedis);
throw e;
}
}
示例9: doRelease
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
@Override
protected void doRelease(String token) {
jedisClient.watch(lockName);
String currentToken = jedisClient.get(lockName);
if (currentToken == null){
jedisClient.unwatch();
return;
}
if (currentToken.equals(token)) {
Transaction t = jedisClient.multi();
t.del(lockName);
t.exec();
} else {
jedisClient.unwatch();
}
}
示例10: doExtend
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
@Override
protected boolean doExtend(final String token, long additionalTime){
jedisClient.watch(lockName);
String currentToken = jedisClient.get(lockName);
if (currentToken == null){
jedisClient.unwatch();
return false;
}
if (token.equals(currentToken)) {
long expiration = jedisClient.pttl(lockName);
if (expiration < 0) {
jedisClient.unwatch();
return false;
}
Transaction t = jedisClient.multi();
t.pexpire(lockName, expiration + additionalTime);
List response = t.exec();
return (!response.isEmpty()) && ((Long)response.get(0) == 1);
}
jedisClient.unwatch();
return false;
}
示例11: updateQueuedJob
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
@Override
public synchronized void updateQueuedJob(int jobId, JSONObject newJob) {
try (Jedis jedis = jedisPool.getResource()) {
jedis.select(redisDBIndex);
if (jedis.zcard(queue) > 0) {
Optional<Double> score = null;
Set<Tuple> tuples = jedis.zrangeByScoreWithScores(queue, 0, Double.MAX_VALUE);
String old = null;
for(Tuple data : tuples) {
JSONObject detail = JSONObject.parseObject(data.getElement());
int id = detail.getIntValue(ScheduledConstants.JOB_ID);
if(id == jobId) {
score = Optional.ofNullable(data.getScore());
old = data.getElement();
break;
}
}
if(score.isPresent()){
Transaction tx = jedis.multi();
tx.zrem(queue, old);
tx.zadd(queue, score.get(), newJob.toJSONString());
tx.exec();
}
}
}
}
示例12: execute
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
public void execute(TransactionCallBack rc) {
Jedis jedis = null;
Transaction transaction;
List<Object> object = null;
try {
jedis = RedisPool.getJedis();
transaction = jedis.multi();
rc.execute(transaction);
object = transaction.exec();
} catch (Exception e) {
log.error("执行redis操作异常" + e.getLocalizedMessage());
}
finally {
RedisPool.returnJedis(jedis);
}
}
示例13: taskLockVersionOne
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
/**
* 方案一的坏处:
* 假如在极端情况下,可能出现集群各个服务器同时执行到taskLockVersionOne,或者 doTask执行时间过长,
* 在redis事物还没提交的时候,会出现同时有多台服务器执行doTask。
* @param id
*/
private static void taskLockVersionOne(String id) {
String key = "default_task_id";
String value = jedis.get(key);
// 用redis 事物是防止 在set成功之后 在执行doTask或者其他情况导致程序终止没有执行到transaction.expire()
// 导致单台机器一直占着锁会有单点事故
Transaction transaction = null;
try {
transaction = jedis.multi();
if (value == null) {
transaction.set(key, id);
doTask(id);
// 设置过期是防止单点错误
transaction.expire(key, 30);
} else {
if (value.equals(id)) {
doTask(id);
}
}
} catch (Exception e) {
log.error("e" + e);
} finally {
transaction.exec();
}
}
示例14: exec
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
public List<Object> exec(long logIndex, Transaction transaction) {
String flag = getClassName() + ".exec";
List<Object> res = null;
Jedis jds = null;
try {
jds = jedisPool.getResource();
res = transaction.exec();
jedisPool.returnResource(jds);
return res;
} catch (Exception e) {
jedisPool.returnBrokenResource(jds);
FRCLogger.getInstance().warn(logIndex, flag, "exception:", e);
res = null;
return res;
}
}
示例15: main
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException {
ApplicationContext ac = new ClassPathXmlApplicationContext("root-context.xml");
JedisPool pool = (JedisPool) ac.getBean("jedisPool");
int count = 0;
while (true) {
Jedis jedis = pool.getResource();
jedis.get("keyName");
Transaction tx = jedis.multi();
tx.incr("keyName");
tx.expire("keyName", 10);
tx.exec();
jedis.close();
System.out.println("jedisPool active count:" + pool.getNumActive());
System.out.println("jedisPool idle count:" + pool.getNumIdle());
Thread.sleep(10);
}
}