Skip to content

Commit 66d8c28

Browse files
committed
ScheduledAnnotationBeanPostProcessor falls back to "taskScheduler" bean by name
Issue: SPR-13236
1 parent 0cce41e commit 66d8c28

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ public class ScheduledAnnotationBeanPostProcessor implements BeanPostProcessor,
8787
EmbeddedValueResolverAware, BeanFactoryAware, ApplicationContextAware,
8888
SmartInitializingSingleton, ApplicationListener<ContextRefreshedEvent>, DisposableBean {
8989

90+
/**
91+
* The default name of the TaskScheduler bean to pick up: "taskScheduler".
92+
* <p>Note that the initial lookup happens by type; this is just the fallback
93+
* in case of multiple scheduler beans found in the context.
94+
*/
95+
public static final String DEFAULT_TASK_SCHEDULER_BEAN_NAME = "taskScheduler";
96+
97+
9098
protected final Log logger = LogFactory.getLog(getClass());
9199

92100
private Object scheduler;
@@ -181,12 +189,19 @@ private void finishRegistration() {
181189
Assert.state(this.beanFactory != null, "BeanFactory must be set to find scheduler by type");
182190
try {
183191
// Search for TaskScheduler bean...
184-
this.registrar.setScheduler(this.beanFactory.getBean(TaskScheduler.class));
192+
this.registrar.setTaskScheduler(this.beanFactory.getBean(TaskScheduler.class));
185193
}
186194
catch (NoUniqueBeanDefinitionException ex) {
187-
throw new IllegalStateException("More than one TaskScheduler exists within the context. " +
188-
"Remove all but one of the beans; or implement the SchedulingConfigurer interface and call " +
189-
"ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback.", ex);
195+
try {
196+
this.registrar.setTaskScheduler(
197+
this.beanFactory.getBean(DEFAULT_TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class));
198+
}
199+
catch (NoSuchBeanDefinitionException ex2) {
200+
throw new IllegalStateException("More than one TaskScheduler bean exists within the context, and " +
201+
"none is named 'taskScheduler'. Mark one of them as primary or name it 'taskScheduler' "+
202+
"(possibly as an alias); or implement the SchedulingConfigurer interface and call " +
203+
"ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback.", ex);
204+
}
190205
}
191206
catch (NoSuchBeanDefinitionException ex) {
192207
logger.debug("Could not find default TaskScheduler bean", ex);
@@ -195,9 +210,10 @@ private void finishRegistration() {
195210
this.registrar.setScheduler(this.beanFactory.getBean(ScheduledExecutorService.class));
196211
}
197212
catch (NoUniqueBeanDefinitionException ex2) {
198-
throw new IllegalStateException("More than one ScheduledExecutorService exists within the context. " +
199-
"Remove all but one of the beans; or implement the SchedulingConfigurer interface and call " +
200-
"ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback.", ex);
213+
throw new IllegalStateException("More than one ScheduledExecutorService bean exists within " +
214+
"the context. Mark one of them as primary; or implement the SchedulingConfigurer " +
215+
"interface and call ScheduledTaskRegistrar#setScheduler explicitly within the " +
216+
"configureTasks() callback.", ex);
201217
}
202218
catch (NoSuchBeanDefinitionException ex2) {
203219
logger.debug("Could not find default ScheduledExecutorService bean", ex);

0 commit comments

Comments
 (0)