Solved

How to connect to multiple parallel SFTP using JSCH?

Posted on 2013-10-30
6
5,249 Views
Last Modified: 2013-10-31
I'm using JSCH to connect to SFTP. What I'm trying to do is, I have some SFTP url, hostname and password stored in my database. Using query, I'll select ,say 5, url, hostname and password form my database and establish SFTP connection to transfer some file. I'm able to connect to SFTP one at time using a for or while loop. But I want to connect to SFTP parallelly. I tried the ExecutorService to create a thread pool to establish parallel connection but it is throwing an error.

ExecutorService executor = Executors.newFixedThreadPool(n);
for(int i = 0; i<n; i++){
Runnable task = new SFTPTask(outbound);
executor.submit(task);
}

executor.shutdown();
executor.awaitTermination(interval, TimeUnit.SECONDS);



class SFTPTask implements Runnable {

		SchedulerOutbound outbound;

		public SFTPTask(SchedulerOutbound outbound) {
			this.outbound = outbound;
		}

		@Override
		public void run() {

			List<SFTPUser> list = service.getList(outbound.getProvider());

			filename = outbound.getDataX12().split("IEA")[1].split("\\*")[2]
					.split("~")[0]
					+ "." + fileExt;

			try {
				BufferedOutputStream out = new BufferedOutputStream(
						new FileOutputStream(sourceFolder + filename));
				byte[] dataInByte = outbound.getDataX12().getBytes();
				out.write(dataInByte);

				out.flush();
				out.close();
			} catch (FileNotFoundException e) {

				e.printStackTrace();

			} catch (IOException e) {

				e.printStackTrace();

			}

			loginId = list.get(0).getLoginFk();
			url = list.get(0).getSftpUrl();
			login = list.get(0).getSftpLogin();
			password = list.get(0).getSftpPasswd();
			host = url.split("//")[1].split(":")[0];
			port = Integer.parseInt(url.split(":")[2]);
			System.out.println(loginId + url + login + password);
			try {

				JSch jsch = new JSch();
				session = jsch.getSession(login, host, port);
				session.setPassword(password);
				Properties config = new java.util.Properties();
				config.put("StrictHostKeyChecking", "no");
				session.connect();
				channel = session.openChannel("sftp");
				channel.connect();
				ChannelSftp channelSftp = (ChannelSftp) channel;

				channelSftp.cd(destFolder);

				File f = new File(sourceFolder + filename);

				channelSftp.put(new FileInputStream(f), f.getName());

				service.updateDataProcessed(outbound);
				 f.delete();
				channelSftp.exit();

			} catch (Exception e) {
				service.setAuthStatus(loginId, "failed");
				e.printStackTrace();

			} finally {
				if (channel != null) {
					channel.disconnect();
					session.disconnect();
					service.setAuthStatus(loginId, "success");
				}
			}
		}
	}
      

Open in new window

                           


I'm getting the following errors:

com.jcraft.jsch.JSchException: session is down

com.jcraft.jsch.JSchException: connection is closed by foreign host
0
Comment
Question by:spectrumsofttech
  • 2
  • 2
  • 2
6 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39613412
Since SFTP runs thru SSH on port 22, I don't see how you can make multiple connections on the same port at the same time.  Unless there is a way to use multiple source ports on your machine.
0
 

Author Comment

by:spectrumsofttech
ID: 39613429
Yes, host and port may be same but the sftp login and password will be different. Shouldn't it be possible to connect to multiple sftp in parallel, if you have different login and password credentials even though host and port are the same?
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39613437
What you posted above must be some *edited* form of your code, and the bits that you have edited out and probably crucial to solving this problem!!

In particular I would need to see how and where you have declared the "session" and "channel" variables. My guess is that they are declared in a scope that means there is only one instance of these variable shared among ALL threads. And so, on line 57 for example, you will have all X number of threads trying to set the *same* session variable to different values. The same for the channel variable.
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39613450
I suspect that there is only one thread and only one instance of all the variables.  To make it even possible, you would have to have a thread for each instance (I believe) and multiple source ports.  I know that in TCPView when you see Firefox making multiple connections, all the source ports are different.  I may be wrong but I don't think you can have multiple instances or programs using the same port at the same time.
0
 

Author Closing Comment

by:spectrumsofttech
ID: 39613528
Your guess about session and channel was correct. That hint was enough to solve my problem. Thank you for the help.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39613867
You're welcome, glad I could help! :)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 77
numbers ascending pyramid 101 196
expectj telnet failing 5 36
Is there a simpler dropbox system? 10 22
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question