//package com.mortals.pstation.tools.lock.service.impl;
//
//import com.mortals.pstation.tools.lock.config.ZookeeperConfiguration;
//import com.mortals.pstation.tools.lock.service.ILockService;
//import lombok.extern.apachecommons.CommonsLog;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.curator.framework.CuratorFramework;
//import org.apache.curator.framework.CuratorFrameworkFactory;
//import org.apache.curator.framework.recipes.locks.InterProcessMutex;
//import org.apache.curator.retry.RetryNTimes;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.PostConstruct;
//import java.util.concurrent.TimeUnit;
//
///**
// * @author karlhoo
// */
//@CommonsLog
//@Service("defaultLockService")
//@ConditionalOnBean(ZookeeperConfiguration.class)
//public class DefaultLockServiceImpl implements ILockService {
//    @Autowired
//    private ZookeeperConfiguration zookeeperConfiguration;
//    private CuratorFramework curatorClient;
//    private String LOCK_KEY_PREFIX;
//    private ThreadLocal<InterProcessMutex> interProcessMutexThreadLocal = new ThreadLocal<>();
//
//    @Override
//    public boolean lock(String lockKey) {
//        InterProcessMutex lock = new InterProcessMutex(getCuratorClient(), LOCK_KEY_PREFIX + (StringUtils.isBlank(lockKey) ? "/" : lockKey));
//        try {
//            if (lock.acquire(zookeeperConfiguration.getLockTimeoutSec(), TimeUnit.SECONDS)) {
//                interProcessMutexThreadLocal.set(lock);
//                return true;
//            }
//        } catch (Exception e) {
//            log.warn("获取分布式锁报错", e);
//        }
//        return false;
//    }
//
//    @Override
//    public void unlock() {
//        try {
//            interProcessMutexThreadLocal.get().release();
//        } catch (Exception e) {
//            log.warn("释放分布式锁报错", e);
//        } finally {
//            interProcessMutexThreadLocal.remove();
//        }
//    }
//
//    private CuratorFramework getCuratorClient() {
//        if (curatorClient == null) {
//            this.curatorClient = CuratorFrameworkFactory.newClient(
//                    zookeeperConfiguration.getZookeeperNodes(), zookeeperConfiguration.getSessionTimeoutMs(),
//                    zookeeperConfiguration.getConnectionTimeoutMs(), new RetryNTimes(zookeeperConfiguration.getMaxRetry(), zookeeperConfiguration.getRetryIntervalMs()));
//            this.curatorClient.start();
//        }
//        return this.curatorClient;
//    }
//
//    @PostConstruct
//    public void init() {
//        StringBuilder lockKeyPrefixBuilder = new StringBuilder("/sms/lock/");
//        if (StringUtils.isNotEmpty(zookeeperConfiguration.getUserMark())) {
//            lockKeyPrefixBuilder.append(zookeeperConfiguration.getUserMark()).append("/");
//        }
//        if (StringUtils.isNotEmpty(zookeeperConfiguration.getConfigProfile())) {
//            lockKeyPrefixBuilder.append(zookeeperConfiguration.getConfigProfile()).append("/");
//        }
//        LOCK_KEY_PREFIX = lockKeyPrefixBuilder.toString();
//    }
//}
//
