Fix pattern how Lock.unlock() is used

Related to https://build.spring.io/browse/INT-MAIN-84/

The `lock.unlock()` must be called in the `finally` block of the
nested `try..catch`, not in the outer which may just fail on `lock.lockInterruptibly()`
in which case there is just not going to be anything we can `unlock()` in the end

**Cherry-pick to `5.4.x` & `5.3.x`**
This commit is contained in:
Artem Bilan
2021-07-19 16:36:42 -04:00
committed by Gary Russell
parent 70f6ceb5b9
commit ddab28a06f
2 changed files with 13 additions and 12 deletions

View File

@@ -514,19 +514,19 @@ public abstract class AbstractCorrelatingMessageHandler extends AbstractMessageP
boolean noOutput = true;
try {
lock.lockInterruptibly();
try {
noOutput = processMessageForGroup(message, correlationKey, groupIdUuid, lock);
}
finally {
if (noOutput || !this.releaseLockBeforeSend) {
lock.unlock();
}
}
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new MessageHandlingException(message, "Interrupted getting lock in the [" + this + ']', e);
}
try {
noOutput = processMessageForGroup(message, correlationKey, groupIdUuid, lock);
}
finally {
if (noOutput || !this.releaseLockBeforeSend) {
lock.unlock();
}
}
}
private boolean processMessageForGroup(Message<?> message, Object correlationKey, UUID groupIdUuid, Lock lock) {

View File

@@ -599,7 +599,7 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand
/**
* Retrieves the File instance from the {@link FileHeaders#ORIGINAL_FILE}
* header if available. If the value is not a File instance or a String
* representation of a file path, this will return <code>null</code>.
* representation of a file path, this will return {@code null}.
*/
private File retrieveOriginalFileFromHeader(Message<?> message) {
Object value = message.getHeaders().get(FileHeaders.ORIGINAL_FILE);
@@ -1075,15 +1075,15 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand
catch (IOException e) {
// ignore
}
finally {
this.lock.unlock();
}
return true;
}
catch (InterruptedException e1) {
Thread.currentThread().interrupt();
return false;
}
finally {
this.lock.unlock();
}
}
}
@@ -1174,6 +1174,7 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand
@Override
public boolean shouldFlush(String fileAbsolutePath, long firstWrite, long lastWrite,
Message<?> triggerMessage) {
Pattern pattern;
if (triggerMessage.getPayload() instanceof String) {
pattern = Pattern.compile((String) triggerMessage.getPayload());