Browse Source

DeferredResult accessors based on volatile fields for proper visibility

Issue: SPR-13451
(cherry picked from commit 045016e)
pull/1057/head
Juergen Hoeller 11 years ago
parent
commit
73a2407545
  1. 23
      spring-web/src/main/java/org/springframework/web/context/request/async/DeferredResult.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 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.
@ -13,6 +13,7 @@ @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.context.request.async;
import java.util.PriorityQueue;
@ -20,6 +21,7 @@ import java.util.concurrent.Callable; @@ -20,6 +21,7 @@ import java.util.concurrent.Callable;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.web.context.request.NativeWebRequest;
@ -63,9 +65,9 @@ public class DeferredResult<T> { @@ -63,9 +65,9 @@ public class DeferredResult<T> {
private DeferredResultHandler resultHandler;
private Object result = RESULT_NONE;
private volatile Object result = RESULT_NONE;
private boolean expired;
private volatile boolean expired;
/**
@ -94,17 +96,17 @@ public class DeferredResult<T> { @@ -94,17 +96,17 @@ public class DeferredResult<T> {
this.timeout = timeout;
}
/**
* Return {@code true} if this DeferredResult is no longer usable either
* because it was previously set or because the underlying request expired.
* <p>
* The result may have been set with a call to {@link #setResult(Object)},
* <p>The result may have been set with a call to {@link #setResult(Object)},
* or {@link #setErrorResult(Object)}, or as a result of a timeout, if a
* timeout result was provided to the constructor. The request may also
* expire due to a timeout or network error.
*/
public final boolean isSetOrExpired() {
return ((this.result != RESULT_NONE) || this.expired);
return (this.result != RESULT_NONE || this.expired);
}
/**
@ -145,12 +147,12 @@ public class DeferredResult<T> { @@ -145,12 +147,12 @@ public class DeferredResult<T> {
Assert.notNull(resultHandler, "DeferredResultHandler is required");
synchronized (this) {
this.resultHandler = resultHandler;
if ((this.result != RESULT_NONE) && (!this.expired)) {
if (this.result != RESULT_NONE && !this.expired) {
try {
this.resultHandler.handleResult(this.result);
}
catch (Throwable t) {
logger.trace("DeferredResult not handled", t);
catch (Throwable ex) {
logger.trace("DeferredResult not handled", ex);
}
}
}
@ -194,9 +196,9 @@ public class DeferredResult<T> { @@ -194,9 +196,9 @@ public class DeferredResult<T> {
return setResultInternal(result);
}
final DeferredResultProcessingInterceptor getInterceptor() {
return new DeferredResultProcessingInterceptorAdapter() {
@Override
public <S> boolean handleTimeout(NativeWebRequest request, DeferredResult<S> deferredResult) {
if (timeoutCallback != null) {
@ -207,7 +209,6 @@ public class DeferredResult<T> { @@ -207,7 +209,6 @@ public class DeferredResult<T> {
}
return true;
}
@Override
public <S> void afterCompletion(NativeWebRequest request, DeferredResult<S> deferredResult) {
synchronized (DeferredResult.this) {

Loading…
Cancel
Save