From fec5d764a6419b171bb85b69e626ff255dcd0294 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Mon, 19 Jun 2017 09:00:47 -0400 Subject: [PATCH] INT-4296: Fix APPEND_NO_FLUSH Timing Problem JIRA: https://jira.spring.io/browse/INT-4296 The `FileState.close()` method correctly acquires the lock to prevent closing while writing. However, with a short flush interval, the close could occur between state creation and the write operation acquiring the lock. Move the state creation to within the scope of the lock acquired by the writer. (cherry picked from commit 404c2c5) --- .../integration/file/FileWritingMessageHandler.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) 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 e4a4087db0..f8e2a416a0 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 @@ -536,13 +536,12 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand if (append) { final File fileToWriteTo = this.determineFileToWrite(resultFile, tempFile); - final FileState state = getFileState(fileToWriteTo, false); - WhileLockedProcessor whileLockedProcessor = new WhileLockedProcessor(this.lockRegistry, fileToWriteTo.getAbsolutePath()) { @Override protected void whileLocked() throws IOException { + FileState state = getFileState(fileToWriteTo, false); BufferedOutputStream bos = null; try { bos = state != null ? state.stream : createOutputStream(fileToWriteTo, true); @@ -620,8 +619,6 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand throws IOException { final File fileToWriteTo = this.determineFileToWrite(resultFile, tempFile); - final FileState state = getFileState(fileToWriteTo, false); - final boolean append = FileExistsMode.APPEND.equals(this.fileExistsMode); WhileLockedProcessor whileLockedProcessor = new WhileLockedProcessor(this.lockRegistry, @@ -629,6 +626,7 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand @Override protected void whileLocked() throws IOException { + FileState state = getFileState(fileToWriteTo, false); BufferedOutputStream bos = null; try { bos = state != null ? state.stream : createOutputStream(fileToWriteTo, append); @@ -664,8 +662,6 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand throws IOException { final File fileToWriteTo = this.determineFileToWrite(resultFile, tempFile); - final FileState state = getFileState(fileToWriteTo, true); - final boolean append = FileExistsMode.APPEND.equals(this.fileExistsMode); WhileLockedProcessor whileLockedProcessor = new WhileLockedProcessor(this.lockRegistry, @@ -673,6 +669,7 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand @Override protected void whileLocked() throws IOException { + FileState state = getFileState(fileToWriteTo, true); BufferedWriter writer = null; try { writer = state != null ? state.writer : createWriter(fileToWriteTo, append);