Мы получаем ошибку соединения для sftp через поток интеграции, фактически мы планируем cron размещать файл на клиентском sftp-сервере ежедневно один раз. таким образом, файл получает ошибку через день. Я имею в виду, что файл успешно помещен один день, а в другой день - ошибка соединения.
Ошибка, которую мы получаем:
at java.lang.Thread.run(Thread.java:748)Caused by: org.springframework.messaging.MessagingException: Failed to execute on session; nested exception is java.lang.IllegalStateException: failed to create FTPClient
at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:446)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:298)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:286)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:278)
at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.handleMessageInternal(FileTransferringMessageHandler.java:205)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:162)
... 27 more Caused by: java.lang.IllegalStateException: failed to create FTPClient
at org.springframework.integration.ftp.session.AbstractFtpSessionFactory.getSession(AbstractFtpSessionFactory.java:170)
at org.springframework.integration.ftp.session.AbstractFtpSessionFactory.getSession(AbstractFtpSessionFactory.java:41)
at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:432)
... 32 more Caused by: org.apache.commons.net.MalformedServerReplyException: Could not parse response code. Server Reply: SSH-2.0-OpenSSH_7.4
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:344)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:300)
at org.apache.commons.net.ftp.FTP._connectAction_(FTP.java:438)
at org.apache.commons.net.ftp.FTPClient._connectAction_(FTPClient.java:962)
at org.apache.commons.net.ftp.FTPClient._connectAction_(FTPClient.java:950)
at org.apache.commons.net.SocketClient._connect(SocketClient.java:244)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:202)
at org.springframework.integration.ftp.session.AbstractFtpSessionFactory.createClient(AbstractFtpSessionFactory.java:193)
at org.springframework.integration.ftp.session.AbstractFtpSessionFactory.getSession(AbstractFtpSessionFactory.java:167)
... 34 more
Код для подключения
public CachingSessionFactory<LsEntry> sftpSessionFactory(FTPOutboundConfigEntity config) {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(config.getHost());
factory.setPort(config.getPort());
factory.setUser(config.getUserName());
factory.setAllowUnknownKeys(true);
factory.setPassword(config.getUserPassword());
if (null != config.getSftpKeyFile()) {
factory.setPrivateKeyPassphrase(config.getPassphrase());
Resource resource = new FileSystemResource(config.getSftpKeyFile());
factory.setPrivateKey(resource);
}
return new CachingSessionFactory<LsEntry>(factory);
}
public IntegrationFlow outboundFlow(FTPOutboundConfigEntity config) {
SftpMessageHandlerSpec outboundAdapter = Sftp.outboundAdapter(sftpSessionFactory(config))
.remoteDirectory(config.getRemoteDirectory());
return IntegrationFlows
.from(Files.inboundAdapter(new File(config.getLocalDirectory())).autoCreateDirectory(true)
.preventDuplicates(true).regexFilter(config.getFileExtensionFilter()),
spec -> spec.poller(Pollers.fixedDelay(config.getInitialDelay(), config.getPoolerFrequency())))
.handle(outboundAdapter, c -> c.advice(expressionAdvice(c, config.getBackupDirectory()))).get();
}
public Advice expressionAdvice(GenericEndpointSpec<FileTransferringMessageHandler<ChannelSftp.LsEntry>> c, String backupPath) {
ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
advice.setOnSuccessExpression(new SpelExpressionParser().parseExpression("payload.renameTo(new java.io.File(payload.absolutePath + '.bkp'))"));
advice.setOnFailureExpression(new SpelExpressionParser().parseExpression("payload.renameTo(new java.io.File(payload.absolutePath + '.failed.to.send'))"));
advice.setTrapException(true);
return advice;
}