Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2010. Axon Framework
   *
   * 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
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License 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 org.axonframework.commandhandling.callbacks;
 
 
Command Handler Callback that allows the dispatching thread to wait for the result of the callback, using the Future mechanism. This callback allows the caller to synchronize calls when an asynchronous command bus is being used.

Parameters:
<R> the type of result of the command handling
Author(s):
Allard Buijze
Since:
0.6
 
 public class FutureCallback<R> implements CommandCallback<R>, Future<R> {
 
     private volatile R result;
     private volatile Throwable failure;
 
     private final CountDownLatch latch = new CountDownLatch(1);
 
     @Override
     public void onSuccess(R executionResult) {
         this. = executionResult;
         .countDown();
     }
 
     @Override
     public void onFailure(Throwable cause) {
         this. = cause;
         .countDown();
     }

    
Waits if necessary for the command handling to complete, and then returns its result.

Returns:
the result of the command handler execution.
Throws:
java.lang.InterruptedException if the current thread is interrupted while waiting
java.util.concurrent.ExecutionException if the command handler threw an exception
 
     @Override
     public R get() throws InterruptedExceptionExecutionException {
         .await();
         return doGetResult();
     }

    
Waits if necessary for at most the given time for the command handling to complete, and then retrieves its result, if available.

Parameters:
timeout the maximum time to wait
unit the time unit of the timeout argument
Returns:
the result of the command handler execution.
Throws:
java.lang.InterruptedException if the current thread is interrupted while waiting
java.util.concurrent.TimeoutException if the wait timed out
java.util.concurrent.ExecutionException if the command handler threw an exception
 
     @Override
     public R get(long timeoutTimeUnit unitthrows TimeoutExceptionInterruptedExceptionExecutionException {
         if (!.await(timeoutunit)) {
             throw new TimeoutException("The timeout of the getResult operation was reached");
         }
         return doGetResult();
     }

    
Always returns false, since command execution cannot be cancelled.

Parameters:
mayInterruptIfRunning true if the thread executing the command should be interrupted; otherwise, in-progress tasks are allowed to complete
Returns:
false
 
     @Override
     public boolean cancel(boolean mayInterruptIfRunning) {
         return false;
     }

    
Always returns false, since command execution cannot be cancelled.

Returns:
false
    @Override
    public boolean isCancelled() {
        return false;
    }

    
Indicates whether command handler execution has finished.

Returns:
true if command handler execution has finished, otherwise false.
    @Override
    public boolean isDone() {
        return .getCount() == 0L;
    }
    private R doGetResult() throws ExecutionException {
        if ( != null) {
            throw new ExecutionException();
        } else {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X