Using connection pool with JSCH

Daniel Lyam Montross picture Daniel Lyam Montross · Oct 8, 2012 · Viewed 10.1k times · Source

I'm using JSCH for file upload ever sftp. In it's current state each thread opens and closes connection when needed.

If it possible to use connection pooling with JSCH in order to avoid overhead caused by large number of connection opening and closing?

Here is a example of function called from inside of thread

 public static void file_upload(String filename) throws IOException {
    JSch jsch = new JSch();
    Session session = null;
    try {
        session = jsch.getSession("user", "server_name", 22);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword("super_secre_password");
        session.connect();

        Channel channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp sftpChannel = (ChannelSftp) channel;

        FileInputStream inputSrr = new FileInputStream(filename);  
        try {  
            sftpChannel.put(inputSrr, "/var/temp/"+filename);  
        } catch (SftpException e) {  
            e.printStackTrace();  

        } finally {  
            if (inputSrr != null) {  
                inputSrr.close();  
            }  
        }  

        sftpChannel.exit();
        session.disconnect();
    } catch (JSchException e) {
        e.printStackTrace();
    } catch (SftpException e) {
        e.printStackTrace();
    }
}

Answer

Mirko picture Mirko · Oct 10, 2012

For that I would prefer commons-pool. ;)