?
Solved

How to connect to multiple parallel SFTP using JSCH?

Posted on 2013-10-30
6
Medium Priority
?
6,070 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
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.
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 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