Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

Commit 6b0933c

Browse files
committed
Reset DynamoDb lockItem when HeartBeat fails
Related to spring-cloud/spring-cloud-stream-binder-aws-kinesis#148 If a `DynamoDbLock` has been locked before, it contains a `lockItem` indicating that we can send a heart-beat the next tine when we would like to re-lock again instead of calling regular lock and fail because the lock record exists already. On the other hand the heart-beat can fail by itself for many reasons including the case when record in DB was removed somehow. * Change the `DynamoDbLock.doLock()` logic to catch `sendHeartBeat()` exception and reset local state to let it to try to lock again with the regular `tryAcquireLock()` API
1 parent 526353c commit 6b0933c

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/main/java/org/springframework/integration/aws/lock/DynamoDbLockRegistry.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2020 the original author or authors.
2+
* Copyright 2018-2021 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.
@@ -526,12 +526,19 @@ public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
526526
}
527527

528528
private boolean doLock() throws InterruptedException {
529-
boolean acquired;
529+
boolean acquired = false;
530530
if (this.lockItem != null) {
531-
this.lockItem.sendHeartBeat();
532-
acquired = true;
531+
try {
532+
this.lockItem.sendHeartBeat();
533+
acquired = true;
534+
}
535+
catch (Exception e) {
536+
// May be no lock record in the DB - discard local holder and try to lock again
537+
this.lockItem = null;
538+
}
533539
}
534-
else {
540+
541+
if (this.lockItem == null) {
535542
this.lockItem = DynamoDbLockRegistry.this.dynamoDBLockClient
536543
.tryAcquireLock(this.acquireLockOptionsBuilder.build()).orElse(null);
537544

0 commit comments

Comments
 (0)