AsyncResult allows for exposing an execution exception
Issue: SPR-13076
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.scheduling.annotation;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.util.concurrent.FailureCallback;
|
||||
@@ -31,21 +33,37 @@ import org.springframework.util.concurrent.SuccessCallback;
|
||||
* plain {@link java.util.concurrent.Future}, along with the corresponding support
|
||||
* in {@code @Async} processing.
|
||||
*
|
||||
* <p>As of Spring 4.2, this class also supports passing execution exceptions back
|
||||
* to the caller.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
* @see Async
|
||||
* @see #forValue(Object)
|
||||
* @see #forExecutionException(Throwable)
|
||||
*/
|
||||
public class AsyncResult<V> implements ListenableFuture<V> {
|
||||
|
||||
private final V value;
|
||||
|
||||
private final ExecutionException executionException;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new AsyncResult holder.
|
||||
* @param value the value to pass through
|
||||
*/
|
||||
public AsyncResult(V value) {
|
||||
this(value, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new AsyncResult holder.
|
||||
* @param value the value to pass through
|
||||
*/
|
||||
private AsyncResult(V value, ExecutionException ex) {
|
||||
this.value = value;
|
||||
this.executionException = ex;
|
||||
}
|
||||
|
||||
|
||||
@@ -65,13 +83,16 @@ public class AsyncResult<V> implements ListenableFuture<V> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get() {
|
||||
public V get() throws ExecutionException {
|
||||
if (this.executionException != null) {
|
||||
throw this.executionException;
|
||||
}
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(long timeout, TimeUnit unit) {
|
||||
return this.value;
|
||||
public V get(long timeout, TimeUnit unit) throws ExecutionException {
|
||||
return get();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -89,4 +110,28 @@ public class AsyncResult<V> implements ListenableFuture<V> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new async result which exposes the given value from {@link Future#get()}.
|
||||
* @param value the value to expose
|
||||
* @since 4.2
|
||||
* @see Future#get()
|
||||
*/
|
||||
public static <V> ListenableFuture<V> forValue(V value) {
|
||||
return new AsyncResult<V>(value, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new async result which exposes the given exception as an
|
||||
* {@link ExecutionException} from {@link Future#get()}.
|
||||
* @param ex the exception to expose (either an pre-built {@link ExecutionException}
|
||||
* or a cause to be wrapped in an {@link ExecutionException})
|
||||
* @since 4.2
|
||||
* @see ExecutionException
|
||||
*/
|
||||
public static <V> ListenableFuture<V> forExecutionException(Throwable ex) {
|
||||
return new AsyncResult<V>(null,
|
||||
(ex instanceof ExecutionException ? (ExecutionException) ex : new ExecutionException(ex)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user