Skip to content

Commit 821dafb

Browse files
committed
Clean up LockRegistry API and some warning
1 parent f7b5946 commit 821dafb

File tree

5 files changed

+51
-41
lines changed

5 files changed

+51
-41
lines changed

spring-integration-core/src/main/java/org/springframework/integration/support/locks/DefaultLockRegistry.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
*
3232
* @author Oleg Zhurakousky
3333
* @author Gary Russell
34+
*
3435
* @since 2.1.1
3536
*
3637
*/
@@ -41,15 +42,15 @@ public final class DefaultLockRegistry implements LockRegistry<Lock> {
4142
private final int mask;
4243

4344
/**
44-
* Constructs a DefaultLockRegistry with the default
45+
* Construct a DefaultLockRegistry with the default
4546
* mask 0xFF with 256 locks.
4647
*/
4748
public DefaultLockRegistry() {
4849
this(0xFF); // NOSONAR magic number
4950
}
5051

5152
/**
52-
* Constructs a DefaultLockRegistry with the supplied
53+
* Construct a DefaultLockRegistry with the supplied
5354
* mask - the mask must have a value Math.pow(2, n) - 1 where n
5455
* is 1 to 31, creating a hash of Math.pow(2, n) locks.
5556
* <p> Examples:
@@ -61,7 +62,8 @@ public DefaultLockRegistry() {
6162
*/
6263
public DefaultLockRegistry(int mask) {
6364
String bits = Integer.toBinaryString(mask);
64-
Assert.isTrue(bits.length() < 32 && (mask == 0 || bits.lastIndexOf('0') < bits.indexOf('1')), "Mask must be a power of 2 - 1"); // NOSONAR magic number
65+
Assert.isTrue(bits.length() < 32 && (mask == 0 || bits.lastIndexOf('0') < bits.indexOf('1')),
66+
"Mask must be a power of 2 - 1"); // NOSONAR magic number
6567
this.mask = mask;
6668
int arraySize = this.mask + 1;
6769
this.lockTable = new ReentrantLock[arraySize];
@@ -71,14 +73,14 @@ public DefaultLockRegistry(int mask) {
7173
}
7274

7375
/**
74-
* Obtains a lock by masking the lockKey's hashCode() with
76+
* Obtain a lock by masking the lockKey's hashCode() with
7577
* the mask and using the result as an index to the lock table.
7678
* @param lockKey the object used to derive the lock index.
7779
*/
7880
@Override
7981
public Lock obtain(Object lockKey) {
8082
Assert.notNull(lockKey, "'lockKey' must not be null");
81-
Integer lockIndex = lockKey.hashCode() & this.mask;
83+
int lockIndex = lockKey.hashCode() & this.mask;
8284
return this.lockTable[lockIndex];
8385
}
8486

spring-integration-core/src/test/java/org/springframework/integration/handler/advice/LockRequestHandlerAdviceTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ void verifyLockAroundHandler() throws ExecutionException, InterruptedException,
114114
public static class Config {
115115

116116
@Bean
117-
LockRegistry lockRegistry() {
117+
LockRegistry<?> lockRegistry() {
118118
return new DefaultLockRegistry();
119119
}
120120

@@ -124,7 +124,7 @@ QueueChannel discardChannel() {
124124
}
125125

126126
@Bean
127-
LockRequestHandlerAdvice lockRequestHandlerAdvice(LockRegistry lockRegistry, QueueChannel discardChannel) {
127+
LockRequestHandlerAdvice lockRequestHandlerAdvice(LockRegistry<?> lockRegistry, QueueChannel discardChannel) {
128128
LockRequestHandlerAdvice lockRequestHandlerAdvice =
129129
new LockRequestHandlerAdvice(lockRegistry, (message) -> message.getHeaders().get(LOCK_KEY_HEADER));
130130
lockRequestHandlerAdvice.setDiscardChannel(discardChannel);

spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/lock/JdbcLockRegistry.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.integration.jdbc.lock;
1818

19+
import java.io.Serial;
1920
import java.time.Duration;
2021
import java.util.ConcurrentModificationException;
2122
import java.util.LinkedHashMap;
@@ -73,6 +74,9 @@ public class JdbcLockRegistry implements ExpirableLockRegistry<DistributedLock>,
7374
private final Map<String, JdbcLock> locks =
7475
new LinkedHashMap<>(16, 0.75F, true) {
7576

77+
@Serial
78+
private static final long serialVersionUID = -8345579941944883141L;
79+
7680
@Override
7781
protected boolean removeEldestEntry(Entry<String, JdbcLock> eldest) {
7882
return size() > JdbcLockRegistry.this.cacheCapacity;

spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/lock/JdbcLockRegistryTests.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ void testLockInterruptibly() throws Exception {
121121
}
122122

123123
@Test
124+
@SuppressWarnings("unchecked")
124125
void testLockWithCustomTtl() throws Exception {
125126
JdbcLockRegistry lockRegistry = new JdbcLockRegistry(client, Duration.ofMillis(100));
126127
long sleepTimeLongerThanDefaultTTL = 110;
@@ -141,6 +142,7 @@ void testLockWithCustomTtl() throws Exception {
141142
}
142143

143144
@Test
145+
@SuppressWarnings("unchecked")
144146
void testTryLockWithCustomTtl() throws Exception {
145147
JdbcLockRegistry lockRegistry = new JdbcLockRegistry(client, Duration.ofMillis(100));
146148
long sleepTimeLongerThanDefaultTTL = 110;
@@ -401,15 +403,15 @@ void testLockRenewWithCustomTtl() throws InterruptedException {
401403

402404
@Test
403405
void concurrentObtainCapacityTest() throws InterruptedException {
404-
final int KEY_CNT = 500;
405-
final int CAPACITY_CNT = 179;
406-
final int THREAD_CNT = 4;
406+
final int keyCnt = 500;
407+
final int capacityCnt = 179;
408+
final int threadCnt = 4;
407409

408-
final CountDownLatch countDownLatch = new CountDownLatch(THREAD_CNT);
409-
registry.setCacheCapacity(CAPACITY_CNT);
410-
final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_CNT);
410+
final CountDownLatch countDownLatch = new CountDownLatch(threadCnt);
411+
registry.setCacheCapacity(capacityCnt);
412+
final ExecutorService executorService = Executors.newFixedThreadPool(threadCnt);
411413

412-
for (int i = 0; i < KEY_CNT; i++) {
414+
for (int i = 0; i < keyCnt; i++) {
413415
int finalI = i;
414416
executorService.submit(() -> {
415417
countDownLatch.countDown();
@@ -429,32 +431,30 @@ void concurrentObtainCapacityTest() throws InterruptedException {
429431
executorService.awaitTermination(5, TimeUnit.SECONDS);
430432

431433
//capacity limit test
432-
assertThat(getRegistryLocks(registry)).hasSize(CAPACITY_CNT);
434+
assertThat(getRegistryLocks(registry)).hasSize(capacityCnt);
433435

434436
registry.expireUnusedOlderThan(-1000);
435437
assertThat(getRegistryLocks(registry)).isEmpty();
436438
}
437439

438440
@Test
439441
void concurrentObtainRemoveOrderTest() throws InterruptedException {
440-
final int THREAD_CNT = 2;
441-
final int DUMMY_LOCK_CNT = 3;
442-
443-
final int CAPACITY_CNT = THREAD_CNT;
442+
final int threadCnt = 2;
443+
final int dummyLockCnt = 3;
444444

445-
final CountDownLatch countDownLatch = new CountDownLatch(THREAD_CNT);
446-
registry.setCacheCapacity(CAPACITY_CNT);
447-
final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_CNT);
445+
final CountDownLatch countDownLatch = new CountDownLatch(threadCnt);
446+
registry.setCacheCapacity(threadCnt);
447+
final ExecutorService executorService = Executors.newFixedThreadPool(threadCnt);
448448
final Queue<String> remainLockCheckQueue = new LinkedBlockingQueue<>();
449449

450450
//Removed due to capcity limit
451-
for (int i = 0; i < DUMMY_LOCK_CNT; i++) {
451+
for (int i = 0; i < dummyLockCnt; i++) {
452452
Lock obtainLock0 = registry.obtain("foo:" + i);
453453
obtainLock0.lock();
454454
obtainLock0.unlock();
455455
}
456456

457-
for (int i = DUMMY_LOCK_CNT; i < THREAD_CNT + DUMMY_LOCK_CNT; i++) {
457+
for (int i = dummyLockCnt; i < threadCnt + dummyLockCnt; i++) {
458458
int finalI = i;
459459
executorService.submit(() -> {
460460
countDownLatch.countDown();
@@ -476,24 +476,24 @@ void concurrentObtainRemoveOrderTest() throws InterruptedException {
476476
executorService.awaitTermination(5, TimeUnit.SECONDS);
477477

478478
assertThat(getRegistryLocks(registry)).containsKeys(
479-
remainLockCheckQueue.toArray(new String[remainLockCheckQueue.size()]));
479+
remainLockCheckQueue.toArray(new String[0]));
480480
}
481481

482482
@Test
483483
void concurrentObtainAccessRemoveOrderTest() throws InterruptedException {
484-
final int THREAD_CNT = 2;
485-
final int DUMMY_LOCK_CNT = 3;
484+
final int threadCnt = 2;
485+
final int dummyLockCnt = 3;
486486

487-
final int CAPACITY_CNT = THREAD_CNT + 1;
487+
final int CAPACITY_CNT = threadCnt + 1;
488488
final String REMAIN_DUMMY_LOCK_KEY = "foo:1";
489489

490-
final CountDownLatch countDownLatch = new CountDownLatch(THREAD_CNT);
490+
final CountDownLatch countDownLatch = new CountDownLatch(threadCnt);
491491
registry.setCacheCapacity(CAPACITY_CNT);
492-
final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_CNT);
492+
final ExecutorService executorService = Executors.newFixedThreadPool(threadCnt);
493493
final Queue<String> remainLockCheckQueue = new LinkedBlockingQueue<>();
494494

495495
//Removed due to capcity limit
496-
for (int i = 0; i < DUMMY_LOCK_CNT; i++) {
496+
for (int i = 0; i < dummyLockCnt; i++) {
497497
Lock obtainLock0 = registry.obtain("foo:" + i);
498498
obtainLock0.lock();
499499
obtainLock0.unlock();
@@ -504,7 +504,7 @@ void concurrentObtainAccessRemoveOrderTest() throws InterruptedException {
504504
obtainLock0.unlock();
505505
remainLockCheckQueue.offer(toUUID(REMAIN_DUMMY_LOCK_KEY));
506506

507-
for (int i = DUMMY_LOCK_CNT; i < THREAD_CNT + DUMMY_LOCK_CNT; i++) {
507+
for (int i = dummyLockCnt; i < threadCnt + dummyLockCnt; i++) {
508508
int finalI = i;
509509
executorService.submit(() -> {
510510
countDownLatch.countDown();
@@ -531,15 +531,15 @@ void concurrentObtainAccessRemoveOrderTest() throws InterruptedException {
531531

532532
@Test
533533
void setCapacityTest() {
534-
final int CAPACITY_CNT = 4;
535-
registry.setCacheCapacity(CAPACITY_CNT);
534+
final int capacityCnt = 4;
535+
registry.setCacheCapacity(capacityCnt);
536536

537537
registry.obtain("foo:1");
538538
registry.obtain("foo:2");
539539
registry.obtain("foo:3");
540540

541541
//capacity 4->3
542-
registry.setCacheCapacity(CAPACITY_CNT - 1);
542+
registry.setCacheCapacity(capacityCnt - 1);
543543

544544
registry.obtain("foo:4");
545545

@@ -549,7 +549,7 @@ void setCapacityTest() {
549549
toUUID("foo:4"));
550550

551551
//capacity 3->4
552-
registry.setCacheCapacity(CAPACITY_CNT);
552+
registry.setCacheCapacity(capacityCnt);
553553
registry.obtain("foo:5");
554554
assertThat(getRegistryLocks(registry)).hasSize(4);
555555
assertThat(getRegistryLocks(registry)).containsKeys(toUUID("foo:3"),
@@ -588,7 +588,7 @@ void testUnlockAfterLockStatusHasBeenExpiredAndLockHasBeenAcquiredByAnotherProce
588588
Lock lock2 = process2Registry.obtain("foo");
589589

590590
lock1.lock();
591-
Thread.sleep(ttl);
591+
Thread.sleep(ttl.toMillis());
592592
assertThat(lock2.tryLock()).isTrue();
593593

594594
assertThatExceptionOfType(ConcurrentModificationException.class)

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@
9999
* @since 4.0
100100
*
101101
*/
102-
public final class RedisLockRegistry implements ExpirableLockRegistry<DistributedLock>, DisposableBean, RenewableLockRegistry<DistributedLock> {
102+
public final class RedisLockRegistry
103+
implements ExpirableLockRegistry<DistributedLock>, DisposableBean, RenewableLockRegistry<DistributedLock> {
103104

104105
private static final Log LOGGER = LogFactory.getLog(RedisLockRegistry.class);
105106

@@ -423,7 +424,8 @@ public long getLockedAt() {
423424
* @throws InterruptedException –
424425
* if the current thread is interrupted while acquiring the lock (and interruption of lock acquisition is supported)
425426
*/
426-
protected abstract boolean tryRedisLockInner(long time, long expireAfter) throws ExecutionException, InterruptedException;
427+
protected abstract boolean tryRedisLockInner(long time, long expireAfter)
428+
throws ExecutionException, InterruptedException;
427429

428430
/**
429431
* Unlock the lock using the unlink method in redis.
@@ -533,7 +535,8 @@ private boolean tryRedisLock(long time, long expireAfter) throws ExecutionExcept
533535
if (RedisLockRegistry.this.renewalTaskScheduler != null) {
534536
Duration delay = Duration.ofMillis(expireAfter / 3);
535537
this.renewFuture =
536-
RedisLockRegistry.this.renewalTaskScheduler.scheduleWithFixedDelay(() -> this.renew(expireAfter), delay);
538+
RedisLockRegistry.this.renewalTaskScheduler.scheduleWithFixedDelay(() ->
539+
this.renew(expireAfter), delay);
537540
}
538541
}
539542
return acquired;
@@ -718,7 +721,8 @@ private RedisPubSubLock(String path) {
718721
}
719722

720723
@Override
721-
protected boolean tryRedisLockInner(long time, long expireAfter) throws ExecutionException, InterruptedException {
724+
protected boolean tryRedisLockInner(long time, long expireAfter)
725+
throws ExecutionException, InterruptedException {
722726
return subscribeLock(time, expireAfter);
723727
}
724728

0 commit comments

Comments
 (0)