Skip to content

Commit

Permalink
Fix detection of ScheduledExecutorService
Browse files Browse the repository at this point in the history
`@EnableScheduling` detects primarily a suitable `TaskScheduler` in the
context and fallbacks to the presence of a `ScheduledExecutorService` if
that is not the case.

This commit improves the auto-configuration to back off when such
scheduled executor service is present, so that the framework
initialization code can pick it up as usual.

Closes spring-projectsgh-15032
  • Loading branch information
snicoll committed Nov 4, 2018
1 parent c790931 commit 0bd69fd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.boot.autoconfigure.task;

import java.util.concurrent.ScheduledExecutorService;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
Expand Down Expand Up @@ -44,7 +46,8 @@ public class TaskSchedulingAutoConfiguration {

@Bean
@ConditionalOnBean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
@ConditionalOnMissingBean({ SchedulingConfigurer.class, TaskScheduler.class })
@ConditionalOnMissingBean({ SchedulingConfigurer.class, TaskScheduler.class,
ScheduledExecutorService.class })
public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder builder) {
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import org.junit.Test;

Expand Down Expand Up @@ -96,6 +98,19 @@ public void enableSchedulingWithExistingTaskSchedulerBacksOff() {
});
}

@Test
public void enableSchedulingWithExistingScheduledExecutorServiceBacksOff() {
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class,
ScheduledExecutorServiceConfiguration.class).run((context) -> {
assertThat(context).doesNotHaveBean(TaskScheduler.class);
assertThat(context).hasSingleBean(ScheduledExecutorService.class);
TestBean bean = context.getBean(TestBean.class);
Thread.sleep(15);
assertThat(bean.threadNames)
.allMatch((name) -> name.contains("pool-"));
});
}

@Test
public void enableSchedulingWithConfigurerBacksOff() {
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class,
Expand Down Expand Up @@ -123,6 +138,16 @@ public TaskScheduler customTaskScheduler() {

}

@Configuration
static class ScheduledExecutorServiceConfiguration {

@Bean
public ScheduledExecutorService customScheduledExecutorService() {
return Executors.newScheduledThreadPool(2);
}

}

@Configuration
static class TaskSchedulerCustomizerConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6382,8 +6382,8 @@ A `ThreadPoolTaskScheduler` can also be auto-configured if need to be associated
scheduled task execution (`@EnableScheduling`). The thread pool uses one thread by default
and those settings can be fine-tuned using the `spring.task.scheduling` namespace.

Both a `TaskExecutorBuilder` bean and a `TaskSchedulerBuilder` bean are made available in the
context if a custom executor or scheduler needs to be created.
Both a `TaskExecutorBuilder` bean and a `TaskSchedulerBuilder` bean are made available in
the context if a custom executor or scheduler needs to be created.



Expand Down

0 comments on commit 0bd69fd

Please sign in to comment.