@@ -34,6 +34,7 @@ public class SftpFileOutput
3434 private final String userInfo ;
3535 private final String host ;
3636 private final int port ;
37+ private final int maxConnectionRetry ;
3738 private final String pathPrefix ;
3839 private final String sequenceFormat ;
3940 private final String fileNameExtension ;
@@ -98,6 +99,7 @@ private FileSystemOptions initializeFsOptions(PluginTask task)
9899 this .fsOptions = initializeFsOptions (task );
99100 this .host = task .getHost ();
100101 this .port = task .getPort ();
102+ this .maxConnectionRetry = task .getMaxConnectionRetry ();
101103 this .pathPrefix = task .getPathPrefix ();
102104 this .sequenceFormat = task .getSequenceFormat ();
103105 this .fileNameExtension = task .getFileNameExtension ();
@@ -203,6 +205,26 @@ private String getOutputFilePath()
203205 private FileObject newSftpFile (URI sftpUri )
204206 throws FileSystemException
205207 {
206- return manager .resolveFile (sftpUri .toString (), fsOptions );
208+ int count = 0 ;
209+ while (true ) {
210+ try {
211+ return manager .resolveFile (sftpUri .toString (), fsOptions );
212+ }
213+ catch (FileSystemException e ) {
214+ if (++count == maxConnectionRetry ) {
215+ throw e ;
216+ }
217+ logger .warn ("failed to connect sftp server: " + e .getMessage (), e );
218+
219+ try {
220+ Thread .sleep (count * 1000 ); // milliseconds
221+ }
222+ catch (InterruptedException e1 ) {
223+ // Ignore this exception
224+ logger .warn (e .getMessage (), e );
225+ }
226+ logger .warn ("retry to connect sftp server: " + count + " times" );
227+ }
228+ }
207229 }
208230}
0 commit comments