Redisson distributed lock integrated with @Scheduled doesn‘t guarantee a single job execution

12 篇文章 0 订阅

Redisson distributed lock integrated with @Scheduled doesn’t guarantee a single scheduled job execution at the same time

Here is a example below:
Thread.sleep(10000); // 如果不加改行代码,则该方法执行时间很短,比如 1ms ,那么 其他线程也是有机会重复执行的
因此,要想保证只有1个scheduled task 被执行,必须保证 在 其他线程获取锁失败,比如超时失败,或者其他的条件失败。
故:可以强制在 方法末尾 必须加上 Thread.sleep(10000);, 保证锁超时。
小伙伴们还有其他方法吗?

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = CameraApplication.class)
@Slf4j
public class RedissonTests {
    @Resource
    RedissonClient redissonClient;
    @Resource
    ExecutorService executorService;

    @Test
    public void testLock() {
        try {
            //Simulating cluster timer tasks with multiple threads
            for (int i = 1; i <= 5; i++) {
                int id = i;
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        RLock rLock = redissonClient.getLock("testLock");
                        try {
                            log.info(id + " Request Lock");
                            //Attempt to lock, wait up to 5 seconds, unlock automatically 10 seconds after lock
                            boolean lockRes = rLock.tryLock(5, 10, TimeUnit.SECONDS);
                            if (lockRes) {
                                try {
                                    log.info(id + " Application Successful,Processing business logic");
                                    Thread.sleep(10000); // 必须强制等待保证锁超时 ,那么 其他线程也就没机会重复执行了
                                } finally {
                                    //Is it locked and whether the current thread has acquired a lock
                                    if (rLock.isLocked() && rLock.isHeldByCurrentThread()) {
                                        rLock.unlock();
                                        log.info(id + " Release lock");
                                    }
                                }
                            } else {
                                log.info(id + " Failed to apply for lock=" + lockRes);
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
            Thread.sleep(500000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值