Skip to content

Commit 8854c33

Browse files
committed
Rearranged WebAsyncTask constructor interdependencies and assertions
Issue: SPR-12457 (cherry picked from commit aadb93f)
1 parent f093a5f commit 8854c33

File tree

1 file changed

+46
-44
lines changed
  • spring-web/src/main/java/org/springframework/web/context/request/async

1 file changed

+46
-44
lines changed

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

+46-44
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-2014 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,11 +13,13 @@
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.concurrent.Callable;
1920

2021
import org.springframework.beans.factory.BeanFactory;
22+
import org.springframework.beans.factory.BeanFactoryAware;
2123
import org.springframework.core.task.AsyncTaskExecutor;
2224
import org.springframework.util.Assert;
2325
import org.springframework.web.context.request.NativeWebRequest;
@@ -26,140 +28,140 @@
2628
* Holder for a {@link Callable}, a timeout value, and a task executor.
2729
*
2830
* @author Rossen Stoyanchev
31+
* @author Juergen Hoeller
2932
* @since 3.2
3033
*/
31-
public class WebAsyncTask<V> {
34+
public class WebAsyncTask<V> implements BeanFactoryAware {
3235

3336
private final Callable<V> callable;
3437

35-
private final Long timeout;
38+
private Long timeout;
39+
40+
private AsyncTaskExecutor executor;
3641

37-
private final String executorName;
42+
private String executorName;
3843

39-
private final AsyncTaskExecutor executor;
44+
private BeanFactory beanFactory;
4045

4146
private Callable<V> timeoutCallback;
4247

4348
private Runnable completionCallback;
4449

45-
private BeanFactory beanFactory;
46-
4750

4851
/**
4952
* Create a {@code WebAsyncTask} wrapping the given {@link Callable}.
5053
* @param callable the callable for concurrent handling
5154
*/
5255
public WebAsyncTask(Callable<V> callable) {
53-
this(null, null, null, callable);
56+
Assert.notNull(callable, "Callable must not be null");
57+
this.callable = callable;
5458
}
5559

5660
/**
5761
* Create a {@code WebAsyncTask} with a timeout value and a {@link Callable}.
58-
* @param timeout timeout value in milliseconds
62+
* @param timeout a timeout value in milliseconds
5963
* @param callable the callable for concurrent handling
6064
*/
6165
public WebAsyncTask(long timeout, Callable<V> callable) {
62-
this(timeout, null, null, callable);
66+
this(callable);
67+
this.timeout = timeout;
6368
}
6469

6570
/**
6671
* Create a {@code WebAsyncTask} with a timeout value, an executor name, and a {@link Callable}.
6772
* @param timeout timeout value in milliseconds; ignored if {@code null}
73+
* @param executorName the name of an executor bean to use
6874
* @param callable the callable for concurrent handling
6975
*/
7076
public WebAsyncTask(Long timeout, String executorName, Callable<V> callable) {
71-
this(timeout, null, executorName, callable);
72-
Assert.notNull(executor, "Executor name must not be null");
77+
this(callable);
78+
Assert.notNull(executorName, "Executor name must not be null");
79+
this.executorName = executorName;
80+
this.timeout = timeout;
7381
}
7482

7583
/**
7684
* Create a {@code WebAsyncTask} with a timeout value, an executor instance, and a Callable.
7785
* @param timeout timeout value in milliseconds; ignored if {@code null}
86+
* @param executor the executor to use
7887
* @param callable the callable for concurrent handling
7988
*/
8089
public WebAsyncTask(Long timeout, AsyncTaskExecutor executor, Callable<V> callable) {
81-
this(timeout, executor, null, callable);
90+
this(callable);
8291
Assert.notNull(executor, "Executor must not be null");
83-
}
84-
85-
private WebAsyncTask(Long timeout, AsyncTaskExecutor executor, String executorName, Callable<V> callable) {
86-
Assert.notNull(callable, "Callable must not be null");
87-
this.callable = callable;
88-
this.timeout = timeout;
8992
this.executor = executor;
90-
this.executorName = executorName;
93+
this.timeout = timeout;
9194
}
9295

9396

9497
/**
95-
* Return the {@link Callable} to use for concurrent handling, never {@code null}.
98+
* Return the {@link Callable} to use for concurrent handling (never {@code null}).
9699
*/
97100
public Callable<?> getCallable() {
98101
return this.callable;
99102
}
100103

101104
/**
102-
* Return the timeout value in milliseconds or {@code null} if not value is set.
105+
* Return the timeout value in milliseconds, or {@code null} if no timeout is set.
103106
*/
104107
public Long getTimeout() {
105108
return this.timeout;
106109
}
107110

108111
/**
109-
* Return the AsyncTaskExecutor to use for concurrent handling, or {@code null}.
112+
* A {@link BeanFactory} to use for resolving an executor name.
113+
* <p>This factory reference will automatically be set when
114+
* {@code WebAsyncTask} is used within a Spring MVC controller.
115+
*/
116+
public void setBeanFactory(BeanFactory beanFactory) {
117+
this.beanFactory = beanFactory;
118+
}
119+
120+
/**
121+
* Return the AsyncTaskExecutor to use for concurrent handling,
122+
* or {@code null} if none specified.
110123
*/
111124
public AsyncTaskExecutor getExecutor() {
112125
if (this.executor != null) {
113126
return this.executor;
114127
}
115128
else if (this.executorName != null) {
116-
Assert.state(this.beanFactory != null, "A BeanFactory is required to look up a task executor bean");
129+
Assert.state(this.beanFactory != null, "BeanFactory is required to look up an executor bean by name");
117130
return this.beanFactory.getBean(this.executorName, AsyncTaskExecutor.class);
118131
}
119132
else {
120133
return null;
121134
}
122135
}
123136

124-
/**
125-
* A {@link BeanFactory} to use to resolve an executor name. Applications are
126-
* not expected to have to set this property when {@code WebAsyncTask} is used in a
127-
* Spring MVC controller.
128-
*/
129-
public void setBeanFactory(BeanFactory beanFactory) {
130-
this.beanFactory = beanFactory;
131-
}
132-
133137

134138
/**
135-
* Register code to invoke when the async request times out. This method is
136-
* called from a container thread when an async request times out before the
137-
* {@code Callable} has completed. The callback is executed in the same
138-
* thread and therefore should return without blocking. It may return an
139-
* alternative value to use, including an {@link Exception} or return
139+
* Register code to invoke when the async request times out.
140+
* <p>This method is called from a container thread when an async request times
141+
* out before the {@code Callable} has completed. The callback is executed in
142+
* the same thread and therefore should return without blocking. It may return
143+
* an alternative value to use, including an {@link Exception} or return
140144
* {@link CallableProcessingInterceptor#RESULT_NONE RESULT_NONE}.
141145
*/
142146
public void onTimeout(Callable<V> callback) {
143147
this.timeoutCallback = callback;
144148
}
145149

146150
/**
147-
* Register code to invoke when the async request completes. This method is
148-
* called from a container thread when an async request completed for any
149-
* reason including timeout and network error.
151+
* Register code to invoke when the async request completes.
152+
* <p>This method is called from a container thread when an async request
153+
* completed for any reason, including timeout and network error.
150154
*/
151155
public void onCompletion(Runnable callback) {
152156
this.completionCallback = callback;
153157
}
154158

155159
CallableProcessingInterceptor getInterceptor() {
156160
return new CallableProcessingInterceptorAdapter() {
157-
158161
@Override
159162
public <T> Object handleTimeout(NativeWebRequest request, Callable<T> task) throws Exception {
160-
return (timeoutCallback != null) ? timeoutCallback.call() : CallableProcessingInterceptor.RESULT_NONE;
163+
return (timeoutCallback != null ? timeoutCallback.call() : CallableProcessingInterceptor.RESULT_NONE);
161164
}
162-
163165
@Override
164166
public <T> void afterCompletion(NativeWebRequest request, Callable<T> task) throws Exception {
165167
if (completionCallback != null) {

0 commit comments

Comments
 (0)