001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.servlet.config.annotation;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.List;
022import java.util.concurrent.Callable;
023
024import org.springframework.core.task.AsyncTaskExecutor;
025import org.springframework.core.task.SimpleAsyncTaskExecutor;
026import org.springframework.web.context.request.async.CallableProcessingInterceptor;
027import org.springframework.web.context.request.async.DeferredResult;
028import org.springframework.web.context.request.async.DeferredResultProcessingInterceptor;
029import org.springframework.web.context.request.async.WebAsyncTask;
030
031/**
032 * Helps with configuring options for asynchronous request processing.
033 *
034 * @author Rossen Stoyanchev
035 * @since 3.2
036 */
037public class AsyncSupportConfigurer {
038
039        private AsyncTaskExecutor taskExecutor;
040
041        private Long timeout;
042
043        private final List<CallableProcessingInterceptor> callableInterceptors =
044                        new ArrayList<CallableProcessingInterceptor>();
045
046        private final List<DeferredResultProcessingInterceptor> deferredResultInterceptors =
047                        new ArrayList<DeferredResultProcessingInterceptor>();
048
049
050        /**
051         * Set the default {@link AsyncTaskExecutor} to use when a controller method
052         * returns a {@link Callable}. Controller methods can override this default on
053         * a per-request basis by returning a {@link WebAsyncTask}.
054         * <p>By default a {@link SimpleAsyncTaskExecutor} instance is used, and it's
055         * highly recommended to change that default in production since the simple
056         * executor does not re-use threads.
057         * @param taskExecutor the task executor instance to use by default
058         */
059        public AsyncSupportConfigurer setTaskExecutor(AsyncTaskExecutor taskExecutor) {
060                this.taskExecutor = taskExecutor;
061                return this;
062        }
063
064        /**
065         * Specify the amount of time, in milliseconds, before asynchronous request
066         * handling times out. In Servlet 3, the timeout begins after the main request
067         * processing thread has exited and ends when the request is dispatched again
068         * for further processing of the concurrently produced result.
069         * <p>If this value is not set, the default timeout of the underlying
070         * implementation is used, e.g. 10 seconds on Tomcat with Servlet 3.
071         * @param timeout the timeout value in milliseconds
072         */
073        public AsyncSupportConfigurer setDefaultTimeout(long timeout) {
074                this.timeout = timeout;
075                return this;
076        }
077
078        /**
079         * Configure lifecycle interceptors with callbacks around concurrent request
080         * execution that starts when a controller returns a
081         * {@link java.util.concurrent.Callable}.
082         * @param interceptors the interceptors to register
083         */
084        public AsyncSupportConfigurer registerCallableInterceptors(CallableProcessingInterceptor... interceptors) {
085                this.callableInterceptors.addAll(Arrays.asList(interceptors));
086                return this;
087        }
088
089        /**
090         * Configure lifecycle interceptors with callbacks around concurrent request
091         * execution that starts when a controller returns a {@link DeferredResult}.
092         * @param interceptors the interceptors to register
093         */
094        public AsyncSupportConfigurer registerDeferredResultInterceptors(DeferredResultProcessingInterceptor... interceptors) {
095                this.deferredResultInterceptors.addAll(Arrays.asList(interceptors));
096                return this;
097        }
098
099
100        protected AsyncTaskExecutor getTaskExecutor() {
101                return this.taskExecutor;
102        }
103
104        protected Long getTimeout() {
105                return this.timeout;
106        }
107
108        protected List<CallableProcessingInterceptor> getCallableInterceptors() {
109                return this.callableInterceptors;
110        }
111
112        protected List<DeferredResultProcessingInterceptor> getDeferredResultInterceptors() {
113                return this.deferredResultInterceptors;
114        }
115
116}