Using ACN Connection Pool

I use JSCH to download sftp files ever. In this current state, each thread opens and closes the connection when necessary.

If you can use the connection pool with AOX to avoid the overhead caused by a large number of open and close connections?

Here is an example of a function called from within a 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(); } } 
+4
source share
2 answers

For this, I would prefer a shared pool .;)

+4
source

Here's the implementation of the Ssh connection pool http://www.javacodegeeks.com/2013/02/pool-of-ssh-connections-using-apache-keyedobjectpool.html

you can use grep4j to use this pool https://code.google.com/p/grep4j/source/browse/trunk/src/main/java/org/grep4j/core/command/linux/SessionFactory.java?r= 354

Also make sure that you can access the server from the runtime device. For example, if the target server is unavailable. This will cause a connection timeout.

+4
source

Source: https://habr.com/ru/post/1438387/


All Articles