Spring SFTP Integration Example with Spring Download

We use the latest Spring download for the Spring application and use the latest Spring integration for SFTP. I was on the Spring Integration SFTP documentation documentation site and I accepted the Spring boot configuration as is:

 @Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost("localhost");
    factory.setPort(port);
    factory.setUser("foo");
    factory.setPassword("foo");
    factory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<LsEntry>(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<File> sftpMessageSource() {
    SftpInboundFileSynchronizingMessageSource source =
            new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer());
    source.setLocalDirectory(new File("ftp-inbound"));
    source.setAutoCreateLocalDirectory(true);
    source.setLocalFilter(new AcceptOnceFileListFilter<File>());
    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());
        }

    };
}

Let me be clear, after cutting and pasting, there are some unit tests that are performed. However, when loading the application context, an error message appeared because there was no poll.

When I googled this error, other posts in StackOverflow said that I also had to add this error message when loading the application context.

@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata defaultPoller() {

    PollerMetadata pollerMetadata = new PollerMetadata();
    pollerMetadata.setTrigger(new PeriodicTrigger(60));
    return pollerMetadata;
}

When I added this code, THEN, at least, my build will work, and the tests will run because the application context is now loading correctly.

, ? Spring SFTP GitHub , ... .

Spring , SFTP-, application-context.xml. , Spring, ?

, , Java Spring application-context.xml... SFTP- .

, , :

@Component
@Profile("sftpInputFetch")
public class SFTPInputFetcher implements InputFetcher
{
    // The PollableChannel seems fine
    @Autowired
    PollableChannel sftpChannel;

    @Autowired
    SourcePollingChannelAdapter sftpChannelAdapter;

@Override
public Stream<String> fetchLatest() throws FileNotFoundException
{
    Stream<String> stream = null;
    sftpChannelAdapter.start();
    Message<?> received = sftpChannel.receive();
    File file = (File)received.getPayload();
    // get Stream<String> from file
    return stream;
}

"sftpChannelAdapter.start();" , . "SourcePollingChannelAdapter".

XML- "id", . Spring , "id" bean.

, XML - , Spring Boot.

. !

+4
1

;

...,

, ?

?

@InboundChannelAdapter(value = "sftpChannel", poller = @Poller(fixedDelay = "5000"))

.

.

( poller), .

@SpringBootApplication
public class SftpJavaApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(SftpJavaApplication.class).web(false).run(args);
    }

    @Bean
    public SessionFactory<LsEntry> sftpSessionFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
        factory.setHost("...");
        factory.setPort(22);
        factory.setUser("...");
        factory.setPassword("...");
        factory.setAllowUnknownKeys(true);
        return new CachingSessionFactory<LsEntry>(factory);
    }

    @Bean
    public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
        SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
        fileSynchronizer.setDeleteRemoteFiles(false);
        fileSynchronizer.setRemoteDirectory("foo");
        fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.txt"));
        return fileSynchronizer;
    }

    @Bean
    @InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "5000"))
    public MessageSource<File> sftpMessageSource() {
        SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
                sftpInboundFileSynchronizer());
        source.setLocalDirectory(new File("ftp-inbound"));
        source.setAutoCreateLocalDirectory(true);
        source.setLocalFilter(new AcceptOnceFileListFilter<File>());
        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());
            }

        };
    }

}

:

16:57:59.697 [task-scheduler-1] WARN  com.jcraft.jsch - Permanently added '10.0.0.3' (RSA) to the list of known hosts.
ftp-inbound/bar.txt
ftp-inbound/baz.txt
+3

Source: https://habr.com/ru/post/1663781/


All Articles