44import org .apache .commons .vfs2 .FileObject ;
55import org .apache .commons .vfs2 .FileSystemException ;
66import org .apache .commons .vfs2 .FileSystemOptions ;
7- import org .apache .commons .vfs2 .Selectors ;
87import org .apache .commons .vfs2 .impl .StandardFileSystemManager ;
98import org .apache .commons .vfs2 .provider .sftp .IdentityInfo ;
109import org .apache .commons .vfs2 .provider .sftp .SftpFileSystemConfigBuilder ;
@@ -35,6 +34,7 @@ public class SftpFileOutput
3534 private final String userInfo ;
3635 private final String host ;
3736 private final int port ;
37+ private final int maxConnectionRetry ;
3838 private final String pathPrefix ;
3939 private final String sequenceFormat ;
4040 private final String fileNameExtension ;
@@ -82,6 +82,7 @@ private FileSystemOptions initializeFsOptions(PluginTask task)
8282 if (task .getSecretKeyFilePath ().isPresent ()) {
8383 IdentityInfo identityInfo = new IdentityInfo (new File ((task .getSecretKeyFilePath ().get ())), task .getSecretKeyPassphrase ().getBytes ());
8484 SftpFileSystemConfigBuilder .getInstance ().setIdentityInfo (fsOptions , identityInfo );
85+ logger .info ("set identity: {}" , task .getSecretKeyFilePath ().get ());
8586 }
8687 }
8788 catch (FileSystemException e ) {
@@ -99,6 +100,7 @@ private FileSystemOptions initializeFsOptions(PluginTask task)
99100 this .fsOptions = initializeFsOptions (task );
100101 this .host = task .getHost ();
101102 this .port = task .getPort ();
103+ this .maxConnectionRetry = task .getMaxConnectionRetry ();
102104 this .pathPrefix = task .getPathPrefix ();
103105 this .sequenceFormat = task .getSequenceFormat ();
104106 this .fileNameExtension = task .getFileNameExtension ();
@@ -204,6 +206,26 @@ private String getOutputFilePath()
204206 private FileObject newSftpFile (URI sftpUri )
205207 throws FileSystemException
206208 {
207- return manager .resolveFile (sftpUri .toString (), fsOptions );
209+ int count = 0 ;
210+ while (true ) {
211+ try {
212+ return manager .resolveFile (sftpUri .toString (), fsOptions );
213+ }
214+ catch (FileSystemException e ) {
215+ if (++count == maxConnectionRetry ) {
216+ throw e ;
217+ }
218+ logger .warn ("failed to connect sftp server: " + e .getMessage (), e );
219+
220+ try {
221+ Thread .sleep (count * 1000 ); // milliseconds
222+ }
223+ catch (InterruptedException e1 ) {
224+ // Ignore this exception
225+ logger .warn (e .getMessage (), e );
226+ }
227+ logger .warn ("retry to connect sftp server: " + count + " times" );
228+ }
229+ }
208230 }
209231}
0 commit comments