Я использую Spring Integration для чтения файлов с SFTP-сервера, и все отлично работает, используя InboundChannelAdapter с Java Конфигурацией.
Теперь я хочу изменить свой процесс, чтобы удалить все обработанные файлы с сервера SFTP. Поэтому я хочу использовать SFTP OutboundGateway с Java Configuration. Это мой новый код с несколькими изменениями, основанными на https://docs.spring.io/spring-integration/docs/5.0.0.BUILD-SNAPSHOT/reference/html/sftp.html#sftp -outbound-gateway :
@Configuration
public class SftpConfiguration {
@Value("${sftp.host}")
String sftpHost = "";
@Value("${sftp.user}")
String sftpUser = "";
@Value("${sftp.pass}")
String sftpPass = "";
@Value("${sftp.port}")
Integer sftpPort = 0;
@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(sftpHost);
factory.setPort(sftpPort);
factory.setUser(sftpUser);
factory.setPassword(sftpPass);
factory.setAllowUnknownKeys(true);
return new CachingSessionFactory<LsEntry>(factory);
}
@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
fileSynchronizer.setDeleteRemoteFiles(false);
fileSynchronizer.setRemoteDirectory("/upload/");
return fileSynchronizer;
}
@Bean
@InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(cron = "0 * * * * ?"))
public MessageSource<File> sftpMessageSource() {
SftpInboundFileSynchronizingMessageSource source =
new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer());
source.setLocalDirectory(new File("sftp-folder"));
source.setAutoCreateLocalDirectory(true);
return source;
}
@Bean
@ServiceActivator(inputChannel = "sftpChannel")
MessageHandler handler() {
return new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
File f = (File) message.getPayload();
try {
myProcessingClass.processFile(f);
SftpOutboundGateway sftpOG = new SftpOutboundGateway(sftpSessionFactory(), "rm",
"'/upload/" + f.getName() + "'");
} catch(QuoCrmException e) {
logger.error("File [ Process with errors, file won't be deleted: " + e.getMessage() + "]");
}
}
};
}
}
Изменения:
- Я определил свой fileSynchronizer для setDeleteRemoteFiles (false), чтобы вручную удалить файлы в соответствии с моим процессом.
- В моем MessageHandler я добавил свой SFTPOutboundGateway и, если нет исключения, это означает, что обработка прошла успешно и удаляет файл (но если исключение не удалит файл).
Этот код не удаляет файл. Есть предложения?