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
{
@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();
return stream;
}
"sftpChannelAdapter.start();" , .
"SourcePollingChannelAdapter".
XML- "id", . Spring , "id" bean.
, XML - , Spring Boot.
. !