• Redis分布式锁实现


    redis分布式锁解决多个应用进程间同步操作

    import java.util.List;
    import java.util.UUID;
    
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.Transaction;
    import redis.clients.jedis.exceptions.JedisException;
    
    /**
     * Jedis实现分布式锁
     *
     */
    public class DistributionLock {
        private final JedisPool jedisPool;
    
        public DistributionLock(JedisPool jedisPool) {
            this.jedisPool = jedisPool;
        }
    
        /**
         * 获取分布式锁
         * 
         * @param lockName
         *            竞争获取锁key
         * @param acquireTimeoutInMS
         *            获取锁超时时间
         * @param lockTimeoutInMS
         *            锁的超时时间
         * @return 获取锁标识
         */
        public String acquireLockWithTimeout(String lockName,
                long acquireTimeoutInMS, long lockTimeoutInMS) {
            Jedis conn = null;
            boolean broken = false;
            String retIdentifier = null;
            try {
                conn = jedisPool.getResource();
                String identifier = UUID.randomUUID().toString();
                String lockKey = "lock:" + lockName;
                int lockExpire = (int) (lockTimeoutInMS / 1000);
    
                long end = System.currentTimeMillis() + acquireTimeoutInMS;
                while (System.currentTimeMillis() < end) {
                    if (conn.setnx(lockKey, identifier) == 1) {
                        conn.expire(lockKey, lockExpire);
                        retIdentifier = identifier;
                    }
                    if (conn.ttl(lockKey) == -1) {
                        conn.expire(lockKey, lockExpire);
                    }
    
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException ie) {
                        Thread.currentThread().interrupt();
                    }
                }
            } catch (JedisException je) {
                if (conn != null) {
                    broken = true;
                    jedisPool.returnBrokenResource(conn);
                }
            } finally {
                if (conn != null && !broken) {
                    jedisPool.returnResource(conn);
                }
            }
            return retIdentifier;
        }
    
        /**
         * 释放锁
         * @param lockName 竞争获取锁key
         * @param identifier 释放锁标识
         * @return
         */
        public boolean releaseLock(String lockName, String identifier) {
            Jedis conn = null;
            boolean broken = false;
            String lockKey = "lock:" + lockName;
            boolean retFlag = false;
            try {
                conn = jedisPool.getResource();
                while (true) {
                    conn.watch(lockKey);
                    if (identifier.equals(conn.get(lockKey))) {
                        Transaction trans = conn.multi();
                        trans.del(lockKey);
                        List<Object> results = trans.exec();
                        if (results == null) {
                            continue;
                        }
                        retFlag = true;
                    }
                    conn.unwatch();
                    break;
                }
    
            } catch (JedisException je) {
                if (conn != null) {
                    broken = true;
                    jedisPool.returnBrokenResource(conn);
                }
            } finally {
                if (conn != null && !broken) {
                    jedisPool.returnResource(conn);
                }
            }
            return retFlag;
        }
    
    }

     

     

    参考:

    http://www.cnblogs.com/wuhuajun/p/5242644.html

  • 相关阅读:
    指针与数组的区别 —— 《C语言深度剖析》读书心得
    console ouput 与 重定向输出 效率对比
    First day in 阿里
    TestNG 使用入门教程
    Spring简单使用简介
    玩转Spring JUnit+mockito+powermock单元测试(使用详解)
    Spring Boot Junit 单元测试详解
    spring @Value注入map、List、Bean、static变量方式及详细使用
    单元测试Junit使用详解
    Mockito & PowerMock详解
  • 原文地址:https://www.cnblogs.com/winner-0715/p/7298935.html
Copyright © 2020-2023  润新知