Skip to content

Commit 9922030

Browse files
committed
1 parent 48d365a commit 9922030

File tree

6 files changed

+397
-0
lines changed

6 files changed

+397
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.scheduling;
18+
19+
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
20+
21+
/**
22+
* Callback interface that can be used to customize a {@link ThreadPoolTaskScheduler}.
23+
*
24+
* @author Stephane Nicoll
25+
* @since 2.1.0
26+
*/
27+
@FunctionalInterface
28+
public interface TaskSchedulerCustomizer {
29+
30+
/**
31+
* Callback to customize a {@link ThreadPoolTaskScheduler} instance.
32+
* @param taskScheduler the task scheduler to customize
33+
*/
34+
void customize(ThreadPoolTaskScheduler taskScheduler);
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.scheduling;
18+
19+
import java.util.List;
20+
import java.util.function.Supplier;
21+
22+
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
23+
24+
/**
25+
* Factory to create {@link ThreadPoolTaskScheduler}.
26+
*
27+
* @author Stephane Nicoll
28+
*/
29+
class TaskSchedulerFactory implements Supplier<ThreadPoolTaskScheduler> {
30+
31+
private final TaskSchedulingProperties properties;
32+
33+
private final List<TaskSchedulerCustomizer> taskSchedulerCustomizers;
34+
35+
TaskSchedulerFactory(TaskSchedulingProperties properties,
36+
List<TaskSchedulerCustomizer> taskSchedulerCustomizers) {
37+
this.properties = properties;
38+
this.taskSchedulerCustomizers = taskSchedulerCustomizers;
39+
}
40+
41+
@Override
42+
public ThreadPoolTaskScheduler get() {
43+
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
44+
taskScheduler.setPoolSize(this.properties.getPool().getSize());
45+
taskScheduler.setThreadNamePrefix(this.properties.getThreadNamePrefix());
46+
47+
if (this.taskSchedulerCustomizers != null) {
48+
for (TaskSchedulerCustomizer customizer : this.taskSchedulerCustomizers) {
49+
customizer.customize(taskScheduler);
50+
}
51+
}
52+
return taskScheduler;
53+
}
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.scheduling;
18+
19+
import java.util.function.Supplier;
20+
import java.util.stream.Collectors;
21+
22+
import org.springframework.beans.factory.ObjectProvider;
23+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
26+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.scheduling.TaskScheduler;
30+
import org.springframework.scheduling.annotation.SchedulingConfigurer;
31+
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
32+
import org.springframework.scheduling.config.TaskManagementConfigUtils;
33+
34+
/**
35+
* {@link EnableAutoConfiguration Auto-configuration} for {@link TaskScheduler}.
36+
*
37+
* @author Stephane Nicoll
38+
* @since 2.1.0
39+
*/
40+
@Configuration
41+
@ConditionalOnBean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
42+
@EnableConfigurationProperties(TaskSchedulingProperties.class)
43+
public class TaskSchedulingAutoConfiguration {
44+
45+
@Bean
46+
@ConditionalOnMissingBean({ SchedulingConfigurer.class, TaskScheduler.class })
47+
public ThreadPoolTaskScheduler taskScheduler(
48+
Supplier<ThreadPoolTaskScheduler> taskSchedulerFactory) {
49+
return taskSchedulerFactory.get();
50+
}
51+
52+
@Bean
53+
@ConditionalOnMissingBean
54+
public Supplier<ThreadPoolTaskScheduler> taskSchedulerFactory(
55+
TaskSchedulingProperties properties,
56+
ObjectProvider<TaskSchedulerCustomizer> taskSchedulerCustomizers) {
57+
return new TaskSchedulerFactory(properties,
58+
taskSchedulerCustomizers.stream().collect(Collectors.toList()));
59+
}
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.scheduling;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* Configuration properties for task scheduling.
23+
*
24+
* @author Stephane Nicoll
25+
*/
26+
@ConfigurationProperties("spring.scheduling")
27+
public class TaskSchedulingProperties {
28+
29+
private final Pool pool = new Pool();
30+
31+
/**
32+
* Prefix to use for the names of newly created threads.
33+
*/
34+
private String threadNamePrefix = "scheduling-";
35+
36+
public Pool getPool() {
37+
return this.pool;
38+
}
39+
40+
public String getThreadNamePrefix() {
41+
return this.threadNamePrefix;
42+
}
43+
44+
public void setThreadNamePrefix(String threadNamePrefix) {
45+
this.threadNamePrefix = threadNamePrefix;
46+
}
47+
48+
public static class Pool {
49+
50+
/**
51+
* Maximum allowed number of threads.
52+
*/
53+
private int size = 1;
54+
55+
public int getSize() {
56+
return this.size;
57+
}
58+
59+
public void setSize(int size) {
60+
this.size = size;
61+
}
62+
63+
}
64+
65+
}

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
9696
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
9797
org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\
9898
org.springframework.boot.autoconfigure.reactor.core.ReactorCoreAutoConfiguration,\
99+
org.springframework.boot.autoconfigure.scheduling.TaskSchedulingAutoConfiguration,\
99100
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
100101
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\
101102
org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration,\

0 commit comments

Comments
 (0)