Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to connect to multiple parallel SFTP using JSCH?

Posted on 2013-10-30
6
Medium Priority
?
6,386 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 84

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 36

Accepted Solution

by:
mccarl earned 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 84

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 36

Expert Comment

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

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…
Suggested Courses

610 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