INT-4305: (S)FTP: Remove Local File Before Rename

JIRA: https://jira.spring.io/browse/INT-4305

The `File.renameTo()` operation may fail, therefore the content of the
local file isn't changed, but since we change `setLastModified()` anyway,
this file might be eligible for local polling.
So, we end up with the same content from local file in a new message,
meanwhile we expect a new content from the remote file

* Check the `File.renameTo()` result and attempt to `delete()`
for existing local file
* When file isn't renames remove the remote file from the `filter` to let
it be transferred one more time on the next poll.
The local file might be opened for processing, so this way we postpone a fresh
remote file for the future poll rounds
* Modify `copyFileToLocalDirectory()` to return `boolean` to reflect the fact of copy.
This way we check the real number of transferred files

**Cherry-pick to 4.3.x**

Do not transfer remote file if we can't remove local one

Polishing Log Messages
This commit is contained in:
Artem Bilan
2017-06-27 12:23:26 -04:00
committed by Gary Russell
parent 36ac9f2203
commit 72bd4e5bcf
4 changed files with 112 additions and 46 deletions

View File

@@ -27,6 +27,9 @@ import static org.junit.Assert.assertThat;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
@@ -64,6 +67,7 @@ import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.FileCopyUtils;
/**
* @author Artem Bilan
@@ -82,7 +86,7 @@ public class FtpTests extends FtpTestSupport {
private ApplicationContext context;
@Test
public void testFtpInboundFlow() {
public void testFtpInboundFlow() throws IOException {
QueueChannel out = new QueueChannel();
IntegrationFlow flow = IntegrationFlows.from(Ftp.inboundAdapter(sessionFactory())
.preserveTimestamp(true)
@@ -112,6 +116,10 @@ public class FtpTests extends FtpTestSupport {
assertNull(out.receive(10));
File remoteFile = new File(this.sourceRemoteDirectory, " " + prefix() + "Source1.txt");
FileOutputStream fos = new FileOutputStream(remoteFile);
fos.write("New content".getBytes());
fos.close();
remoteFile.setLastModified(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
message = out.receive(10_000);
@@ -120,6 +128,7 @@ public class FtpTests extends FtpTestSupport {
assertThat(payload, instanceOf(File.class));
file = (File) payload;
assertEquals(" FTPSOURCE1.TXT.a", file.getName());
assertEquals("New content", FileCopyUtils.copyToString(new FileReader(file)));
MessageSource<?> source = context.getBean(FtpInboundFileSynchronizingMessageSource.class);
assertThat(TestUtils.getPropertyValue(source, "maxFetchSize"), equalTo(10));