11package com .example .redisson ;
22
3+ import cn .hutool .core .exceptions .ExceptionUtil ;
34import org .redisson .api .RLock ;
45import org .redisson .api .RedissonClient ;
56import org .slf4j .Logger ;
1516 * https://zhuanlan.zhihu.com/p/128131107
1617 * https://zhuanlan.zhihu.com/p/130327922
1718 * 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
1820 *
1921 * @author wliduo[i@dolyw.com]
2022 * @date 2021/9/15 15:33
@@ -85,10 +87,10 @@ public void lock(String lockKey, long time, TimeUnit unit, VoidHandle handle) th
8587 * 加锁业务处理
8688 *
8789 * @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
9294 * @return void
9395 * @throws
9496 * @author wliduo[i@dolyw.com]
@@ -100,7 +102,13 @@ public void lock(String lockKey, boolean fair, long time, TimeUnit unit, VoidHan
100102 rLock .lock (time , unit );
101103 handle .execute ();
102104 } 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+ }
104112 }
105113 }
106114
@@ -142,7 +150,13 @@ public <T> T lock(String lockKey, boolean fair, long time, TimeUnit unit, Return
142150 rLock .lock (time , unit );
143151 return handle .execute ();
144152 } 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+ }
146160 }
147161 }
148162
@@ -161,7 +175,7 @@ public Boolean getTryLock(String lockKey) throws Exception {
161175 * 尝试获取锁
162176 *
163177 * @param lockKey
164- * @param fair 公平锁
178+ * @param fair 公平锁
165179 * @return java.lang.Boolean
166180 * @throws
167181 * @author wliduo[i@dolyw.com]
@@ -204,11 +218,11 @@ public void tryLock(String lockKey, long wait, long time, TimeUnit unit, VoidHan
204218 * 尝试加锁业务处理
205219 *
206220 * @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
212226 * @return void
213227 * @throws
214228 * @author wliduo[i@dolyw.com]
@@ -222,7 +236,13 @@ public void tryLock(String lockKey, boolean fair, long wait, long release, TimeU
222236 try {
223237 handle .execute ();
224238 } 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+ }
226246 }
227247 }
228248
@@ -249,11 +269,11 @@ public <T> T tryLock(String lockKey, long wait, long time, TimeUnit unit, Return
249269 * 返回值尝试加锁业务处理
250270 *
251271 * @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
257277 * @return T
258278 * @throws
259279 * @author wliduo[i@dolyw.com]
@@ -267,7 +287,13 @@ public <T> T tryLock(String lockKey, boolean fair, long wait, long release, Time
267287 try {
268288 return handle .execute ();
269289 } 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+ }
271297 }
272298 }
273299
0 commit comments