当前位置: 首页>>代码示例>>Java>>正文


Java RedissonClient.getLock方法代码示例

本文整理汇总了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();
}
 
开发者ID:redisson,项目名称:redisson-examples,代码行数:23,代码来源:LockExamples.java

示例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);
}
 
开发者ID:SevenSpikes,项目名称:shopify-api-java-wrapper,代码行数:30,代码来源:ShopifyJacksonDecoder.java

示例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());
                }
            }
        }
    }
}
 
开发者ID:SevenSpikes,项目名称:shopify-api-java-wrapper,代码行数:68,代码来源:RequestLimitInterceptor.java


注:本文中的org.redisson.api.RedissonClient.getLock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。