I'm kind of new to the Multithreading approach. currently, in my code I am using ExecutorService Executors.newSingleThreadExecutor() to span a single thread that checks for files in a particular Directory if present I will fetch n files path(ex: n=5 files path) and will perform SFTP Operations of 5 files one by one. But when I have a Large number of files the single-threaded approach will take a lot of time to sftp each file one by one.
SftpUpload Service class:-
@Autowired
private UploadMessageGateway gateway;
public void uploadFile(File file) throws SftpException
{
try
{
gateway.uploadFile(file);
}
catch (Exception e)
{
logger.error(" Exception found at SftpUpload {} ",
e.getMessage());
}
}
sftp gateway:-
@MessagingGateway
public interface UploadMessageGateway
{
@Gateway(requestChannel = "uploadfile")
void uploadFile(File file);
}
sftp config file:-
@Bean
public SessionFactory<ChannelSftp.LsEntry> factoryDetails() throws
SftpException {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
factory.setHost(host);
factory.setPort(port);
factory.setAllowUnknownKeys(true);
factory.setUser(username);
factory.setPassword(password);
CachingSessionFactory<ChannelSftp.LsEntry> cachingSessionFactory = new CachingSessionFactory<>(factory);
cachingSessionFactory.setTestSession(true);
return new CachingSessionFactory<ChannelSftp.LsEntry>(factory);
}
@Bean
@ServiceActivator(inputChannel = "uploadfile")
public MessageHandler uploadHandler()
{
SessionFactory<ChannelSftp.LsEntry>
defaultSessionFactory= factoryDetails();
sftpMessageHandler = new SftpMessageHandler(defaultSessionFactory);
sftpMessageHandler.setRemoteDirectoryExpression(
new LiteralExpression(remotePath,
defaultSessionFactory)));
return sftpMessageHandler;
}
is it a better option to use the spring-batch master-slave approach or to make use of Executors.newScheduledThreadPool(5) or is there any standard approach? your suggestions/links will be very helpful.
Thank you.