1
1
/*
2
- * Copyright 2014-2019 the original author or authors.
2
+ * Copyright 2014-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import java .text .SimpleDateFormat ;
20
20
import java .util .Collections ;
21
21
import java .util .Date ;
22
- import java .util .Iterator ;
23
22
import java .util .Map ;
24
23
import java .util .UUID ;
25
24
import java .util .concurrent .ConcurrentHashMap ;
@@ -116,6 +115,8 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl
116
115
*/
117
116
private boolean executorExplicitlySet ;
118
117
118
+ private volatile boolean unlinkAvailable = true ;
119
+
119
120
/**
120
121
* Constructs a lock registry with the default (60 second) lock expiration.
121
122
* @param connectionFactory The connection factory.
@@ -160,15 +161,12 @@ public Lock obtain(Object lockKey) {
160
161
161
162
@ Override
162
163
public void expireUnusedOlderThan (long age ) {
163
- Iterator <Map .Entry <String , RedisLock >> iterator = this .locks .entrySet ().iterator ();
164
164
long now = System .currentTimeMillis ();
165
- while (iterator .hasNext ()) {
166
- Map .Entry <String , RedisLock > entry = iterator .next ();
167
- RedisLock lock = entry .getValue ();
168
- if (now - lock .getLockedAt () > age && !lock .isAcquiredInThisProcess ()) {
169
- iterator .remove ();
170
- }
171
- }
165
+ this .locks .entrySet ()
166
+ .removeIf ((entry ) -> {
167
+ RedisLock lock = entry .getValue ();
168
+ return now - lock .getLockedAt () > age && !lock .isAcquiredInThisProcess ();
169
+ });
172
170
}
173
171
174
172
@ Override
@@ -184,16 +182,14 @@ private final class RedisLock implements Lock {
184
182
185
183
private final ReentrantLock localLock = new ReentrantLock ();
186
184
187
- private volatile boolean unlinkAvailable = true ;
188
-
189
185
private volatile long lockedAt ;
190
186
191
187
private RedisLock (String path ) {
192
188
this .lockKey = constructLockKey (path );
193
189
}
194
190
195
191
private String constructLockKey (String path ) {
196
- return RedisLockRegistry .this .registryKey + ":" + path ;
192
+ return RedisLockRegistry .this .registryKey + ':' + path ;
197
193
}
198
194
199
195
public long getLockedAt () {
@@ -331,14 +327,20 @@ public void unlock() {
331
327
}
332
328
333
329
private void removeLockKey () {
334
- if (this .unlinkAvailable ) {
330
+ if (RedisLockRegistry . this .unlinkAvailable ) {
335
331
try {
336
332
RedisLockRegistry .this .redisTemplate .unlink (this .lockKey );
337
333
}
338
334
catch (Exception ex ) {
339
- LOGGER .warn ("The UNLINK command has failed (not supported on the Redis server?); " +
340
- "falling back to the regular DELETE command" , ex );
341
- this .unlinkAvailable = false ;
335
+ RedisLockRegistry .this .unlinkAvailable = false ;
336
+ if (LOGGER .isDebugEnabled ()) {
337
+ LOGGER .debug ("The UNLINK command has failed (not supported on the Redis server?); " +
338
+ "falling back to the regular DELETE command" , ex );
339
+ }
340
+ else {
341
+ LOGGER .warn ("The UNLINK command has failed (not supported on the Redis server?); " +
342
+ "falling back to the regular DELETE command: " + ex .getMessage ());
343
+ }
342
344
RedisLockRegistry .this .redisTemplate .delete (this .lockKey );
343
345
}
344
346
}
@@ -359,7 +361,7 @@ public boolean isAcquiredInThisProcess() {
359
361
360
362
@ Override
361
363
public String toString () {
362
- SimpleDateFormat dateFormat = new SimpleDateFormat ("YYYY -MM-dd@HH:mm:ss.SSS" );
364
+ SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy -MM-dd@HH:mm:ss.SSS" );
363
365
return "RedisLock [lockKey=" + this .lockKey
364
366
+ ",lockedAt=" + dateFormat .format (new Date (this .lockedAt ))
365
367
+ ", clientId=" + RedisLockRegistry .this .clientId
0 commit comments