Solved

How to connect to multiple parallel SFTP using JSCH?

Posted on 2013-10-30
6
5,073 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Hello to you all, I hear of many people congratulate AWS (Amazon Web Services) on how easy it is to spin up and create new EC2 (Elastic Compute Cloud) instances, but then fail and struggle to connect to them using simple tools such as SSH (Secure…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now