11/*
2- * Copyright 2014-2019 the original author or authors.
2+ * Copyright 2014-2020 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1919import java .text .SimpleDateFormat ;
2020import java .util .Collections ;
2121import java .util .Date ;
22- import java .util .Iterator ;
2322import java .util .Map ;
2423import java .util .UUID ;
2524import java .util .concurrent .ConcurrentHashMap ;
@@ -116,6 +115,8 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl
116115 */
117116 private boolean executorExplicitlySet ;
118117
118+ private volatile boolean unlinkAvailable = true ;
119+
119120 /**
120121 * Constructs a lock registry with the default (60 second) lock expiration.
121122 * @param connectionFactory The connection factory.
@@ -160,15 +161,12 @@ public Lock obtain(Object lockKey) {
160161
161162 @ Override
162163 public void expireUnusedOlderThan (long age ) {
163- Iterator <Map .Entry <String , RedisLock >> iterator = this .locks .entrySet ().iterator ();
164164 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+ });
172170 }
173171
174172 @ Override
@@ -184,16 +182,14 @@ private final class RedisLock implements Lock {
184182
185183 private final ReentrantLock localLock = new ReentrantLock ();
186184
187- private volatile boolean unlinkAvailable = true ;
188-
189185 private volatile long lockedAt ;
190186
191187 private RedisLock (String path ) {
192188 this .lockKey = constructLockKey (path );
193189 }
194190
195191 private String constructLockKey (String path ) {
196- return RedisLockRegistry .this .registryKey + ":" + path ;
192+ return RedisLockRegistry .this .registryKey + ':' + path ;
197193 }
198194
199195 public long getLockedAt () {
@@ -331,14 +327,20 @@ public void unlock() {
331327 }
332328
333329 private void removeLockKey () {
334- if (this .unlinkAvailable ) {
330+ if (RedisLockRegistry . this .unlinkAvailable ) {
335331 try {
336332 RedisLockRegistry .this .redisTemplate .unlink (this .lockKey );
337333 }
338334 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+ }
342344 RedisLockRegistry .this .redisTemplate .delete (this .lockKey );
343345 }
344346 }
@@ -359,7 +361,7 @@ public boolean isAcquiredInThisProcess() {
359361
360362 @ Override
361363 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" );
363365 return "RedisLock [lockKey=" + this .lockKey
364366 + ",lockedAt=" + dateFormat .format (new Date (this .lockedAt ))
365367 + ", clientId=" + RedisLockRegistry .this .clientId
0 commit comments