本文整理汇总了Java中org.redisson.api.RedissonClient.getLock方法的典型用法代码示例。如果您正苦于以下问题:Java RedissonClient.getLock方法的具体用法?Java RedissonClient.getLock怎么用?Java RedissonClient.getLock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.redisson.api.RedissonClient
的用法示例。
在下文中一共展示了RedissonClient.getLock方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.redisson.api.RedissonClient; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException {
// connects to 127.0.0.1:6379 by default
RedissonClient redisson = Redisson.create();
RLock lock = redisson.getLock("lock");
lock.lock(2, TimeUnit.SECONDS);
Thread t = new Thread() {
public void run() {
RLock lock1 = redisson.getLock("lock");
lock1.lock();
lock1.unlock();
};
};
t.start();
t.join();
lock.unlock();
redisson.shutdown();
}
示例2: decode
import org.redisson.api.RedissonClient; //导入方法依赖的package包/类
@Override
public Object decode(Response response, Type type) throws IOException
{
Collection<String> shopifyApiCallLimitHeader = response.headers().get("HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT");
String[] callLimitValues = shopifyApiCallLimitHeader.iterator().next().split("/");
if(callLimitValues[0] != null && callLimitValues[0] != "")
{
Long createdCalls = Long.parseLong(callLimitValues[0]);
Long remainingCalls = _shopifyRedissonManager.calculateAvalableCredits(createdCalls);
RedissonClient redisson = _shopifyRedissonManager.getRedissonClient();
// Lock per shopify store. The lock is distributed, so it will work for multiple threads and applications.
RLock lock = redisson.getLock(_shopifyRedissonManager.getMyShopifyUrl());
RAtomicLong remainingCreditsAtomic = redisson.getAtomicLong(_shopifyRedissonManager.getRemainingCreditsKey());
RAtomicLong lastRequestTimeAtomic = redisson.getAtomicLong(_shopifyRedissonManager.getLastRequestTimeKey());
remainingCreditsAtomic.set(remainingCalls);
lastRequestTimeAtomic.set(System.currentTimeMillis());
lock.unlock();
}
return super.decode(response, type);
}
示例3: apply
import org.redisson.api.RedissonClient; //导入方法依赖的package包/类
@Override
public void apply(RequestTemplate template)
{
Boolean tryGetCredit = true;
RedissonClient redisson = _shopifyRedissonManager.getRedissonClient();
while (tryGetCredit)
{
// Lock per shopify store. The lock is distributed, so it will work for multiple threads and applications.
RLock lock = redisson.getLock(_shopifyRedissonManager.getMyShopifyUrl());
RAtomicLong isDefaultRemainingCreditsValueSet = redisson.getAtomicLong(_shopifyRedissonManager.getIsDefaultRemainingCreditsValueSetKey());
RAtomicLong remainingCreditsAtomic = redisson.getAtomicLong(_shopifyRedissonManager.getRemainingCreditsKey());
if(isDefaultRemainingCreditsValueSet.get() == 0)
{
remainingCreditsAtomic.set(_shopifyRedissonManager.getCreditLimit());
isDefaultRemainingCreditsValueSet.set(1);
}
RAtomicLong lastRequestTimeAtomic = redisson.getAtomicLong(_shopifyRedissonManager.getLastRequestTimeKey());
Long remainingCredits = remainingCreditsAtomic.get();
if(remainingCredits > 0)
{
// These values are set here, because a request can be made while the current request is still in progress.
// We set the actual values inside the decoder (when the request is complete), but if we don't set them here
// as well a raised condition can occur.
remainingCreditsAtomic.set(remainingCredits - 1);
lastRequestTimeAtomic.set(System.currentTimeMillis());
tryGetCredit = false;
lock.unlock();
}
else
{
// Check if there were enough time since the last request time.
// If the latest request's remaining calls were 0 and no calls were made after that, the remaining credits
// will not be updated. This is why the last request time is used as well.
long availableCalls = (long)Math.floor((System.currentTimeMillis() - lastRequestTimeAtomic.get())/500);
if(availableCalls > 0)
{
remainingCreditsAtomic.set(availableCalls - 1);
lastRequestTimeAtomic.set(System.currentTimeMillis());
tryGetCredit = false;
lock.unlock();
}
else
{
lock.unlock();
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println("Error while waiting for available Shopify call credit. " + e.getMessage());
}
}
}
}
}