Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2011-2015 Amazon Technologies, Inc.
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at:
  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  * OR CONDITIONS OF ANY KIND, either express or implied. See the
  * License for the specific language governing permissions and
  * limitations under the License.
 import static com.amazonaws.event.SDKProgressPublisher.publishProgress;
 import java.util.List;
Manages an upload by periodically checking to see if the upload is done, and returning a result if so. Otherwise, schedules a copy of itself to be run in the future and returns null. When waiting on the result of this class via a Future object, clients must call isDone() and getFuture()
 public class UploadMonitor implements Callable<UploadResult>, TransferMonitor {
     private final AmazonS3 s3;
     private final PutObjectRequest origReq;
     private final ProgressListenerChain listener;
     private final UploadCallable multipartUploadCallable;
     private final UploadImpl transfer;
     private final ExecutorService threadPool;
      * Futures of threads that upload the parts.
     private final List<Future<PartETag>> futures = Collections
             .synchronizedList(new ArrayList<Future<PartETag>>());
      * State for clients wishing to poll for completion
     private boolean isUploadDone = false;
     private Future<UploadResultfuture;
     public synchronized Future<UploadResultgetFuture() {
         return ;
     private synchronized void setFuture(Future<UploadResultfuture) {
         this. = future;
     private synchronized void cancelFuture() {
     public synchronized boolean isDone() {
         return ;
     private synchronized void markAllDone() {
          = true;

Constructs a new upload watcher and then immediately submits it to the thread pool.

manager The that owns this upload.
transfer The transfer being processed.
threadPool The java.util.concurrent.ExecutorService to which we should submit new tasks.
multipartUploadCallable The callable responsible for processing the upload asynchronously
putObjectRequest The original putObject request
progressListenerChain A chain of listeners that wish to be notified of upload progress
    public static UploadMonitor create(
            TransferManager manager,
            UploadImpl transfer,
            ExecutorService threadPool,
            UploadCallable multipartUploadCallable,
            PutObjectRequest putObjectRequest,
            ProgressListenerChain progressListenerChain) {
        UploadMonitor uploadMonitor = new UploadMonitor(managertransfer,
        return uploadMonitor;
    private UploadMonitor(TransferManager managerUploadImpl transferExecutorService threadPool,
            UploadCallable multipartUploadCallablePutObjectRequest putObjectRequest,
            ProgressListenerChain progressListenerChain) {
        this. = manager.getAmazonS3Client();
        this. = multipartUploadCallable;
        this. = putObjectRequest;
        this. = progressListenerChain;
        this. = transfer;
        this. = threadPool;
    public UploadResult call() throws Exception {
        try {
            UploadResult result = .call();

If the result is null, it is a mutli part parellel upload. So, an new task is submitted for initiating a complete multi part upload request.
            if (result == null) {
                setFuture(.submit(new CompleteMultipartUpload(
                        .getMultipartUploadId(), ,
                                .getETags(), this)));
            } else {
            return result;
        } catch (CancellationException e) {
            throw new AmazonClientException("Upload canceled");
        } catch (Exception e) {
            throw e;
    void uploadComplete() {
        // AmazonS3Client takes care of all the events for single part uploads,
        // so we only need to send a completed event for multipart uploads.

Cancels the futures in the following cases - If the user has requested for forcefully aborting the transfers. - If the upload is a multi part parellel upload. - If the upload operation hasn't started. Cancels all the in flight transfers of the upload if applicable. Returns the multi-part upload Id in case of the parallel multi-part uploads. Returns null otherwise.
    PauseResult<PersistableUploadpause(boolean forceCancel) {
        PersistableUpload persistableUpload = 
        if (persistableUpload == null) {
            PauseStatus pauseStatus = TransferManagerUtils
                    .determinePauseStatus(.getState(), forceCancel);
            if (forceCancel) {
            return new PauseResult<PersistableUpload>(pauseStatus);
        return new PauseResult<PersistableUpload>(.,

Cancels the inflight transfers if they are not completed.
    private void cancelFutures() {
        for (Future<PartETagf : ) {

Cancels all the futures associated with this upload operation. Also cleans up the parts on Amazon S3 if the uplaod is performed as a multi-part upload operation.
    void performAbort() {
New to GrepCode? Check out our FAQ X