Skip to content

Commit 5fc0d43

Browse files
committed
AsyncResult properly propagates execution exception
Issue: SPR-14249 (cherry picked from commit 1b1aac9)
1 parent 2dbc8b0 commit 5fc0d43

File tree

2 files changed

+101
-5
lines changed

2 files changed

+101
-5
lines changed

spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncResult.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@
3737
* to the caller.
3838
*
3939
* @author Juergen Hoeller
40+
* @author Rossen Stoyanchev
4041
* @since 3.0
4142
* @see Async
4243
* @see #forValue(Object)
@@ -102,11 +103,17 @@ public void addCallback(ListenableFutureCallback<? super V> callback) {
102103

103104
@Override
104105
public void addCallback(SuccessCallback<? super V> successCallback, FailureCallback failureCallback) {
105-
try {
106-
successCallback.onSuccess(this.value);
106+
if (this.executionException != null) {
107+
Throwable cause = this.executionException.getCause();
108+
failureCallback.onFailure(cause != null ? cause : this.executionException);
107109
}
108-
catch (Throwable ex) {
109-
failureCallback.onFailure(ex);
110+
else {
111+
try {
112+
successCallback.onSuccess(this.value);
113+
}
114+
catch (Throwable ex) {
115+
failureCallback.onFailure(ex);
116+
}
110117
}
111118
}
112119

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.scheduling.annotation;
18+
19+
import java.io.IOException;
20+
import java.util.HashSet;
21+
import java.util.Set;
22+
23+
import org.junit.Test;
24+
25+
import org.springframework.util.concurrent.ListenableFuture;
26+
import org.springframework.util.concurrent.ListenableFutureCallback;
27+
28+
import static org.junit.Assert.*;
29+
30+
/**
31+
* @author Juergen Hoeller
32+
*/
33+
public class AsyncResultTests {
34+
35+
@Test
36+
public void asyncResultWithCallbackAndValue() {
37+
String value = "val";
38+
final Set<String> values = new HashSet<>(1);
39+
ListenableFuture<String> future = AsyncResult.forValue(value);
40+
future.addCallback(new ListenableFutureCallback<String>() {
41+
@Override
42+
public void onSuccess(String result) {
43+
values.add(result);
44+
}
45+
@Override
46+
public void onFailure(Throwable ex) {
47+
fail("Failure callback not expected: " + ex);
48+
}
49+
});
50+
assertSame(value, values.iterator().next());
51+
}
52+
53+
@Test
54+
public void asyncResultWithCallbackAndException() {
55+
IOException ex = new IOException();
56+
final Set<Throwable> values = new HashSet<>(1);
57+
ListenableFuture<String> future = AsyncResult.forExecutionException(ex);
58+
future.addCallback(new ListenableFutureCallback<String>() {
59+
@Override
60+
public void onSuccess(String result) {
61+
fail("Success callback not expected: " + result);
62+
}
63+
@Override
64+
public void onFailure(Throwable ex) {
65+
values.add(ex);
66+
}
67+
});
68+
assertSame(ex, values.iterator().next());
69+
}
70+
71+
@Test
72+
public void asyncResultWithSeparateCallbacksAndValue() {
73+
String value = "val";
74+
final Set<String> values = new HashSet<>(1);
75+
ListenableFuture<String> future = AsyncResult.forValue(value);
76+
future.addCallback(values::add, (ex) -> fail("Failure callback not expected: " + ex));
77+
assertSame(value, values.iterator().next());
78+
}
79+
80+
@Test
81+
public void asyncResultWithSeparateCallbacksAndException() {
82+
IOException ex = new IOException();
83+
final Set<Throwable> values = new HashSet<>(1);
84+
ListenableFuture<String> future = AsyncResult.forExecutionException(ex);
85+
future.addCallback((result) -> fail("Success callback not expected: " + result), values::add);
86+
assertSame(ex, values.iterator().next());
87+
}
88+
89+
}

0 commit comments

Comments
 (0)