69.安心技术梳理 - Redis实现【分布式锁】工具,含超时时间/等待时间干货

1.创建引入一个Redis工具方法类

2.实现工具 - redisClient为工具

String lockKey = "key"; 1000L后获取锁,20000L维持锁存在总时长
DistributedLocker.instance(redisClient)
        .lock(lockKey, 1000L, 20000L,
                () -> this.method(提交方法));

实现工具类:

import com.RedisClient;
import com.utils.RetryUtils;

import java.util.concurrent.TimeUnit;

//分布式锁
public class DistributedLocker {

    private static final Long DEFAULT_WAIT_TIME = 1000L;
    private static final Long DEFAULT_EXPIRE_TIME = 10000L;

    private RedisClient redisClient;

    private DistributedLocker(RedisClient redisClient) throws DistributedLockException {
        if (redisClient == null) {
            throw new DistributedLockException("Distributed lock, redisClient must not null");
        }
        this.redisClient = redisClient;
    }

    public static DistributedLocker instance(RedisClient redisClient) {
        return new DistributedLocker(redisClient);
    }

    public  T lock(String key, Long waitTimeMilliseconds, Long expireTimeMilliseconds, LockCallback callback) {
        if (waitTimeMilliseconds == null) {
            waitTimeMilliseconds = DEFAULT_WAIT_TIME;
        }
        if (expireTimeMilliseconds == null) {
            expireTimeMilliseconds = DEFAULT_EXPIRE_TIME;
        }
        paramCheck(key, waitTimeMilliseconds, expireTimeMilliseconds, callback);
        boolean success = false;
        try {
            long interval = 10L;
            long tryTimes = waitTimeMilliseconds / interval;
            do {
                success = redisClient.set(key, "1", expireTimeMilliseconds, TimeUnit.MILLISECONDS, false);
                if (success) {
                    return callback.call();
                } else {
                    try {
                        Thread.sleep(interval);
                    } catch (InterruptedException e) {
                        //do nothing
                    }
                }
            } while (--tryTimes > 0);
        } finally {
            if (success) {
                RetryUtils.retryQuietly(3, () -> redisClient.del(key));
            }
        }
        return null;
    }

    private  void paramCheck(String key, Long waitTimeMilliseconds, Long expireTimeMilliseconds,
                                LockCallback callback) {
        if (key == null || key.trim().length() == 0) {
            throw new DistributedLockException("Distributed lock, key can not null");
        }
        if (waitTimeMilliseconds <= 0) {
            throw new DistributedLockException("Distributed lock, waitTimeMilliseconds must larger than 0");
        }
        if (expireTimeMilliseconds <= 0) {
            throw new DistributedLockException("Distributed lock, expireTimeMilliseconds must larger than 0");
        }
        if (callback == null) {
            throw new DistributedLockException("Distributed lock, callback must not null");
        }
    }

}

辅助工具1

public class DistributedLockException extends RuntimeException {

    public DistributedLockException(String msg) {
        super(msg);
    }
}

辅助工具2

//回调函数

public interface LockCallback {

    T call();
}

你可能感兴趣的:(安心技术,java)