1
1
package com .example .redisson ;
2
2
3
+ import cn .hutool .core .exceptions .ExceptionUtil ;
3
4
import org .redisson .api .RLock ;
4
5
import org .redisson .api .RedissonClient ;
5
6
import org .slf4j .Logger ;
15
16
* https://zhuanlan.zhihu.com/p/128131107
16
17
* https://zhuanlan.zhihu.com/p/130327922
17
18
* https://gitee.com/zlt2000/microservices-platform/tree/master/zlt-commons/zlt-common-core/src/main/java/com/central/common/lock
19
+ * https://www.cnblogs.com/luao/p/14633264.html
18
20
*
19
21
* @author wliduo[i@dolyw.com]
20
22
* @date 2021/9/15 15:33
@@ -85,10 +87,10 @@ public void lock(String lockKey, long time, TimeUnit unit, VoidHandle handle) th
85
87
* 加锁业务处理
86
88
*
87
89
* @param lockKey
88
- * @param fair
89
- * @param time
90
- * @param unit
91
- * @param handle
90
+ * @param fair
91
+ * @param time
92
+ * @param unit
93
+ * @param handle
92
94
* @return void
93
95
* @throws
94
96
* @author wliduo[i@dolyw.com]
@@ -100,7 +102,13 @@ public void lock(String lockKey, boolean fair, long time, TimeUnit unit, VoidHan
100
102
rLock .lock (time , unit );
101
103
handle .execute ();
102
104
} finally {
103
- rLock .unlock ();
105
+ try {
106
+ if (rLock .isLocked () && rLock .isHeldByCurrentThread ()) {
107
+ rLock .unlock ();
108
+ }
109
+ } catch (Exception e ) {
110
+ logger .warn ("释放锁Key:{},异常:{}" , lockKey , ExceptionUtil .stacktraceToOneLineString (e ), e );
111
+ }
104
112
}
105
113
}
106
114
@@ -142,7 +150,13 @@ public <T> T lock(String lockKey, boolean fair, long time, TimeUnit unit, Return
142
150
rLock .lock (time , unit );
143
151
return handle .execute ();
144
152
} finally {
145
- rLock .unlock ();
153
+ try {
154
+ if (rLock .isLocked () && rLock .isHeldByCurrentThread ()) {
155
+ rLock .unlock ();
156
+ }
157
+ } catch (Exception e ) {
158
+ logger .warn ("释放锁Key:{},异常:{}" , lockKey , ExceptionUtil .stacktraceToOneLineString (e ), e );
159
+ }
146
160
}
147
161
}
148
162
@@ -161,7 +175,7 @@ public Boolean getTryLock(String lockKey) throws Exception {
161
175
* 尝试获取锁
162
176
*
163
177
* @param lockKey
164
- * @param fair 公平锁
178
+ * @param fair 公平锁
165
179
* @return java.lang.Boolean
166
180
* @throws
167
181
* @author wliduo[i@dolyw.com]
@@ -204,11 +218,11 @@ public void tryLock(String lockKey, long wait, long time, TimeUnit unit, VoidHan
204
218
* 尝试加锁业务处理
205
219
*
206
220
* @param lockKey
207
- * @param fair
208
- * @param wait
209
- * @param release
210
- * @param unit
211
- * @param handle
221
+ * @param fair
222
+ * @param wait
223
+ * @param release
224
+ * @param unit
225
+ * @param handle
212
226
* @return void
213
227
* @throws
214
228
* @author wliduo[i@dolyw.com]
@@ -222,7 +236,13 @@ public void tryLock(String lockKey, boolean fair, long wait, long release, TimeU
222
236
try {
223
237
handle .execute ();
224
238
} finally {
225
- rLock .unlock ();
239
+ try {
240
+ if (rLock .isLocked () && rLock .isHeldByCurrentThread ()) {
241
+ rLock .unlock ();
242
+ }
243
+ } catch (Exception e ) {
244
+ logger .warn ("释放锁Key:{},异常:{}" , lockKey , ExceptionUtil .stacktraceToOneLineString (e ), e );
245
+ }
226
246
}
227
247
}
228
248
@@ -249,11 +269,11 @@ public <T> T tryLock(String lockKey, long wait, long time, TimeUnit unit, Return
249
269
* 返回值尝试加锁业务处理
250
270
*
251
271
* @param lockKey
252
- * @param fair
253
- * @param wait
254
- * @param release
255
- * @param unit
256
- * @param handle
272
+ * @param fair
273
+ * @param wait
274
+ * @param release
275
+ * @param unit
276
+ * @param handle
257
277
* @return T
258
278
* @throws
259
279
* @author wliduo[i@dolyw.com]
@@ -267,7 +287,13 @@ public <T> T tryLock(String lockKey, boolean fair, long wait, long release, Time
267
287
try {
268
288
return handle .execute ();
269
289
} finally {
270
- rLock .unlock ();
290
+ try {
291
+ if (rLock .isLocked () && rLock .isHeldByCurrentThread ()) {
292
+ rLock .unlock ();
293
+ }
294
+ } catch (Exception e ) {
295
+ logger .warn ("释放锁Key:{},异常:{}" , lockKey , ExceptionUtil .stacktraceToOneLineString (e ), e );
296
+ }
271
297
}
272
298
}
273
299
0 commit comments