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

com.github.dennisit.vplus.data.lock.zookeeper.ZookeeperDistributeLock Maven / Gradle / Ivy

/*--------------------------------------------------------------------------
 *  Copyright (c) 2010-2020, Elon.su All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the elon developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: Elon.su, you can also mail [email protected]
 *--------------------------------------------------------------------------
*/
package com.github.dennisit.vplus.data.lock.zookeeper;

import com.github.dennisit.vplus.data.criteria.RetryCriteria;
import com.github.dennisit.vplus.data.lock.DistributeLock;
import com.github.dennisit.vplus.data.criteria.action.RetryAction;
import org.apache.commons.lang3.StringUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * Created by Elon.su on 17/11/6.
 */
public class ZookeeperDistributeLock implements DistributeLock{

    private static final Logger LOG = LoggerFactory.getLogger(ZookeeperDistributeLock.class);

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

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

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


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


    @Override
    public boolean tryLock(String key, int tryMilliSeconds) {
        try {
            String zkNode = builderLockKey(key);
            interProcessMutex = new InterProcessMutex(curatorFramework, zkNode);
            return interProcessMutex.acquire(tryMilliSeconds, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(),e);
        }
    }


    @Override
    public void unlock(String key) {
        RetryAction.doWithTry(new RetryCriteria() {
            @Override
            public String handle() {
                String zkNode = builderLockKey(key);
                try {
                    interProcessMutex.release();
                } catch (Exception e) {
                    LOG.error("释放分布式锁" + zkNode, e);
                }
                return zkNode;
            }
        });
    }

    /**
     * 锁的ID,对应zk一个PERSISTENT节点,下挂EPHEMERAL节点.
     * 构建zk分布式锁Key
     * @param key
     * @return
     */
    private String builderLockKey(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