2015-05-01 25 views
0

我正在使用TransferManager將我的服務器上的某些文件異步上傳到S3。問題在於它出於未知原因而失敗,並且在日誌中沒有看到任何異常。使用AWS TransferManager上傳時無法找到堆棧跟蹤

在傳輸失敗時,我在文檔中看到有關查看異常的註釋。問題是這需要阻塞線程。

// Or you can block the current thread and wait for your transfer to 
// to complete. If the transfer fails, this method will throw an 
// AmazonClientException or AmazonServiceException detailing the reason. 
myUpload.waitForCompletion(); 

我試過使用ProgressListener,但似乎只在傳輸失敗時通知我。我沒有看到拋出任何異常。

有沒有一種方法來查看這些異常沒有阻塞?

回答

1

一旦ProgressListener通知您上傳已完成(或失敗),您可以撥打waitForCompletion以獲取結果,而不會阻止。

public class FailureListener extends SyncProgressListener { 

    public static void bind(Upload upload) { 
     upload.addProgressListener(new FailureListener(upload)); 
    } 

    private final Upload upload; 

    public FailureListener(Upload upload) { 
     this.upload = upload; 
    } 

    @Override 
    public void progressChanged(ProgressEvent progressEvent) { 
     if (progressEvent.getEventType() == ProgressEventType.TRANSFER_FAILED_EVENT) { 
      AmazonClientException e = getException(); 
      e.printStackTrace(); 
     } 
    } 

    private AmazonClientException getException() { 
     try { 
      // Won't actually "wait" since the transfer has already failed. 
      return upload.waitForException(); 
     } catch (InterruptedException e) { 
      Thread.currentThread().interrupt(); 
      throw new Error("WTF?", e); 
     } 
    } 
}