001/*
002 * Copyright 2002-2018 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.scheduling.annotation;
018
019import java.util.Collection;
020import java.util.concurrent.Executor;
021import java.util.function.Supplier;
022
023import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
024import org.springframework.beans.factory.annotation.Autowired;
025import org.springframework.context.annotation.Configuration;
026import org.springframework.context.annotation.ImportAware;
027import org.springframework.core.annotation.AnnotationAttributes;
028import org.springframework.core.type.AnnotationMetadata;
029import org.springframework.lang.Nullable;
030import org.springframework.util.CollectionUtils;
031
032/**
033 * Abstract base {@code Configuration} class providing common structure for enabling
034 * Spring's asynchronous method execution capability.
035 *
036 * @author Chris Beams
037 * @author Juergen Hoeller
038 * @author Stephane Nicoll
039 * @since 3.1
040 * @see EnableAsync
041 */
042@Configuration
043public abstract class AbstractAsyncConfiguration implements ImportAware {
044
045        @Nullable
046        protected AnnotationAttributes enableAsync;
047
048        @Nullable
049        protected Supplier<Executor> executor;
050
051        @Nullable
052        protected Supplier<AsyncUncaughtExceptionHandler> exceptionHandler;
053
054
055        @Override
056        public void setImportMetadata(AnnotationMetadata importMetadata) {
057                this.enableAsync = AnnotationAttributes.fromMap(
058                                importMetadata.getAnnotationAttributes(EnableAsync.class.getName(), false));
059                if (this.enableAsync == null) {
060                        throw new IllegalArgumentException(
061                                        "@EnableAsync is not present on importing class " + importMetadata.getClassName());
062                }
063        }
064
065        /**
066         * Collect any {@link AsyncConfigurer} beans through autowiring.
067         */
068        @Autowired(required = false)
069        void setConfigurers(Collection<AsyncConfigurer> configurers) {
070                if (CollectionUtils.isEmpty(configurers)) {
071                        return;
072                }
073                if (configurers.size() > 1) {
074                        throw new IllegalStateException("Only one AsyncConfigurer may exist");
075                }
076                AsyncConfigurer configurer = configurers.iterator().next();
077                this.executor = configurer::getAsyncExecutor;
078                this.exceptionHandler = configurer::getAsyncUncaughtExceptionHandler;
079        }
080
081}