问题
I'm using Spring Integration to poll files from remote FTP server and process them.
Is there a way to configure FtpInboundFileSynchronizer (or other component) to fetch and process remote files in specific order. Say i have file1 and file2 in remote directory, is it possible to fetch and process file1 before file2.
Thanks in advance
回答1:
There are (at least) 3 techniques to achieve this:
Add a custom
FileListFilter<FTPFile>
(that sorts theFTPFile
objects into the order you desire) to the synchronizer.Use two FTP outbound gateways, one the list (ls) the files, and one to get each file as needed.
Use the
FtpRemoteFileTemplate
from within your own code to list and fetch files.
EDIT
Actually, for #1, you would also need a custom FileListFilter<File>
in the local filter to sort the File
objects. Since the local files are emitted as message payloads after the synchronization is complete.
EDIT2 Remote file template example
This just copies the first file in the list, but it should give you what you need...
@SpringBootApplication
public class So49462148Application {
public static void main(String[] args) {
SpringApplication.run(So49462148Application.class, args);
}
@Bean
public ApplicationRunner runner(FtpRemoteFileTemplate template) {
return args -> {
FTPFile[] files = template.list("*.txt");
System.out.println(Arrays.toString(files));
template.get(files[0].getName(), is -> {
File file = new File("/tmp/" + files[0].getName());
FileOutputStream os = new FileOutputStream(file);
FileCopyUtils.copy(is, os);
System.out.println("Copied: " + file.getAbsolutePath());
});
};
}
@Bean
public DefaultFtpSessionFactory sf() {
DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
sf.setHost("...");
sf.setUsername("...");
sf.setPassword("...");
return sf;
}
@Bean
public FtpRemoteFileTemplate template(DefaultFtpSessionFactory sf) {
FtpRemoteFileTemplate template = new FtpRemoteFileTemplate(sf);
template.setRemoteDirectoryExpression(new LiteralExpression("foo"));
return template;
}
}
来源:https://stackoverflow.com/questions/49462148/spring-integration-ftp-inboud-channel-processing-files-in-specific-order