|
20 | 20 | import org.slf4j.Logger; |
21 | 21 |
|
22 | 22 | import java.io.BufferedOutputStream; |
| 23 | +import java.io.Closeable; |
23 | 24 | import java.io.File; |
24 | 25 | import java.io.FileInputStream; |
25 | 26 | import java.io.IOException; |
|
39 | 40 | public class SftpUtils |
40 | 41 | { |
41 | 42 | private final Logger logger = Exec.getLogger(SftpUtils.class); |
42 | | - private final DefaultFileSystemManager manager; |
| 43 | + private DefaultFileSystemManager manager; |
43 | 44 | private final FileSystemOptions fsOptions; |
44 | 45 | private final String userInfo; |
45 | 46 | private final String user; |
@@ -159,12 +160,23 @@ public Void call() throws Exception |
159 | 160 | final FileObject remoteFile = newSftpFile(getSftpFileUri(remotePath)); |
160 | 161 | final BufferedOutputStream outputStream = openStream(remoteFile); |
161 | 162 | // When channel is broken, closing resource may hang, hence the time-out wrapper |
162 | | - // Note: closing FileObject will also close OutputStream |
163 | | - try (final TimeoutCloser ignored1 = new TimeoutCloser(outputStream); |
164 | | - final TimeoutCloser ignored2 = new TimeoutCloser(remoteFile)) { |
| 163 | + try (final TimeoutCloser ignored = new TimeoutCloser(outputStream)) { |
165 | 164 | appendFile(localTempFile, remoteFile, outputStream); |
166 | 165 | return null; |
167 | 166 | } |
| 167 | + finally { |
| 168 | + // closing sequentially |
| 169 | + new TimeoutCloser(remoteFile).close(); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void onRetry(Exception exception, int retryCount, int retryLimit, int retryWait) |
| 175 | + { |
| 176 | + super.onRetry(exception, retryCount, retryLimit, retryWait); |
| 177 | + // re-connect |
| 178 | + manager.close(); |
| 179 | + manager = initializeStandardFileSystemManager(); |
168 | 180 | } |
169 | 181 | }); |
170 | 182 | } |
@@ -256,18 +268,9 @@ FileObject resolve(final String remoteFilePath) throws FileSystemException |
256 | 268 | return manager.resolveFile(getSftpFileUri(remoteFilePath).toString(), fsOptions); |
257 | 269 | } |
258 | 270 |
|
259 | | - BufferedOutputStream openStream(final FileObject remoteFile) |
| 271 | + BufferedOutputStream openStream(final FileObject remoteFile) throws FileSystemException |
260 | 272 | { |
261 | | - // output stream is already a BufferedOutputStream, no need to wrap |
262 | | - final String taskName = "SFTP open stream"; |
263 | | - return withRetry(new DefaultRetry<BufferedOutputStream>(taskName) |
264 | | - { |
265 | | - @Override |
266 | | - public BufferedOutputStream call() throws Exception |
267 | | - { |
268 | | - return new BufferedOutputStream(remoteFile.getContent().getOutputStream()); |
269 | | - } |
270 | | - }); |
| 273 | + return new BufferedOutputStream(remoteFile.getContent().getOutputStream()); |
271 | 274 | } |
272 | 275 |
|
273 | 276 | URI getSftpFileUri(String remoteFilePath) |
|
0 commit comments