All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.spring.boxes.redis.lock.zookeeper.ZookeeperLockTemplate Maven / Gradle / Ivy

The newest version!
package com.spring.boxes.redis.lock.zookeeper;

import com.github.rholder.retry.*;
import com.spring.boxes.redis.lock.support.AbstractLockTemplate;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;

import java.util.concurrent.TimeUnit;

@Slf4j
public class ZookeeperLockTemplate extends AbstractLockTemplate {

    /**
     * 所有PERSISTENT锁节点的根位置
     */
    public static final String DEFAULT_LOCK_ROOT_PATH = "/ROOT_DISTRIBUTE_LOCK/";

    /**
     * zk 共享锁实现
     */
    private InterProcessMutex interProcessMutex = null;

    /**
     * zk的客户端
     */
    private final CuratorFramework curatorFramework;

    // 重试器
    private Retryer retry = RetryerBuilder
            .newBuilder().retryIfException()
            .retryIfException()
            .withBlockStrategy(BlockStrategies.threadSleepStrategy())
            .withStopStrategy(StopStrategies.stopAfterAttempt(2))
            .withWaitStrategy(WaitStrategies.fixedWait(50L, TimeUnit.MILLISECONDS)).build();

    public ZookeeperLockTemplate(CuratorFramework curatorFramework) {
        this.curatorFramework = curatorFramework;
    }

    @Override
    public boolean tryLock(String lockKey, long waitTime, long leaseTime) {
        try {
            String zkNode = toLockKey(lockKey);
            interProcessMutex = new InterProcessMutex(curatorFramework, zkNode);
            return interProcessMutex.acquire(waitTime, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @Override
    public void unlock(String lockKey) {
        try {
            retry.call(() -> {
                String zkNode = toLockKey(lockKey);
                interProcessMutex = new InterProcessMutex(curatorFramework, zkNode);
                interProcessMutex.release();
                return true;
            });
        } catch (Exception e) {
            String error = String.format("UNLOCK FAILED: key=%s", lockKey);
            throw new IllegalStateException(error, e);
        }
    }

    @Override
    public boolean isLocked(String lockKey) {
        String zkNode = toLockKey(lockKey);
        interProcessMutex = new InterProcessMutex(curatorFramework, zkNode);
        return interProcessMutex.isAcquiredInThisProcess();
    }

    @Override
    public boolean isHeldByCurrentThread(String lockKey) {
        String zkNode = toLockKey(lockKey);
        interProcessMutex = new InterProcessMutex(curatorFramework, zkNode);
        return interProcessMutex.isOwnedByCurrentThread();
    }

    protected String toLockKey(String key) {
        if (StringUtils.isBlank(key)) {
            throw new NullPointerException("分布式锁Key为空");
        }
        return DEFAULT_LOCK_ROOT_PATH + StringUtils.trim(key);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy