From be7b07f8327f1dc3b3fd78eb2041892114ba99a2 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Mon, 10 Dec 2012 13:17:35 -0600 Subject: [PATCH] Make DeferredResult extensible Previously it was cumbersome to associate data or behavior to a DeferredResult because it was marked as final and had no extension points. Now DeferredResult is non-final which allows subclasses to associate additional data and behavior to it. Issue: SPR-10059 --- .../context/request/async/DeferredResult.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/context/request/async/DeferredResult.java b/spring-web/src/main/java/org/springframework/web/context/request/async/DeferredResult.java index 5166ce192ec..a218beae77c 100644 --- a/spring-web/src/main/java/org/springframework/web/context/request/async/DeferredResult.java +++ b/spring-web/src/main/java/org/springframework/web/context/request/async/DeferredResult.java @@ -15,6 +15,7 @@ */ package org.springframework.web.context.request.async; +import java.util.PriorityQueue; import java.util.concurrent.Callable; import org.apache.commons.logging.Log; @@ -28,10 +29,24 @@ import org.springframework.web.context.request.NativeWebRequest; * concurrently on behalf of the application, with a {@code DeferredResult} the * application can produce the result from a thread of its choice. * + *

Subclasses can extend this class to easily associate additional data or + * behavior with the {@link DeferredResult}. For example, one might want to + * associate the user used to create the {@link DeferredResult} by extending the + * class and adding an addition property for the user. In this way, the user + * could easily be accessed later without the need to use a data structure to do + * the mapping. + * + *

An example of associating additional behavior to this class might be + * realized by extending the class to implement an additional interface. For + * example, one might want to implement a {@link Comparable} so that when the + * {@link DeferredResult} is added to a {@link PriorityQueue} it is handled in + * the correct order. + * * @author Rossen Stoyanchev + * @author Rob Winch * @since 3.2 */ -public final class DeferredResult { +public class DeferredResult { private static final Log logger = LogFactory.getLog(DeferredResult.class); @@ -88,14 +103,14 @@ public final class DeferredResult { * timeout result was provided to the constructor. The request may also * expire due to a timeout or network error. */ - public boolean isSetOrExpired() { + public final boolean isSetOrExpired() { return ((this.result != RESULT_NONE) || this.expired); } /** * Return the configured timeout value in milliseconds. */ - Long getTimeoutValue() { + final Long getTimeoutValue() { return this.timeout; } @@ -126,7 +141,7 @@ public final class DeferredResult { * @param resultHandler the handler * @see {@link DeferredResultProcessingInterceptor} */ - public void setResultHandler(DeferredResultHandler resultHandler) { + public final void setResultHandler(DeferredResultHandler resultHandler) { Assert.notNull(resultHandler, "DeferredResultHandler is required"); synchronized (this) { this.resultHandler = resultHandler; @@ -179,7 +194,7 @@ public final class DeferredResult { return setResultInternal(result); } - DeferredResultProcessingInterceptor getInterceptor() { + final DeferredResultProcessingInterceptor getInterceptor() { return new DeferredResultProcessingInterceptorAdapter() { @Override