2
使用Spring Integration,是否可以使用ftp入站通道適配器遞歸地同步文件(包括子文件夾的文件)?Spring集成ftp入站通道適配器遞歸同步
使用Spring Integration,是否可以使用ftp入站通道適配器遞歸地同步文件(包括子文件夾的文件)?Spring集成ftp入站通道適配器遞歸同步
否;但你可以與出站網關遞歸MGET獲取完整的遠程目錄樹...
@SpringBootApplication
@IntegrationComponentScan
public class So42324318Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(So42324318Application.class, args);
List<File> files = context.getBean(Gateway.class).fetchFiles("foo/*");
System.out.println(files);
context.close();
}
@MessagingGateway(defaultRequestChannel = "fetchRecursive")
public interface Gateway {
public List<File> fetchFiles(String remoteDir);
}
@Bean
@ServiceActivator(inputChannel = "fetchRecursive")
public FtpOutboundGateway gateway() {
// Create a recursive MGET gateway that gets the remote directory from the payload
FtpOutboundGateway gateway = new FtpOutboundGateway(sessionFactory(), "mget", "payload");
gateway.setOptions("-R");
gateway.setLocalDirectoryExpression(new SpelExpressionParser().parseExpression("#remoteDirectory"));
return gateway;
}
@Bean
public SessionFactory<FTPFile> sessionFactory() {
return new CachingSessionFactory<>(ftpSF());
}
private SessionFactory<FTPFile> ftpSF() {
DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
sf.setHost("10.0.0.3");
sf.setUsername("ftptest");
sf.setPassword("ftptest");
sf.setClientMode(FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE);
return sf;
}
}
結果:
2017-02-19 09:55:09.351 INFO 61921 --- [ main] o.s.integration.ftp.session.FtpSession : File has been successfully transferred from: foo/bar.tx
2017-02-19 09:55:09.353 INFO 61921 --- [ main] o.s.integration.ftp.session.FtpSession : File has been successfully transferred from: foo/bar.txt
2017-02-19 09:55:09.356 INFO 61921 --- [ main] o.s.integration.ftp.session.FtpSession : File has been successfully transferred from: foo/bar/abc.txt
2017-02-19 09:55:09.358 INFO 61921 --- [ main] o.s.integration.ftp.session.FtpSession : File has been successfully transferred from: foo/baz.txt
2017-02-19 09:55:09.362 INFO 61921 --- [ main] o.s.integration.ftp.session.FtpSession : File has been successfully transferred from: foo/foo/bar/qux.txt
2017-02-19 09:55:09.364 INFO 61921 --- [ main] o.s.integration.ftp.session.FtpSession : File has been successfully transferred from: foo/foo/baz/fiz.txt
[foo/bar.tx, foo/bar.txt, foo/bar/abc.txt, foo/baz.txt, foo/foo/bar/qux.txt, foo/foo/baz/fiz.txt]
感謝@Gary的快速反應。使用你的代碼,我可以得到具有目錄層次結構的文件。我需要在遠程服務器上更新文件時輪詢並獲取文件,我如何使用出站網關執行此操作。基本上,我需要同步本地文件樹結構完好無損)與遙控器。 – user2654631
如果將'FileExistsMode'設置爲IGNORE,則每個MGET將只會獲取先前未提取的新文件。 5.0版本將有[新模式](https://github.com/spring-projects/spring-integration/pull/2062)'REPLACE_IF_MODIFIED',它將重新獲取存在但具有不同時間戳的文件(我昨天創建了PR )。另一種可能的解決方案是對入站適配器使用[智能輪詢器](http://docs.spring.io/spring-integration/reference/html/messaging-channels-section.html#__smart_polling)並更改遠程和當輪詢返回null時的本地目錄。 –