Skip to content

Commit

Permalink
Merge pull request spring-projects#15951 from filiphr
Browse files Browse the repository at this point in the history
* pr/15951:
  Add support for task scheduling shutdown related properties
  Polish "Add support for task executor shutdown related properties"
  Add support for task executor shutdown related properties
  • Loading branch information
snicoll committed Feb 18, 2019
2 parents 9540905 + fa49dfc commit 35fc929
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.task.TaskExecutionProperties.Shutdown;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.task.TaskExecutorBuilder;
import org.springframework.boot.task.TaskExecutorCustomizer;
Expand Down Expand Up @@ -74,6 +75,9 @@ public TaskExecutorBuilder taskExecutorBuilder() {
builder = builder.maxPoolSize(pool.getMaxSize());
builder = builder.allowCoreThreadTimeOut(pool.isAllowCoreThreadTimeout());
builder = builder.keepAlive(pool.getKeepAlive());
Shutdown shutdown = this.properties.getShutdown();
builder = builder.awaitTermination(shutdown.isAwaitTermination());
builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod());
builder = builder.threadNamePrefix(this.properties.getThreadNamePrefix());
builder = builder.customizers(this.taskExecutorCustomizers);
builder = builder.taskDecorator(this.taskDecorator.getIfUnique());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,13 +24,16 @@
* Configuration properties for task execution.
*
* @author Stephane Nicoll
* @author Filip Hrisafov
* @since 2.1.0
*/
@ConfigurationProperties("spring.task.execution")
public class TaskExecutionProperties {

private final Pool pool = new Pool();

private final Shutdown shutdown = new Shutdown();

/**
* Prefix to use for the names of newly created threads.
*/
Expand All @@ -40,6 +43,10 @@ public Pool getPool() {
return this.pool;
}

public Shutdown getShutdown() {
return this.shutdown;
}

public String getThreadNamePrefix() {
return this.threadNamePrefix;
}
Expand Down Expand Up @@ -121,4 +128,34 @@ public void setKeepAlive(Duration keepAlive) {

}

public static class Shutdown {

/**
* Whether the executor should wait for scheduled tasks to complete on shutdown.
*/
private boolean awaitTermination;

/**
* Maximum time the executor should wait for remaining tasks to complete.
*/
private Duration awaitTerminationPeriod;

public boolean isAwaitTermination() {
return this.awaitTermination;
}

public void setAwaitTermination(boolean awaitTermination) {
this.awaitTermination = awaitTermination;
}

public Duration getAwaitTerminationPeriod() {
return this.awaitTerminationPeriod;
}

public void setAwaitTerminationPeriod(Duration awaitTerminationPeriod) {
this.awaitTerminationPeriod = awaitTerminationPeriod;
}

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.task.TaskSchedulingProperties.Shutdown;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.task.TaskSchedulerBuilder;
import org.springframework.boot.task.TaskSchedulerCustomizer;
Expand Down Expand Up @@ -58,6 +59,9 @@ public TaskSchedulerBuilder taskSchedulerBuilder(TaskSchedulingProperties proper
ObjectProvider<TaskSchedulerCustomizer> taskSchedulerCustomizers) {
TaskSchedulerBuilder builder = new TaskSchedulerBuilder();
builder = builder.poolSize(properties.getPool().getSize());
Shutdown shutdown = properties.getShutdown();
builder = builder.awaitTermination(shutdown.isAwaitTermination());
builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod());
builder = builder.threadNamePrefix(properties.getThreadNamePrefix());
builder = builder.customizers(taskSchedulerCustomizers);
return builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,8 @@

package org.springframework.boot.autoconfigure.task;

import java.time.Duration;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
Expand All @@ -29,6 +31,8 @@ public class TaskSchedulingProperties {

private final Pool pool = new Pool();

private final Shutdown shutdown = new Shutdown();

/**
* Prefix to use for the names of newly created threads.
*/
Expand All @@ -38,6 +42,10 @@ public Pool getPool() {
return this.pool;
}

public Shutdown getShutdown() {
return this.shutdown;
}

public String getThreadNamePrefix() {
return this.threadNamePrefix;
}
Expand All @@ -63,4 +71,34 @@ public void setSize(int size) {

}

public static class Shutdown {

/**
* Whether the executor should wait for scheduled tasks to complete on shutdown.
*/
private boolean awaitTermination;

/**
* Maximum time the executor should wait for remaining tasks to complete.
*/
private Duration awaitTerminationPeriod;

public boolean isAwaitTermination() {
return this.awaitTermination;
}

public void setAwaitTermination(boolean awaitTermination) {
this.awaitTermination = awaitTermination;
}

public Duration getAwaitTerminationPeriod() {
return this.awaitTerminationPeriod;
}

public void setAwaitTerminationPeriod(Duration awaitTerminationPeriod) {
this.awaitTerminationPeriod = awaitTerminationPeriod;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public void taskExecutorBuilderShouldApplyCustomSettings() {
"spring.task.execution.pool.max-size=4",
"spring.task.execution.pool.allow-core-thread-timeout=true",
"spring.task.execution.pool.keep-alive=5s",
"spring.task.execution.shutdown.await-termination=true",
"spring.task.execution.shutdown.await-termination-period=30s",
"spring.task.execution.thread-name-prefix=mytest-")
.run(assertTaskExecutor((taskExecutor) -> {
assertThat(taskExecutor).hasFieldOrPropertyWithValue("queueCapacity",
Expand All @@ -78,6 +80,10 @@ public void taskExecutorBuilderShouldApplyCustomSettings() {
assertThat(taskExecutor)
.hasFieldOrPropertyWithValue("allowCoreThreadTimeOut", true);
assertThat(taskExecutor.getKeepAliveSeconds()).isEqualTo(5);
assertThat(taskExecutor).hasFieldOrPropertyWithValue(
"waitForTasksToCompleteOnShutdown", true);
assertThat(taskExecutor)
.hasFieldOrPropertyWithValue("awaitTerminationSeconds", 30);
assertThat(taskExecutor.getThreadNamePrefix()).isEqualTo("mytest-");
}));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -59,11 +59,18 @@ public void noSchedulingDoesNotExposeTaskScheduler() {
public void enableSchedulingWithNoTaskExecutorAutoConfiguresOne() {
this.contextRunner
.withPropertyValues(
"spring.task.scheduling.shutdown.await-termination=true",
"spring.task.scheduling.shutdown.await-termination-period=30s",
"spring.task.scheduling.thread-name-prefix=scheduling-test-")
.withUserConfiguration(SchedulingConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(TaskExecutor.class);
TaskExecutor taskExecutor = context.getBean(TaskExecutor.class);
TestBean bean = context.getBean(TestBean.class);
Thread.sleep(15);
assertThat(taskExecutor).hasFieldOrPropertyWithValue(
"waitForTasksToCompleteOnShutdown", true);
assertThat(taskExecutor)
.hasFieldOrPropertyWithValue("awaitTerminationSeconds", 30);
assertThat(bean.threadNames)
.allMatch((name) -> name.contains("scheduling-test-"));
});
Expand Down
Loading

0 comments on commit 35fc929

Please sign in to comment.