001/* 002 * Copyright 2002-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.scheduling.annotation; 018 019import java.util.concurrent.ExecutionException; 020import java.util.concurrent.Future; 021import java.util.concurrent.TimeUnit; 022 023import org.springframework.util.concurrent.FailureCallback; 024import org.springframework.util.concurrent.ListenableFuture; 025import org.springframework.util.concurrent.ListenableFutureCallback; 026import org.springframework.util.concurrent.SuccessCallback; 027 028/** 029 * A pass-through {@code Future} handle that can be used for method signatures 030 * which are declared with a {@code Future} return type for asynchronous execution. 031 * 032 * <p>As of Spring 4.1, this class implements {@link ListenableFuture}, not just 033 * plain {@link java.util.concurrent.Future}, along with the corresponding support 034 * in {@code @Async} processing. 035 * 036 * <p>As of Spring 4.2, this class also supports passing execution exceptions back 037 * to the caller. 038 * 039 * @author Juergen Hoeller 040 * @author Rossen Stoyanchev 041 * @since 3.0 042 * @see Async 043 * @see #forValue(Object) 044 * @see #forExecutionException(Throwable) 045 */ 046public class AsyncResult<V> implements ListenableFuture<V> { 047 048 private final V value; 049 050 private final ExecutionException executionException; 051 052 053 /** 054 * Create a new AsyncResult holder. 055 * @param value the value to pass through 056 */ 057 public AsyncResult(V value) { 058 this(value, null); 059 } 060 061 /** 062 * Create a new AsyncResult holder. 063 * @param value the value to pass through 064 */ 065 private AsyncResult(V value, ExecutionException ex) { 066 this.value = value; 067 this.executionException = ex; 068 } 069 070 071 @Override 072 public boolean cancel(boolean mayInterruptIfRunning) { 073 return false; 074 } 075 076 @Override 077 public boolean isCancelled() { 078 return false; 079 } 080 081 @Override 082 public boolean isDone() { 083 return true; 084 } 085 086 @Override 087 public V get() throws ExecutionException { 088 if (this.executionException != null) { 089 throw this.executionException; 090 } 091 return this.value; 092 } 093 094 @Override 095 public V get(long timeout, TimeUnit unit) throws ExecutionException { 096 return get(); 097 } 098 099 @Override 100 public void addCallback(ListenableFutureCallback<? super V> callback) { 101 addCallback(callback, callback); 102 } 103 104 @Override 105 public void addCallback(SuccessCallback<? super V> successCallback, FailureCallback failureCallback) { 106 try { 107 if (this.executionException != null) { 108 Throwable cause = this.executionException.getCause(); 109 failureCallback.onFailure(cause != null ? cause : this.executionException); 110 } 111 else { 112 successCallback.onSuccess(this.value); 113 } 114 } 115 catch (Throwable ex) { 116 // Ignore 117 } 118 } 119 120 121 /** 122 * Create a new async result which exposes the given value from {@link Future#get()}. 123 * @param value the value to expose 124 * @since 4.2 125 * @see Future#get() 126 */ 127 public static <V> ListenableFuture<V> forValue(V value) { 128 return new AsyncResult<V>(value, null); 129 } 130 131 /** 132 * Create a new async result which exposes the given exception as an 133 * {@link ExecutionException} from {@link Future#get()}. 134 * @param ex the exception to expose (either an pre-built {@link ExecutionException} 135 * or a cause to be wrapped in an {@link ExecutionException}) 136 * @since 4.2 137 * @see ExecutionException 138 */ 139 public static <V> ListenableFuture<V> forExecutionException(Throwable ex) { 140 return new AsyncResult<V>(null, 141 (ex instanceof ExecutionException ? (ExecutionException) ex : new ExecutionException(ex))); 142 } 143 144}