From bb9dd892862413866d20027b620eef06b9a4d611 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 16 Dec 2021 13:48:51 -0500 Subject: [PATCH] Fix RedisLockRegistry Sonar smells * Remove redundant `subscribeLock()` method * Rework `lock()` and `lockInterruptibly()` logic in favor of `while (true) {` to avoid "empty `while()`" smell --- .../redis/util/RedisLockRegistry.java | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java b/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java index b46e694306..e157c87fb1 100644 --- a/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java +++ b/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java @@ -284,10 +284,9 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl this.localLock.lock(); while (true) { try { - while (!subscribeLock()) { - // empty + if (subscribeLock(-1L)) { + return; } - break; } catch (InterruptedException e) { /* @@ -310,19 +309,21 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl @Override public void lockInterruptibly() throws InterruptedException { this.localLock.lockInterruptibly(); - try { - while (!subscribeLock()) { - // empty + while (true) { + try { + if (subscribeLock(-1L)) { + return; + } + } + catch (InterruptedException ie) { + this.localLock.unlock(); + Thread.currentThread().interrupt(); + throw ie; + } + catch (Exception e) { + this.localLock.unlock(); + rethrowAsLockException(e); } - } - catch (InterruptedException ie) { - this.localLock.unlock(); - Thread.currentThread().interrupt(); - throw ie; - } - catch (Exception e) { - this.localLock.unlock(); - rethrowAsLockException(e); } } @@ -357,10 +358,6 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl return false; } - private boolean subscribeLock() throws ExecutionException, InterruptedException { - return subscribeLock(-1L); - } - private boolean subscribeLock(long time) throws ExecutionException, InterruptedException { if (!obtainLock()) { if (!RedisLockRegistry.this.redisMessageListenerContainer.isRunning()) {