Skip to content

Commit 73a2407

Browse files
committed
DeferredResult accessors based on volatile fields for proper visibility
Issue: SPR-13451 (cherry picked from commit 045016e)
1 parent b15da37 commit 73a2407

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -13,13 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.web.context.request.async;
1718

1819
import java.util.PriorityQueue;
1920
import java.util.concurrent.Callable;
2021

2122
import org.apache.commons.logging.Log;
2223
import org.apache.commons.logging.LogFactory;
24+
2325
import org.springframework.util.Assert;
2426
import org.springframework.web.context.request.NativeWebRequest;
2527

@@ -63,9 +65,9 @@ public class DeferredResult<T> {
6365

6466
private DeferredResultHandler resultHandler;
6567

66-
private Object result = RESULT_NONE;
68+
private volatile Object result = RESULT_NONE;
6769

68-
private boolean expired;
70+
private volatile boolean expired;
6971

7072

7173
/**
@@ -94,17 +96,17 @@ public DeferredResult(Long timeout, Object timeoutResult) {
9496
this.timeout = timeout;
9597
}
9698

99+
97100
/**
98101
* Return {@code true} if this DeferredResult is no longer usable either
99102
* because it was previously set or because the underlying request expired.
100-
* <p>
101-
* The result may have been set with a call to {@link #setResult(Object)},
103+
* <p>The result may have been set with a call to {@link #setResult(Object)},
102104
* or {@link #setErrorResult(Object)}, or as a result of a timeout, if a
103105
* timeout result was provided to the constructor. The request may also
104106
* expire due to a timeout or network error.
105107
*/
106108
public final boolean isSetOrExpired() {
107-
return ((this.result != RESULT_NONE) || this.expired);
109+
return (this.result != RESULT_NONE || this.expired);
108110
}
109111

110112
/**
@@ -145,12 +147,12 @@ public final void setResultHandler(DeferredResultHandler resultHandler) {
145147
Assert.notNull(resultHandler, "DeferredResultHandler is required");
146148
synchronized (this) {
147149
this.resultHandler = resultHandler;
148-
if ((this.result != RESULT_NONE) && (!this.expired)) {
150+
if (this.result != RESULT_NONE && !this.expired) {
149151
try {
150152
this.resultHandler.handleResult(this.result);
151153
}
152-
catch (Throwable t) {
153-
logger.trace("DeferredResult not handled", t);
154+
catch (Throwable ex) {
155+
logger.trace("DeferredResult not handled", ex);
154156
}
155157
}
156158
}
@@ -194,9 +196,9 @@ public boolean setErrorResult(Object result) {
194196
return setResultInternal(result);
195197
}
196198

199+
197200
final DeferredResultProcessingInterceptor getInterceptor() {
198201
return new DeferredResultProcessingInterceptorAdapter() {
199-
200202
@Override
201203
public <S> boolean handleTimeout(NativeWebRequest request, DeferredResult<S> deferredResult) {
202204
if (timeoutCallback != null) {
@@ -207,7 +209,6 @@ public <S> boolean handleTimeout(NativeWebRequest request, DeferredResult<S> def
207209
}
208210
return true;
209211
}
210-
211212
@Override
212213
public <S> void afterCompletion(NativeWebRequest request, DeferredResult<S> deferredResult) {
213214
synchronized (DeferredResult.this) {

0 commit comments

Comments
 (0)