19
19
import java .util .PriorityQueue ;
20
20
import java .util .concurrent .Callable ;
21
21
import java .util .function .Consumer ;
22
+ import java .util .function .Supplier ;
23
+
22
24
23
25
import org .apache .commons .logging .Log ;
24
26
import org .apache .commons .logging .LogFactory ;
@@ -60,7 +62,7 @@ public class DeferredResult<T> {
60
62
@ Nullable
61
63
private final Long timeout ;
62
64
63
- private final Object timeoutResult ;
65
+ private final Supplier <?> timeoutResult ;
64
66
65
67
private Runnable timeoutCallback ;
66
68
@@ -79,18 +81,18 @@ public class DeferredResult<T> {
79
81
* Create a DeferredResult.
80
82
*/
81
83
public DeferredResult () {
82
- this (null , RESULT_NONE );
84
+ this (null , () -> RESULT_NONE );
83
85
}
84
86
85
87
/**
86
- * Create a DeferredResult with a timeout value.
88
+ * Create a DeferredResult with a custom timeout value.
87
89
* <p>By default not set in which case the default configured in the MVC
88
90
* Java Config or the MVC namespace is used, or if that's not set, then the
89
91
* timeout depends on the default of the underlying server.
90
92
* @param timeout timeout value in milliseconds
91
93
*/
92
94
public DeferredResult (Long timeout ) {
93
- this (timeout , RESULT_NONE );
95
+ this (timeout , () -> RESULT_NONE );
94
96
}
95
97
96
98
/**
@@ -99,11 +101,22 @@ public DeferredResult(Long timeout) {
99
101
* @param timeout timeout value in milliseconds (ignored if {@code null})
100
102
* @param timeoutResult the result to use
101
103
*/
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 ;
104
106
this .timeout = timeout ;
105
107
}
106
108
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
+ }
107
120
108
121
/**
109
122
* Return {@code true} if this DeferredResult is no longer usable either
@@ -281,10 +294,11 @@ public <S> boolean handleTimeout(NativeWebRequest request, DeferredResult<S> def
281
294
}
282
295
}
283
296
finally {
284
- if (timeoutResult != RESULT_NONE ) {
297
+ Object value = timeoutResult .get ();
298
+ if (value != RESULT_NONE ) {
285
299
continueProcessing = false ;
286
300
try {
287
- setResultInternal (timeoutResult );
301
+ setResultInternal (value );
288
302
}
289
303
catch (Throwable ex ) {
290
304
logger .debug ("Failed to handle timeout result" , ex );
0 commit comments