1

I've created a class which uploads files to SFTP server using Apache Commons VFS2 and JSCH libraries. Everything works fine for the first (usually) 19 transfers but after that I'm starting to get exception

SEVERE: org.apache.commons.vfs2.FileSystemException: Could not connect to SFTP server at "sftp://username:***@localhost/".
at org.apache.commons.vfs2.provider.sftp.SftpFileProvider.doCreateFileSystem(SftpFileProvider.java:107)
at org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider.getFileSystem(AbstractOriginatingFileProvider.java:103)
at org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider.findFile(AbstractOriginatingFileProvider.java:81)
at org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider.findFile(AbstractOriginatingFileProvider.java:65)
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:693)
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:621)
at com.mycompany.ftp.FtpTransfer.upload(FtpTransfer.java:74)
at com.mycompany.ftp.FtpTransfer.upload(FtpTransfer.java:62)
at com.mycompany.ftp.FtpTransfer.upload(FtpTransfer.java:55)
at com.mycompany.ftp.FtpTest.testMultipleUploads(FtpTest.java:183)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.commons.vfs2.FileSystemException: Could not connect to SFTP server at "localhost".
at org.apache.commons.vfs2.provider.sftp.SftpClientFactory.createConnection(SftpClientFactory.java:230)
at org.apache.commons.vfs2.provider.sftp.SftpFileProvider.doCreateFileSystem(SftpFileProvider.java:96)
... 32 more
Caused by: com.jcraft.jsch.JSchException: Session.connect: java.net.SocketException: Connection reset
at com.jcraft.jsch.Session.connect(Session.java:558)
at com.jcraft.jsch.Session.connect(Session.java:183)
at org.apache.commons.vfs2.provider.sftp.SftpClientFactory.createConnection(SftpClientFactory.java:226)

Now if sleep for lets say three secs after each transfer I don't get exceptions at all.

Here's the code:

public void upload() throws Exception {         
    StandardFileSystemManager manager = new StandardFileSystemManager();
    FileObject localFile = null;
    FileObject remoteFile = null;
    try {                       
        // Initializes the file manager
        manager.init();
        // Setup our SFTP configuration
        FileSystemOptions opts = new FileSystemOptions();
        SftpFileSystemConfigBuilder configBuilder = SftpFileSystemConfigBuilder.getInstance();

        configBuilder.setStrictHostKeyChecking(opts, "no");
        configBuilder.setUserDirIsRoot(opts, true);
        configBuilder.setTimeout(opts, 10000);

        // Create local file object
        File file = new File(fileName);     
        localFile = manager.resolveFile(file.getAbsolutePath());

        // Create remote file object
        remoteFile = manager.resolveFile(this.createConnectionString().toString(),opts);

        // Copy local file to sftp server
        remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);      

    } catch (Exception e) {
        writeExceptionLog(e);
        throw e;
    } finally {         
        try {
            if(localFile != null)
                localFile.close();
            if(remoteFile != null) 
                remoteFile.close();
            manager.close();            
        } catch (FileSystemException e) {
            writeExceptionLog(e);
            throw e;
        }           
    }

Are there some resources which need to be released or does it just take time to free them internally?

Janne Rantala
  • 93
  • 1
  • 7
  • Could you edit your question to show a more complete copy of the exception and its stacktrace? Your own code should be in the stacktrace somewhere. And I'd expect to see one or more "caused by" blocks as well. – Kenster Feb 10 '16 at 16:44
  • Hi, thanks for your reply. I've added complete stacktrace. Also tried sending files using plain JSCH library and also SSHJ but all of them gave the same exception after 19 transfers. Is there some limit for concurrent or recurring ssh connections? – Janne Rantala Feb 11 '16 at 07:28
  • Hi there, did you manage to solve the problem? – deevee Nov 16 '17 at 14:48

0 Answers0