Skip to content

Commit 4ee704c

Browse files
陈灵敏rstoyanchev
authored andcommitted
Supplier for timeout result in DeferredResult
Issue: SPR-17364
1 parent 8df0bc8 commit 4ee704c

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

spring-web/src/main/java/org/springframework/web/context/request/async/DeferredResult.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.PriorityQueue;
2020
import java.util.concurrent.Callable;
2121
import java.util.function.Consumer;
22+
import java.util.function.Supplier;
23+
2224

2325
import org.apache.commons.logging.Log;
2426
import org.apache.commons.logging.LogFactory;
@@ -60,7 +62,7 @@ public class DeferredResult<T> {
6062
@Nullable
6163
private final Long timeout;
6264

63-
private final Object timeoutResult;
65+
private final Supplier<?> timeoutResult;
6466

6567
private Runnable timeoutCallback;
6668

@@ -79,18 +81,18 @@ public class DeferredResult<T> {
7981
* Create a DeferredResult.
8082
*/
8183
public DeferredResult() {
82-
this(null, RESULT_NONE);
84+
this(null, () -> RESULT_NONE);
8385
}
8486

8587
/**
86-
* Create a DeferredResult with a timeout value.
88+
* Create a DeferredResult with a custom timeout value.
8789
* <p>By default not set in which case the default configured in the MVC
8890
* Java Config or the MVC namespace is used, or if that's not set, then the
8991
* timeout depends on the default of the underlying server.
9092
* @param timeout timeout value in milliseconds
9193
*/
9294
public DeferredResult(Long timeout) {
93-
this(timeout, RESULT_NONE);
95+
this(timeout, () -> RESULT_NONE);
9496
}
9597

9698
/**
@@ -99,11 +101,22 @@ public DeferredResult(Long timeout) {
99101
* @param timeout timeout value in milliseconds (ignored if {@code null})
100102
* @param timeoutResult the result to use
101103
*/
102-
public DeferredResult(@Nullable Long timeout, Object timeoutResult) {
103-
this.timeoutResult = timeoutResult;
104+
public DeferredResult(@Nullable Long timeout, final Object timeoutResult) {
105+
this.timeoutResult = () -> timeoutResult;
104106
this.timeout = timeout;
105107
}
106108

109+
/**
110+
* Variant of {@link #DeferredResult(Long, Object)} that accepts a dynamic
111+
* fallback value based on a {@link Supplier}.
112+
* @param timeout timeout value in milliseconds (ignored if {@code null})
113+
* @param timeoutResult the result supplier to use
114+
* @since 5.1.1
115+
*/
116+
public DeferredResult(@Nullable Long timeout, Supplier<?> timeoutResult) {
117+
this.timeoutResult = timeoutResult;
118+
this.timeout = timeout;
119+
}
107120

108121
/**
109122
* Return {@code true} if this DeferredResult is no longer usable either
@@ -281,10 +294,11 @@ public <S> boolean handleTimeout(NativeWebRequest request, DeferredResult<S> def
281294
}
282295
}
283296
finally {
284-
if (timeoutResult != RESULT_NONE) {
297+
Object value = timeoutResult.get();
298+
if (value != RESULT_NONE) {
285299
continueProcessing = false;
286300
try {
287-
setResultInternal(timeoutResult);
301+
setResultInternal(value);
288302
}
289303
catch (Throwable ex) {
290304
logger.debug("Failed to handle timeout result", ex);

0 commit comments

Comments
 (0)