From 9462d36de27ab7265126ed5732aa23bcac201765 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 14 Jul 2016 14:30:38 -0400 Subject: [PATCH] Add (S)FTP Inbound Java Config samples into Docs --- src/reference/asciidoc/ftp.adoc | 129 ++++++++++++++++++++++++++++++ src/reference/asciidoc/sftp.adoc | 131 +++++++++++++++++++++++++++++++ 2 files changed, 260 insertions(+) diff --git a/src/reference/asciidoc/ftp.adoc b/src/reference/asciidoc/ftp.adoc index 113a7c40c0..9f0bcde471 100644 --- a/src/reference/asciidoc/ftp.adoc +++ b/src/reference/asciidoc/ftp.adoc @@ -326,6 +326,97 @@ This will work for any `ResettableFileListFilter`. class="org.springframework.integration.transaction.PseudoTransactionManager" /> ---- +==== Configuring with Java Configuration + +The following Spring Boot application provides an example of configuring the inbound adapter using Java configuration: +[source, java] +---- +@SpringBootApplication +public class FtpJavaApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(FtpJavaApplication.class) + .web(false) + .run(args); + } + + @Bean + public SessionFactory ftpSessionFactory() { + DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory(); + sf.setHost("localhost"); + sf.setPort(port); + sf.setUsername("foo"); + sf.setPassword("foo"); + return new CachingSessionFactory(sf); + } + + @Bean + public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() { + FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(ftpSessionFactory()); + fileSynchronizer.setDeleteRemoteFiles(false); + fileSynchronizer.setRemoteDirectory("/"); + fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.xml")); + return fileSynchronizer; + } + + @Bean + @InboundChannelAdapter(channel = "ftpChannel") + public MessageSource ftpMessageSource() { + FtpInboundFileSynchronizingMessageSource source = + new FtpInboundFileSynchronizingMessageSource(ftpInboundFileSynchronizer()); + source.setLocalDirectory(new File("ftp-inbound")); + source.setAutoCreateLocalDirectory(true); + source.setLocalFilter(new AcceptOnceFileListFilter()); + return source; + } + + @Bean + @ServiceActivator(inputChannel = "ftpChannel") + public MessageHandler handler() { + return new MessageHandler() { + + @Override + public void handleMessage(Message message) throws MessagingException { + System.out.println(message.getPayload()); + } + + }; + } + +} +---- + +==== Configuring with the Java DSL + +The following Spring Boot application provides an example of configuring the inbound adapter using the Java DSL: + +[source, java] +---- +@SpringBootApplication +public class FtpJavaApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(FtpJavaApplication.class) + .web(false) + .run(args); + } + + @Bean + public IntegrationFlow ftpInboundFlow() { + return IntegrationFlows + .from(s -> s.ftp(this.ftpSessionFactory) + .preserveTimestamp(true) + .remoteDirectory("ftpSource") + .regexFilter(".*\\.txt$") + .localFilename(f -> f.toUpperCase() + ".a") + .localDirectory(new File("d:\ftp_files")), + e -> e.id("ftpInboundAdapter").autoStartup(false)) + .handle(m -> System.out.println(m.getPayload())) + .get(); + } +} +---- + [[ftp-streaming]] === FTP Streaming Inbound Channel Adapter @@ -361,6 +452,44 @@ file being processed again, you can configure an `FtpPersistentFileListFilter` i If you don't actually want to persist the state, an in-memory `SimpleMetadataStore` can be used with the filter. If you wish to use a filename pattern (or regex) as well, use a `CompositeFileListFilter`. +==== Configuring with Java Configuration + +The following Spring Boot application provides an example of configuring the inbound adapter using Java configuration: +[source, java] +---- +@SpringBootApplication +public class FtpJavaApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(FtpJavaApplication.class) + .web(false) + .run(args); + } + + @Bean + @InboundChannelAdapter(channel = "stream") + public MessageSource ftpMessageSource() { + FtpStreamingMessageSource messageSource = new FtpStreamingMessageSource(template(), null); + messageSource.setRemoteDirectory("ftpSource/"); + messageSource.setFilter(new FtpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), + "streaming")); + return messageSource; + } + + @Bean + @Transformer(inputChannel = "stream", outputChannel = "data") + public org.springframework.integration.transformer.Transformer transformer() { + return new StreamTransformer(); + } + + @Bean + public FtpRemoteFileTemplate template() { + return new FtpRemoteFileTemplate(ftpSessionFactory()); + } + +} +---- + [[ftp-outbound]] === FTP Outbound Channel Adapter diff --git a/src/reference/asciidoc/sftp.adoc b/src/reference/asciidoc/sftp.adoc index 8e56d1b70c..14940bde29 100644 --- a/src/reference/asciidoc/sftp.adoc +++ b/src/reference/asciidoc/sftp.adoc @@ -434,6 +434,98 @@ This will work for any `ResettableFileListFilter`. class="org.springframework.integration.transaction.PseudoTransactionManager" /> ---- +==== Configuring with Java Configuration + +The following Spring Boot application provides an example of configuring the inbound adapter using Java configuration: +[source, java] +---- +@SpringBootApplication +public class SftpJavaApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(SftpJavaApplication.class) + .web(false) + .run(args); + } + + @Bean + public SessionFactory sftpSessionFactory() { + DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true); + factory.setHost("localhost"); + factory.setPort(port); + factory.setUser("foo"); + factory.setPassword("foo"); + factory.setAllowUnknownKeys(true); + return new CachingSessionFactory(factory); + } + + @Bean + public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() { + SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory()); + fileSynchronizer.setDeleteRemoteFiles(false); + fileSynchronizer.setRemoteDirectory("/"); + fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.xml")); + return fileSynchronizer; + } + + @Bean + @InboundChannelAdapter(channel = "sftpChannel") + public MessageSource sftpMessageSource() { + SftpInboundFileSynchronizingMessageSource source = + new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer()); + source.setLocalDirectory(new File("ftp-inbound")); + source.setAutoCreateLocalDirectory(true); + source.setLocalFilter(new AcceptOnceFileListFilter()); + return source; + } + + @Bean + @ServiceActivator(inputChannel = "sftpChannel") + public MessageHandler handler() { + return new MessageHandler() { + + @Override + public void handleMessage(Message message) throws MessagingException { + System.out.println(message.getPayload()); + } + + }; + } + +} +---- + +==== Configuring with the Java DSL + +The following Spring Boot application provides an example of configuring the inbound adapter using the Java DSL: + +[source, java] +---- +@SpringBootApplication +public class SftpJavaApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(SftpJavaApplication.class) + .web(false) + .run(args); + } + + @Bean + public IntegrationFlow sftpInboundFlow() { + return IntegrationFlows + .from(s -> s.sftp(this.sftpSessionFactory) + .preserveTimestamp(true) + .remoteDirectory("sftpSource") + .regexFilter(".*\\.txt$") + .localFilenameExpression("#this.toUpperCase() + '.a'") + .localDirectory(this.sftpServer.getTargetLocalDirectory()), + e -> e.id("sftpInboundAdapter").autoStartup(false)) + .handle(m -> System.out.println(m.getPayload())) + .get(); + } +} +---- + [[sftp-streaming]] === SFTP Streaming Inbound Channel Adapter @@ -469,6 +561,45 @@ file being processed again, you can configure an `SftpPersistentFileListFilter` If you don't actually want to persist the state, an in-memory `SimpleMetadataStore` can be used with the filter. If you wish to use a filename pattern (or regex) as well, use a `CompositeFileListFilter`. +==== Configuring with Java Configuration + +The following Spring Boot application provides an example of configuring the inbound adapter using Java configuration: +[source, java] +---- +@SpringBootApplication +public class SftpJavaApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(SftpJavaApplication.class) + .web(false) + .run(args); + } + + @Bean + @InboundChannelAdapter(channel = "stream") + public MessageSource ftpMessageSource() { + SftpStreamingMessageSource messageSource = new SftpStreamingMessageSource(template(), null); + messageSource.setRemoteDirectory("sftpSource/"); + messageSource.setFilter(new SftpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), + "streaming")); + return messageSource; + } + + @Bean + @Transformer(inputChannel = "stream", outputChannel = "data") + public org.springframework.integration.transformer.Transformer transformer() { + return new StreamTransformer(); + } + + @Bean + public SftpRemoteFileTemplate template() { + return new SftpRemoteFileTemplate(sftpSessionFactory()); + } + +} +---- + + [[sftp-outbound]] === SFTP Outbound Channel Adapter