0

I've faced a problem with getting files by sftp from parallel stream.

I have SFTP server and some list of objects and need to download files from this server according to object's data. I am using getFile method in each loop and it works till I reach maximum of server connections.

list.stream().parallel().forEach(item -> {
    try {
        getFile(item.getFrom(), to)
    } catch (Exception e) {
        e.printStackTrace();
    }
});
public static void getFile(String copyFrom, String copyTo) throws JSchException, SftpException {
    JSch jsch = new JSch();
    Session session = jsch.getSession("user", "ip", port);
    session.setConfig("StrictHostKeyChecking", "no");
    session.setPassword("password");
    session.connect();
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    sftpChannel.get(copyFrom, copyTo, monitor, ChannelSftp.OVERWRITE);
    sftpChannel.exit();
    session.disconnect();
}

Is there a way not to reconnect each loop and download huge number of files by having single connection?

VSs
  • 61
  • 4

0 Answers0