Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012-2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. 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.
  */
 
 
 
 package com.amazonaws.services.sqs.buffered;
 
 
QueueBufferFuture class is used to deliver asynchronous results of various QueueBuffer operations. QueueBufferFutures are not cancellable
 
 class QueueBufferFuture<Req extends AmazonWebServiceRequest, Res> implements Future<Res> {
     private Res result = null;
     private Exception e = null;
     private boolean done = false;
    
    
callback we should call after the future is done. may be null
 
     
     private final QueueBufferCallback<Req, Res> callback;
    
    
every future should hold a reference to the buffer that issued it. that way, even if all other references to the buffer are lost, it will not be garbage collected while at least one future it issued is still outstanding.
 
     private QueueBuffer issuingBuffer = null;
     
     public QueueBufferFuture() {
         this(null);
     }
     
     public QueueBufferFutureQueueBufferCallback<Req, Res > cb  ) {
          = cb;
     }

    
Report that the task this future represents has succeeded.
 
     public synchronized void setSuccess(Res paramResult) {
         if (return;  //can't mark done twice
          = paramResult;
          = true;
         notifyAll();
         
         //if we have a callback to call, schedule
         //it on a different thread. Who knows what this
         //thread is doing.
         if (  != null &&  != null ) {
             ..submit(new Callable<Void>() {
                 public Void call() throws Exception {
                     .onSuccess();
                     return null;
                 }
             });
 
         }
     }

    
Report that the task this future represents has failed.
 
     public synchronized void setFailure(Exception paramE) {
         if (return;  //can't mark done twice
          = paramE;
          = true;
         notifyAll();
         
         //if we have a callback to call, schedule
         //it on a different thread. Who knows what this
         //thread is doing.
         if (  != null &&  != null ) {
             ..submit(new Callable<Void>() {
                 public Void call() throws Exception {
                    .onError();
                    return null;
                }
            });
        }
    }
    @Override
    public boolean cancel(boolean arg0) {
        // not cancellable
        return false;
    }
    
    public void setBuffer ( QueueBuffer paramBuffer ) {
         = paramBuffer;
    }
    
    @Override
    public Res get() throws InterruptedExceptionExecutionException {
        while (true) {
            try {
                return get(..);
            } catch (TimeoutException te) {
                // shouldn't really happen, since we're specifying a very-very
                // long wait. but if it does, just loop
                // and wait more.
            }
        }
    }
    @Override
    public synchronized Res get(long timeoutTimeUnit tu)
            throws InterruptedExceptionExecutionException,
            TimeoutException {
        long waitStartMs = ..convert(System.nanoTime(), .);
        long timeoutMs = ..convert(timeouttu);
        long timeToWaitMs = timeoutMs;
        while (!) {
            //if timeToWaitMs is zero, we don't call wait() at all, because wait(0) means 
            //"wait forever", which is the opposite of what we want.
            if ( timeToWaitMs <= 0 ) {
                throw new TimeoutException("Timed out waiting for results after " + timeout + " " + tu);
            }
            
            wait(timeToWaitMs);
            
            //compute how long to wait in the next loop
            long nowMs = ..convert(System.nanoTime(), .);
            timeToWaitMs = timeoutMs - ( nowMs - waitStartMs  );
        }
        //if we got here, we are done.  Throw if there's anything to throw, 
        //otherwise return the result 
        if ( != null) {
            throw new ExecutionException();
        }
        // may be null, e.g. for Void futures 
        return ;
    }
    @Override
    public boolean isCancelled() {
        // not cancellable
        return false;
    }
    @Override
    public synchronized boolean isDone() {
        return ;
    }
New to GrepCode? Check out our FAQ X