问题
i am using spring integration to move a file from SFTP to local directory. i am able to move files from one sftp server to local, i have 3 4 sftp servers, first i thought about writing different classes for each SFTP server, after my research i found that we can use delegating sessionfactory for setting more than one SFTP using java. i read the documentation but i couldn't implement that. anybody can help me with that. i am adding my code below.
@Configuration
@EnableIntegration
public class SftpFileMove {
@Value("${sftpConfig.host}")
private String host;
@Value("${sftpConfig.username}")
private String userName;
@Value("${sftpConfig.password}")
private String password;
@Value("${sftpConfig.port}")
private Integer port;
@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
final DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(host);
factory.setPort(port);
factory.setUser(userName);
factory.setPassword(password);
factory.setAllowUnknownKeys(true);
return new CachingSessionFactory<LsEntry>(factory);
}
@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
fileSynchronizer.setDeleteRemoteFiles(true);
fileSynchronizer.setRemoteDirectory("/upload/INV/");
fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.xml"));
return fileSynchronizer;
}
@Bean
@InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "30000"))
public MessageSource<File> sftpMessageSource() {
SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
sftpInboundFileSynchronizer());
source.setLocalDirectory(new File("feeds/"));
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 {
BatchProcessorLogger.debug("F111F7B0-9235-11EA-AB12-0800200C9A66", "Moved succussfully to{}",
message.getPayload());
}
};
}
}
回答1:
See Inbound Channel Adapters: Polling Multiple Servers and Directories.
Simply configure a session factory for each server an add them to the delegating factory, each with a key.
Then set up the RotatingServerAdvice
to cycle through the servers/directories.
来源:https://stackoverflow.com/questions/62256451/how-can-i-connect-with-different-sftp-server-dynamically