|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2014 the original author or authors. |
| 2 | + * Copyright 2002-2015 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import java.lang.annotation.Annotation;
|
20 | 20 | import java.util.concurrent.Executor;
|
21 | 21 |
|
| 22 | +import org.apache.commons.logging.Log; |
| 23 | +import org.apache.commons.logging.LogFactory; |
| 24 | + |
22 | 25 | import org.springframework.aop.framework.AbstractAdvisingBeanPostProcessor;
|
23 | 26 | import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
|
24 | 27 | import org.springframework.beans.factory.BeanFactory;
|
25 | 28 | import org.springframework.beans.factory.BeanFactoryAware;
|
| 29 | +import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| 30 | +import org.springframework.beans.factory.NoUniqueBeanDefinitionException; |
26 | 31 | import org.springframework.core.task.TaskExecutor;
|
27 | 32 | import org.springframework.util.Assert;
|
28 | 33 |
|
|
54 | 59 | * @see Async
|
55 | 60 | * @see AsyncAnnotationAdvisor
|
56 | 61 | * @see #setBeforeExistingAdvisors
|
| 62 | + * @see ScheduledAnnotationBeanPostProcessor |
57 | 63 | */
|
58 | 64 | @SuppressWarnings("serial")
|
59 | 65 | public class AsyncAnnotationBeanPostProcessor extends AbstractAdvisingBeanPostProcessor implements BeanFactoryAware {
|
60 | 66 |
|
| 67 | + /** |
| 68 | + * The default name of the {@link TaskExecutor} bean to pick up: "taskExecutor". |
| 69 | + * <p>Note that the initial lookup happens by type; this is just the fallback |
| 70 | + * in case of multiple executor beans found in the context. |
| 71 | + */ |
| 72 | + public static final String DEFAULT_TASK_EXECUTOR_BEAN_NAME = "taskExecutor"; |
| 73 | + |
| 74 | + |
| 75 | + protected final Log logger = LogFactory.getLog(getClass()); |
| 76 | + |
61 | 77 | private Class<? extends Annotation> asyncAnnotationType;
|
62 | 78 |
|
63 | 79 | private Executor executor;
|
@@ -100,9 +116,35 @@ public void setExceptionHandler(AsyncUncaughtExceptionHandler exceptionHandler)
|
100 | 116 | this.exceptionHandler = exceptionHandler;
|
101 | 117 | }
|
102 | 118 |
|
| 119 | + |
103 | 120 | @Override
|
104 | 121 | public void setBeanFactory(BeanFactory beanFactory) {
|
105 |
| - AsyncAnnotationAdvisor advisor = new AsyncAnnotationAdvisor(this.executor, this.exceptionHandler); |
| 122 | + Executor executorToUse = this.executor; |
| 123 | + if (executorToUse == null) { |
| 124 | + try { |
| 125 | + // Search for TaskExecutor bean... not plain Executor since that would |
| 126 | + // match with ScheduledExecutorService as well, which is unusable for |
| 127 | + // our purposes here. TaskExecutor is more clearly designed for it. |
| 128 | + executorToUse = beanFactory.getBean(TaskExecutor.class); |
| 129 | + } |
| 130 | + catch (NoUniqueBeanDefinitionException ex) { |
| 131 | + try { |
| 132 | + executorToUse = beanFactory.getBean(DEFAULT_TASK_EXECUTOR_BEAN_NAME, TaskExecutor.class); |
| 133 | + } |
| 134 | + catch (NoSuchBeanDefinitionException ex2) { |
| 135 | + throw new IllegalStateException("More than one TaskExecutor bean exists within the context, " + |
| 136 | + "and none is named 'taskExecutor'. Mark one of them as primary or name it " + |
| 137 | + "'taskExecutor' (possibly as an alias); or specify the AsyncConfigurer interface " + |
| 138 | + "and implement getAsyncExecutor() accordingly.", ex); |
| 139 | + } |
| 140 | + } |
| 141 | + catch (NoSuchBeanDefinitionException ex) { |
| 142 | + logger.debug("Could not find default TaskExecutor bean", ex); |
| 143 | + // Giving up -> falling back to default executor within the advisor... |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + AsyncAnnotationAdvisor advisor = new AsyncAnnotationAdvisor(executorToUse, this.exceptionHandler); |
106 | 148 | if (this.asyncAnnotationType != null) {
|
107 | 149 | advisor.setAsyncAnnotationType(this.asyncAnnotationType);
|
108 | 150 | }
|
|
0 commit comments