From ddab28a06f7f69cbc5f0ec4e6073ebe47e0b67e1 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Mon, 19 Jul 2021 16:36:42 -0400 Subject: [PATCH] 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`** --- .../AbstractCorrelatingMessageHandler.java | 16 ++++++++-------- .../file/FileWritingMessageHandler.java | 9 +++++---- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/aggregator/AbstractCorrelatingMessageHandler.java b/spring-integration-core/src/main/java/org/springframework/integration/aggregator/AbstractCorrelatingMessageHandler.java index f098c1ae37..1b19ddf4fb 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/aggregator/AbstractCorrelatingMessageHandler.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/aggregator/AbstractCorrelatingMessageHandler.java @@ -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) { diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java b/spring-integration-file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java index 712f6e8c09..bca85e1281 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java @@ -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 null. + * 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());