• 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

  • 相关阅读:
    聊聊mysql中的int(1)
    如何有效防止sql注入
    微信小程序WXML页面常用语法(讲解+示例)
    Spring Boot 2.x基础教程:使用集中式缓存Redis
    基于.NetCore3.1系列 —— 日志记录之自定义日志组件
    精讲RestTemplate第10篇-使用代理作为跳板发送请求
    使用Java API进行tar.gz文件及文件夹压缩解压缩
    精讲RestTemplate第9篇-如何通过HTTP Basic Auth认证
    精讲RestTemplate第8篇-请求失败自动重试机制
    精讲RestTemplate第7篇-自定义请求失败异常处理
  • 原文地址:https://www.cnblogs.com/winner-0715/p/7298935.html
Copyright © 2020-2023  润新知