使用 RedissonClient 进行加锁

RLock lock = redissonClient.getLock(LOCK_KEY);
try {
    if (!lock.tryLock(Constants.TEN, TimeUnit.SECONDS)) {
        throw new BusinessException("访问频繁请稍后再试");
    }
    doSomething();
} catch (Exception e) {
    log.error("系统异常");
} finally {
    if (lock.isLocked() && lock.isHeldByCurrentThread()) {
        lock.unlock();
    }
}
posted @ 2021-05-17 11:02  梦见舟  阅读(4173)  评论(0编辑  收藏  举报