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