本文整理汇总了Java中redis.clients.jedis.Transaction.incr方法的典型用法代码示例。如果您正苦于以下问题:Java Transaction.incr方法的具体用法?Java Transaction.incr怎么用?Java Transaction.incr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redis.clients.jedis.Transaction
的用法示例。
在下文中一共展示了Transaction.incr方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
}
示例2: send
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
/**
* This method takes a RedisMessage and pushes it onto the topic specified by the message.
*
* @param message message to push onto RedisMQ
*/
public void send(RedisMessage message) throws IOException {
Preconditions.checkNotNull(message);
List<Object> result = null;
int tries = 0;
String topic = message.getTopic();
String nextIndexKey = RedisMQUtils.getNextIndexForTopicKey(topic);
long nextIndex = 1;
// Only try sending three times. The result object will not be null if the exec() command
// succeeds.
while ((result == null || result.size() == 0) && tries < 3) {
if (jedis.exists(nextIndexKey)) {
nextIndex = Long.parseLong(RedisMQUtils.jedisGetString(jedis, nextIndexKey)) + 1;
}
String messageKey = RedisMQUtils.getMessageKey(topic, Long.toString(nextIndex));
// If the nextIndexKey or messageKey change then the transaction should fail
jedis.watch(nextIndexKey, messageKey);
Transaction t = jedis.multi();
t.incr(nextIndexKey);
t.set(messageKey.getBytes(), message.getPayload());
result = t.exec();
tries++;
}
if (result == null || result.size() == 0) {
throw new RuntimeException("Attempted to send 3 times. Could not obtain lock.");
}
}
示例3: poll
import redis.clients.jedis.Transaction; //导入方法依赖的package包/类
/**
* This method polls the given topic for a message. Optional.absent() is returned when no new message is found.
*
* @param topic the topic to poll for a message
* @return the retrieved message
*/
public Optional<byte[]> poll(String topic) throws IOException {
Preconditions.checkArgument(!Strings.isNullOrEmpty(topic));
Optional<byte[]> result = Optional.absent();
String currentIndexKey = RedisMQUtils.getNextIndexForTopicKey(topic);
long endMillis = System.currentTimeMillis() + timeout;
// Continue to poll the queue until the timeout period ends
while (System.currentTimeMillis() < endMillis) {
// If the current index does not exist, then the topic does not exist
if (jedis.exists(currentIndexKey)) {
long currentIndex = Long.parseLong(RedisMQUtils.jedisGetString(jedis, currentIndexKey));
String consumerIndexKey = RedisMQUtils.getNextIndexForGroupIdKey(topic, groupId);
// Watch the consumer index to make sure that we don't increment twice when only looking for one
// message
jedis.watch(consumerIndexKey);
long consumerIndex = 0;
if (jedis.exists(consumerIndexKey)) {
consumerIndex = Long.parseLong(RedisMQUtils.jedisGetString(jedis, consumerIndexKey));
}
// Check to make sure the current message index is higher than the group index, signifying that there
// are new messages on the queue
if (currentIndex > consumerIndex) {
Transaction t = jedis.multi();
t.incr(consumerIndexKey);
List<Object> execResult = t.exec();
// If the transaction succeeded it means that the key was incremented and we can consume a message
if (execResult != null && execResult.size() > 0) {
consumerIndex = (Long)execResult.get(0);
String key = RedisMQUtils.getMessageKey(topic, Long.toString(consumerIndex));
byte[] keyBytes = key.getBytes();
if (jedis.exists(keyBytes)) {
byte[] payload = RedisMQUtils.jedisGetBytes(jedis, keyBytes);
result = Optional.of(payload);
break;
}
}
}
}
}
return result;
}