Skip to content

Commit bb9dd89

Browse files
committed
Fix RedisLockRegistry Sonar smells
* Remove redundant `subscribeLock()` method * Rework `lock()` and `lockInterruptibly()` logic in favor of `while (true) {` to avoid "empty `while()`" smell
1 parent 8c57cb7 commit bb9dd89

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,9 @@ public void lock() {
284284
this.localLock.lock();
285285
while (true) {
286286
try {
287-
while (!subscribeLock()) {
288-
// empty
287+
if (subscribeLock(-1L)) {
288+
return;
289289
}
290-
break;
291290
}
292291
catch (InterruptedException e) {
293292
/*
@@ -310,19 +309,21 @@ private void rethrowAsLockException(Exception e) {
310309
@Override
311310
public void lockInterruptibly() throws InterruptedException {
312311
this.localLock.lockInterruptibly();
313-
try {
314-
while (!subscribeLock()) {
315-
// empty
312+
while (true) {
313+
try {
314+
if (subscribeLock(-1L)) {
315+
return;
316+
}
317+
}
318+
catch (InterruptedException ie) {
319+
this.localLock.unlock();
320+
Thread.currentThread().interrupt();
321+
throw ie;
322+
}
323+
catch (Exception e) {
324+
this.localLock.unlock();
325+
rethrowAsLockException(e);
316326
}
317-
}
318-
catch (InterruptedException ie) {
319-
this.localLock.unlock();
320-
Thread.currentThread().interrupt();
321-
throw ie;
322-
}
323-
catch (Exception e) {
324-
this.localLock.unlock();
325-
rethrowAsLockException(e);
326327
}
327328
}
328329

@@ -357,10 +358,6 @@ public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
357358
return false;
358359
}
359360

360-
private boolean subscribeLock() throws ExecutionException, InterruptedException {
361-
return subscribeLock(-1L);
362-
}
363-
364361
private boolean subscribeLock(long time) throws ExecutionException, InterruptedException {
365362
if (!obtainLock()) {
366363
if (!RedisLockRegistry.this.redisMessageListenerContainer.isRunning()) {

0 commit comments

Comments
 (0)