Blame view

java110-utils/src/main/java/com/java110/utils/lock/DistributedLock.java 2.72 KB
88e030b7   王彪总   init project
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  package com.java110.utils.lock;
  
  import com.java110.utils.cache.BaseCache;
  import com.java110.utils.cache.Jedis;
  
  import java.util.Collections;
  import java.util.UUID;
  
  /**
   * 分布式事务锁
   * add by wuxw 2020-01-11
   */
  public class DistributedLock extends BaseCache {
  
      private static final String LOCK_SUCCESS = "OK";
      private static final String SET_IF_NOT_EXIST = "NX";
      private static final String SET_WITH_EXPIRE_TIME = "PX";
      private static final int DEFAULT_EXPIRE_TIME = 1000;
  
  
      private static final Long RELEASE_SUCCESS = 1L;
  
      /**
       * 获取UUID
       *
       * @return
       */
      public static String getLockUUID() {
          return UUID.randomUUID().toString();
      }
  
      /**
       * 等待获取锁
       *
       * @param lockKey   
       * @param requestId 请求标识
       * @return
       */
      public static boolean waitGetDistributedLock(String lockKey, String requestId) {
          return waitGetDistributedLock(lockKey, requestId, DEFAULT_EXPIRE_TIME);
      }
  
      public static boolean waitGetDistributedLock(String lockKey, String requestId, int expireTime) {
          Jedis redis = null;
          try {
              redis = getJedis();
              while (true) {
                  if (tryGetDistributedLock(redis, lockKey, requestId, expireTime)) {
                      return true;
                  }
              }
          } finally {
              if (redis != null) {
                  redis.close();
              }
          }
      }
  
  
      /**
       * 尝试获取分布式锁
       *
       * @param lockKey    
       * @param requestId  请求标识
       * @param expireTime 超期时间
       * @return 是否获取成功
       */
      private static boolean tryGetDistributedLock(Jedis redis, String lockKey, String requestId, int expireTime) {
          String result = redis.set(lockKey, requestId, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime);
  
          if (LOCK_SUCCESS.equals(result)) {
              return true;
          }
          return false;
  
      }
  
      /**
       * 释放分布式锁
       *
       * @param lockKey   
       * @param requestId 请求标识
       * @return 是否释放成功
       */
      public static boolean releaseDistributedLock(String lockKey, String requestId) {
          Jedis redis = null;
          try {
              redis = getJedis();
              String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
              Object result = redis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId));
              if (RELEASE_SUCCESS.equals(result)) {
                  return true;
              }
              return false;
          } finally {
              if (redis != null) {
                  redis.close();
              }
          }
      }
  }