pull/14318/head
Phillip Webb 6 years ago
parent 0252e5452b
commit c3de4c84f2

@ -101,7 +101,6 @@ public class DefaultEndpointObjectNameFactoryTests {
public void generateObjectNameWithUniqueNamesDeprecatedPropertyMismatchMainProperty() { public void generateObjectNameWithUniqueNamesDeprecatedPropertyMismatchMainProperty() {
this.environment.setProperty("spring.jmx.unique-names", "false"); this.environment.setProperty("spring.jmx.unique-names", "false");
this.properties.setUniqueNames(true); this.properties.setUniqueNames(true);
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("spring.jmx.unique-names"); this.thrown.expectMessage("spring.jmx.unique-names");
this.thrown.expectMessage("management.endpoints.jmx.unique-names"); this.thrown.expectMessage("management.endpoints.jmx.unique-names");

@ -72,10 +72,8 @@ public class CodecsAutoConfiguration {
@Bean @Bean
public CodecCustomizer loggingCodecCustomizer(InsightsProperties properties) { public CodecCustomizer loggingCodecCustomizer(InsightsProperties properties) {
return (configurer) -> { return (configurer) -> configurer.defaultCodecs().enableLoggingRequestDetails(
configurer.defaultCodecs().enableLoggingRequestDetails( properties.getWeb().isLogRequestDetails());
properties.getWeb().isLogRequestDetails());
};
} }
} }

@ -56,15 +56,12 @@ class KafkaStreamsAnnotationDrivenConfiguration {
Map<String, Object> streamsProperties = this.properties.buildStreamsProperties(); Map<String, Object> streamsProperties = this.properties.buildStreamsProperties();
if (this.properties.getStreams().getApplicationId() == null) { if (this.properties.getStreams().getApplicationId() == null) {
String applicationName = environment.getProperty("spring.application.name"); String applicationName = environment.getProperty("spring.application.name");
if (applicationName != null) { if (applicationName == null) {
streamsProperties.put(StreamsConfig.APPLICATION_ID_CONFIG,
applicationName);
}
else {
throw new InvalidConfigurationPropertyValueException( throw new InvalidConfigurationPropertyValueException(
"spring.kafka.streams.application-id", null, "spring.kafka.streams.application-id", null,
"This property is mandatory and fallback 'spring.application.name' is not set either."); "This property is mandatory and fallback 'spring.application.name' is not set either.");
} }
streamsProperties.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationName);
} }
return new KafkaStreamsConfiguration(streamsProperties); return new KafkaStreamsConfiguration(streamsProperties);
} }

@ -17,7 +17,6 @@
package org.springframework.boot.autoconfigure.task; package org.springframework.boot.autoconfigure.task;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.stream.Collectors;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -66,14 +65,16 @@ public class TaskExecutionAutoConfiguration {
@ConditionalOnMissingBean @ConditionalOnMissingBean
public TaskExecutorBuilder taskExecutorBuilder() { public TaskExecutorBuilder taskExecutorBuilder() {
TaskExecutionProperties.Pool pool = this.properties.getPool(); TaskExecutionProperties.Pool pool = this.properties.getPool();
return new TaskExecutorBuilder().queueCapacity(pool.getQueueCapacity()) TaskExecutorBuilder builder = new TaskExecutorBuilder();
.corePoolSize(pool.getCoreSize()).maxPoolSize(pool.getMaxSize()) builder = builder.queueCapacity(pool.getQueueCapacity());
.allowCoreThreadTimeOut(pool.isAllowCoreThreadTimeout()) builder = builder.corePoolSize(pool.getCoreSize());
.keepAlive(pool.getKeepAlive()) builder = builder.maxPoolSize(pool.getMaxSize());
.threadNamePrefix(this.properties.getThreadNamePrefix()) builder = builder.allowCoreThreadTimeOut(pool.isAllowCoreThreadTimeout());
.customizers(this.taskExecutorCustomizers.stream() builder = builder.keepAlive(pool.getKeepAlive());
.collect(Collectors.toList())) builder = builder.threadNamePrefix(this.properties.getThreadNamePrefix());
.taskDecorator(this.taskDecorator.getIfUnique()); builder = builder.customizers(this.taskExecutorCustomizers);
builder = builder.taskDecorator(this.taskDecorator.getIfUnique());
return builder;
} }
@Bean(name = APPLICATION_TASK_EXECUTOR_BEAN_NAME) @Bean(name = APPLICATION_TASK_EXECUTOR_BEAN_NAME)

@ -16,8 +16,6 @@
package org.springframework.boot.autoconfigure.task; package org.springframework.boot.autoconfigure.task;
import java.util.stream.Collectors;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@ -55,9 +53,11 @@ public class TaskSchedulingAutoConfiguration {
@ConditionalOnMissingBean @ConditionalOnMissingBean
public TaskSchedulerBuilder taskSchedulerBuilder(TaskSchedulingProperties properties, public TaskSchedulerBuilder taskSchedulerBuilder(TaskSchedulingProperties properties,
ObjectProvider<TaskSchedulerCustomizer> taskSchedulerCustomizers) { ObjectProvider<TaskSchedulerCustomizer> taskSchedulerCustomizers) {
return new TaskSchedulerBuilder().poolSize(properties.getPool().getSize()) TaskSchedulerBuilder builder = new TaskSchedulerBuilder();
.threadNamePrefix(properties.getThreadNamePrefix()).customizers( builder = builder.poolSize(properties.getPool().getSize());
taskSchedulerCustomizers.stream().collect(Collectors.toList())); builder = builder.threadNamePrefix(properties.getThreadNamePrefix());
builder = builder.customizers(taskSchedulerCustomizers);
return builder;
} }
} }

@ -5879,9 +5879,9 @@ The following code shows a typical example:
---- ----
[[boot-features-webclient-runtime]] [[boot-features-webclient-runtime]]
=== WebClient Runtime === WebClient Runtime
Spring Boot will auto-detect which `ClientHttpConnector` to drive `WebClient`, depending Spring Boot will auto-detect which `ClientHttpConnector` to drive `WebClient`, depending
on the libraries available on the application classpath. on the libraries available on the application classpath.
@ -5901,6 +5901,7 @@ You can learn more about the
options in the Spring Framework reference documentation]. options in the Spring Framework reference documentation].
[[boot-features-webclient-customization]] [[boot-features-webclient-customization]]
=== WebClient Customization === WebClient Customization
There are three main approaches to `WebClient` customization, depending on how broadly you There are three main approaches to `WebClient` customization, depending on how broadly you

@ -18,7 +18,6 @@ package org.springframework.boot.task;
import java.time.Duration; import java.time.Duration;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
@ -59,11 +58,9 @@ public class TaskExecutorBuilder {
private final TaskDecorator taskDecorator; private final TaskDecorator taskDecorator;
private final Set<TaskExecutorCustomizer> taskExecutorCustomizers; private final Set<TaskExecutorCustomizer> customizers;
public TaskExecutorBuilder(TaskExecutorCustomizer... taskExecutorCustomizers) { public TaskExecutorBuilder() {
Assert.notNull(taskExecutorCustomizers,
"TaskExecutorCustomizers must not be null");
this.queueCapacity = null; this.queueCapacity = null;
this.corePoolSize = null; this.corePoolSize = null;
this.maxPoolSize = null; this.maxPoolSize = null;
@ -71,14 +68,13 @@ public class TaskExecutorBuilder {
this.keepAlive = null; this.keepAlive = null;
this.threadNamePrefix = null; this.threadNamePrefix = null;
this.taskDecorator = null; this.taskDecorator = null;
this.taskExecutorCustomizers = Collections.unmodifiableSet( this.customizers = null;
new LinkedHashSet<>(Arrays.asList(taskExecutorCustomizers)));
} }
public TaskExecutorBuilder(Integer queueCapacity, Integer corePoolSize, private TaskExecutorBuilder(Integer queueCapacity, Integer corePoolSize,
Integer maxPoolSize, Boolean allowCoreThreadTimeOut, Duration keepAlive, Integer maxPoolSize, Boolean allowCoreThreadTimeOut, Duration keepAlive,
String threadNamePrefix, TaskDecorator taskDecorator, String threadNamePrefix, TaskDecorator taskDecorator,
Set<TaskExecutorCustomizer> taskExecutorCustomizers) { Set<TaskExecutorCustomizer> customizers) {
this.queueCapacity = queueCapacity; this.queueCapacity = queueCapacity;
this.corePoolSize = corePoolSize; this.corePoolSize = corePoolSize;
this.maxPoolSize = maxPoolSize; this.maxPoolSize = maxPoolSize;
@ -86,7 +82,7 @@ public class TaskExecutorBuilder {
this.keepAlive = keepAlive; this.keepAlive = keepAlive;
this.threadNamePrefix = threadNamePrefix; this.threadNamePrefix = threadNamePrefix;
this.taskDecorator = taskDecorator; this.taskDecorator = taskDecorator;
this.taskExecutorCustomizers = taskExecutorCustomizers; this.customizers = customizers;
} }
/** /**
@ -98,7 +94,7 @@ public class TaskExecutorBuilder {
public TaskExecutorBuilder queueCapacity(int queueCapacity) { public TaskExecutorBuilder queueCapacity(int queueCapacity) {
return new TaskExecutorBuilder(queueCapacity, this.corePoolSize, this.maxPoolSize, return new TaskExecutorBuilder(queueCapacity, this.corePoolSize, this.maxPoolSize,
this.allowCoreThreadTimeOut, this.keepAlive, this.threadNamePrefix, this.allowCoreThreadTimeOut, this.keepAlive, this.threadNamePrefix,
this.taskDecorator, this.taskExecutorCustomizers); this.taskDecorator, this.customizers);
} }
/** /**
@ -113,7 +109,7 @@ public class TaskExecutorBuilder {
public TaskExecutorBuilder corePoolSize(int corePoolSize) { public TaskExecutorBuilder corePoolSize(int corePoolSize) {
return new TaskExecutorBuilder(this.queueCapacity, corePoolSize, this.maxPoolSize, return new TaskExecutorBuilder(this.queueCapacity, corePoolSize, this.maxPoolSize,
this.allowCoreThreadTimeOut, this.keepAlive, this.threadNamePrefix, this.allowCoreThreadTimeOut, this.keepAlive, this.threadNamePrefix,
this.taskDecorator, this.taskExecutorCustomizers); this.taskDecorator, this.customizers);
} }
/** /**
@ -128,7 +124,7 @@ public class TaskExecutorBuilder {
public TaskExecutorBuilder maxPoolSize(int maxPoolSize) { public TaskExecutorBuilder maxPoolSize(int maxPoolSize) {
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, maxPoolSize, return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, maxPoolSize,
this.allowCoreThreadTimeOut, this.keepAlive, this.threadNamePrefix, this.allowCoreThreadTimeOut, this.keepAlive, this.threadNamePrefix,
this.taskDecorator, this.taskExecutorCustomizers); this.taskDecorator, this.customizers);
} }
/** /**
@ -140,7 +136,7 @@ public class TaskExecutorBuilder {
public TaskExecutorBuilder allowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) { public TaskExecutorBuilder allowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) {
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
this.maxPoolSize, allowCoreThreadTimeOut, this.keepAlive, this.maxPoolSize, allowCoreThreadTimeOut, this.keepAlive,
this.threadNamePrefix, this.taskDecorator, this.taskExecutorCustomizers); this.threadNamePrefix, this.taskDecorator, this.customizers);
} }
/** /**
@ -151,7 +147,7 @@ public class TaskExecutorBuilder {
public TaskExecutorBuilder keepAlive(Duration keepAlive) { public TaskExecutorBuilder keepAlive(Duration keepAlive) {
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
this.maxPoolSize, this.allowCoreThreadTimeOut, keepAlive, this.maxPoolSize, this.allowCoreThreadTimeOut, keepAlive,
this.threadNamePrefix, this.taskDecorator, this.taskExecutorCustomizers); this.threadNamePrefix, this.taskDecorator, this.customizers);
} }
/** /**
@ -162,7 +158,7 @@ public class TaskExecutorBuilder {
public TaskExecutorBuilder threadNamePrefix(String threadNamePrefix) { public TaskExecutorBuilder threadNamePrefix(String threadNamePrefix) {
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive, this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive,
threadNamePrefix, this.taskDecorator, this.taskExecutorCustomizers); threadNamePrefix, this.taskDecorator, this.customizers);
} }
/** /**
@ -173,7 +169,7 @@ public class TaskExecutorBuilder {
public TaskExecutorBuilder taskDecorator(TaskDecorator taskDecorator) { public TaskExecutorBuilder taskDecorator(TaskDecorator taskDecorator) {
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive, this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive,
this.threadNamePrefix, taskDecorator, this.taskExecutorCustomizers); this.threadNamePrefix, taskDecorator, this.customizers);
} }
/** /**
@ -181,15 +177,13 @@ public class TaskExecutorBuilder {
* applied to the {@link ThreadPoolTaskExecutor}. Customizers are applied in the order * applied to the {@link ThreadPoolTaskExecutor}. Customizers are applied in the order
* that they were added after builder configuration has been applied. Setting this * that they were added after builder configuration has been applied. Setting this
* value will replace any previously configured customizers. * value will replace any previously configured customizers.
* @param taskExecutorCustomizers the customizers to set * @param customizers the customizers to set
* @return a new builder instance * @return a new builder instance
* @see #additionalCustomizers(TaskExecutorCustomizer...) * @see #additionalCustomizers(TaskExecutorCustomizer...)
*/ */
public TaskExecutorBuilder customizers( public TaskExecutorBuilder customizers(TaskExecutorCustomizer... customizers) {
TaskExecutorCustomizer... taskExecutorCustomizers) { Assert.notNull(customizers, "Customizers must not be null");
Assert.notNull(taskExecutorCustomizers, return customizers(Arrays.asList(customizers));
"TaskExecutorCustomizers must not be null");
return customizers(Arrays.asList(taskExecutorCustomizers));
} }
/** /**
@ -197,52 +191,46 @@ public class TaskExecutorBuilder {
* applied to the {@link ThreadPoolTaskExecutor}. Customizers are applied in the order * applied to the {@link ThreadPoolTaskExecutor}. Customizers are applied in the order
* that they were added after builder configuration has been applied. Setting this * that they were added after builder configuration has been applied. Setting this
* value will replace any previously configured customizers. * value will replace any previously configured customizers.
* @param taskExecutorCustomizers the customizers to set * @param customizers the customizers to set
* @return a new builder instance * @return a new builder instance
* @see #additionalCustomizers(TaskExecutorCustomizer...) * @see #additionalCustomizers(TaskExecutorCustomizer...)
*/ */
public TaskExecutorBuilder customizers( public TaskExecutorBuilder customizers(Iterable<TaskExecutorCustomizer> customizers) {
Collection<? extends TaskExecutorCustomizer> taskExecutorCustomizers) { Assert.notNull(customizers, "Customizers must not be null");
Assert.notNull(taskExecutorCustomizers,
"TaskExecutorCustomizers must not be null");
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive, this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive,
this.threadNamePrefix, this.taskDecorator, this.threadNamePrefix, this.taskDecorator, append(null, customizers));
Collections.unmodifiableSet(new LinkedHashSet<TaskExecutorCustomizer>(
taskExecutorCustomizers)));
} }
/** /**
* Add {@link TaskExecutorCustomizer TaskExecutorCustomizers} that should be applied * Add {@link TaskExecutorCustomizer TaskExecutorCustomizers} that should be applied
* to the {@link ThreadPoolTaskExecutor}. Customizers are applied in the order that * to the {@link ThreadPoolTaskExecutor}. Customizers are applied in the order that
* they were added after builder configuration has been applied. * they were added after builder configuration has been applied.
* @param taskExecutorCustomizers the customizers to add * @param customizers the customizers to add
* @return a new builder instance * @return a new builder instance
* @see #customizers(TaskExecutorCustomizer...) * @see #customizers(TaskExecutorCustomizer...)
*/ */
public TaskExecutorBuilder additionalCustomizers( public TaskExecutorBuilder additionalCustomizers(
TaskExecutorCustomizer... taskExecutorCustomizers) { TaskExecutorCustomizer... customizers) {
Assert.notNull(taskExecutorCustomizers, Assert.notNull(customizers, "Customizers must not be null");
"TaskExecutorCustomizers must not be null"); return additionalCustomizers(Arrays.asList(customizers));
return additionalCustomizers(Arrays.asList(taskExecutorCustomizers));
} }
/** /**
* Add {@link TaskExecutorCustomizer TaskExecutorCustomizers} that should be applied * Add {@link TaskExecutorCustomizer TaskExecutorCustomizers} that should be applied
* to the {@link ThreadPoolTaskExecutor}. Customizers are applied in the order that * to the {@link ThreadPoolTaskExecutor}. Customizers are applied in the order that
* they were added after builder configuration has been applied. * they were added after builder configuration has been applied.
* @param taskExecutorCustomizers the customizers to add * @param customizers the customizers to add
* @return a new builder instance * @return a new builder instance
* @see #customizers(TaskExecutorCustomizer...) * @see #customizers(TaskExecutorCustomizer...)
*/ */
public TaskExecutorBuilder additionalCustomizers( public TaskExecutorBuilder additionalCustomizers(
Collection<? extends TaskExecutorCustomizer> taskExecutorCustomizers) { Iterable<TaskExecutorCustomizer> customizers) {
Assert.notNull(taskExecutorCustomizers, Assert.notNull(customizers, "Customizers must not be null");
"TaskExecutorCustomizers must not be null");
return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize,
this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive, this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive,
this.threadNamePrefix, this.taskDecorator, this.threadNamePrefix, this.taskDecorator,
append(this.taskExecutorCustomizers, taskExecutorCustomizers)); append(this.customizers, customizers));
} }
/** /**
@ -288,18 +276,15 @@ public class TaskExecutorBuilder {
map.from(this.threadNamePrefix).whenHasText() map.from(this.threadNamePrefix).whenHasText()
.to(taskExecutor::setThreadNamePrefix); .to(taskExecutor::setThreadNamePrefix);
map.from(this.taskDecorator).to(taskExecutor::setTaskDecorator); map.from(this.taskDecorator).to(taskExecutor::setTaskDecorator);
if (!CollectionUtils.isEmpty(this.customizers)) {
if (!CollectionUtils.isEmpty(this.taskExecutorCustomizers)) { this.customizers.forEach((customizer) -> customizer.customize(taskExecutor));
for (TaskExecutorCustomizer customizer : this.taskExecutorCustomizers) {
customizer.customize(taskExecutor);
}
} }
return taskExecutor; return taskExecutor;
} }
private static <T> Set<T> append(Set<T> set, Collection<? extends T> additions) { private <T> Set<T> append(Set<T> set, Iterable<? extends T> additions) {
Set<T> result = new LinkedHashSet<>((set != null) ? set : Collections.emptySet()); Set<T> result = new LinkedHashSet<>((set != null) ? set : Collections.emptySet());
result.addAll(additions); additions.forEach(result::add);
return Collections.unmodifiableSet(result); return Collections.unmodifiableSet(result);
} }

@ -17,7 +17,6 @@
package org.springframework.boot.task; package org.springframework.boot.task;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
@ -45,22 +44,19 @@ public class TaskSchedulerBuilder {
private final String threadNamePrefix; private final String threadNamePrefix;
private final Set<TaskSchedulerCustomizer> taskSchedulerCustomizers; private final Set<TaskSchedulerCustomizer> customizers;
public TaskSchedulerBuilder(TaskSchedulerCustomizer... taskSchedulerCustomizers) { public TaskSchedulerBuilder() {
Assert.notNull(taskSchedulerCustomizers,
"TaskSchedulerCustomizers must not be null");
this.poolSize = null; this.poolSize = null;
this.threadNamePrefix = null; this.threadNamePrefix = null;
this.taskSchedulerCustomizers = Collections.unmodifiableSet( this.customizers = null;
new LinkedHashSet<>(Arrays.asList(taskSchedulerCustomizers)));
} }
public TaskSchedulerBuilder(Integer poolSize, String threadNamePrefix, public TaskSchedulerBuilder(Integer poolSize, String threadNamePrefix,
Set<TaskSchedulerCustomizer> taskSchedulerCustomizers) { Set<TaskSchedulerCustomizer> taskSchedulerCustomizers) {
this.poolSize = poolSize; this.poolSize = poolSize;
this.threadNamePrefix = threadNamePrefix; this.threadNamePrefix = threadNamePrefix;
this.taskSchedulerCustomizers = taskSchedulerCustomizers; this.customizers = taskSchedulerCustomizers;
} }
/** /**
@ -70,7 +66,7 @@ public class TaskSchedulerBuilder {
*/ */
public TaskSchedulerBuilder poolSize(int poolSize) { public TaskSchedulerBuilder poolSize(int poolSize) {
return new TaskSchedulerBuilder(poolSize, this.threadNamePrefix, return new TaskSchedulerBuilder(poolSize, this.threadNamePrefix,
this.taskSchedulerCustomizers); this.customizers);
} }
/** /**
@ -80,7 +76,7 @@ public class TaskSchedulerBuilder {
*/ */
public TaskSchedulerBuilder threadNamePrefix(String threadNamePrefix) { public TaskSchedulerBuilder threadNamePrefix(String threadNamePrefix) {
return new TaskSchedulerBuilder(this.poolSize, threadNamePrefix, return new TaskSchedulerBuilder(this.poolSize, threadNamePrefix,
this.taskSchedulerCustomizers); this.customizers);
} }
/** /**
@ -88,15 +84,13 @@ public class TaskSchedulerBuilder {
* applied to the {@link ThreadPoolTaskScheduler}. Customizers are applied in the * applied to the {@link ThreadPoolTaskScheduler}. Customizers are applied in the
* order that they were added after builder configuration has been applied. Setting * order that they were added after builder configuration has been applied. Setting
* this value will replace any previously configured customizers. * this value will replace any previously configured customizers.
* @param taskSchedulerCustomizers the customizers to set * @param customizers the customizers to set
* @return a new builder instance * @return a new builder instance
* @see #additionalCustomizers(TaskSchedulerCustomizer...) * @see #additionalCustomizers(TaskSchedulerCustomizer...)
*/ */
public TaskSchedulerBuilder customizers( public TaskSchedulerBuilder customizers(TaskSchedulerCustomizer... customizers) {
TaskSchedulerCustomizer... taskSchedulerCustomizers) { Assert.notNull(customizers, "Customizers must not be null");
Assert.notNull(taskSchedulerCustomizers, return customizers(Arrays.asList(customizers));
"TaskSchedulerCustomizers must not be null");
return customizers(Arrays.asList(taskSchedulerCustomizers));
} }
/** /**
@ -104,48 +98,44 @@ public class TaskSchedulerBuilder {
* applied to the {@link ThreadPoolTaskScheduler}. Customizers are applied in the * applied to the {@link ThreadPoolTaskScheduler}. Customizers are applied in the
* order that they were added after builder configuration has been applied. Setting * order that they were added after builder configuration has been applied. Setting
* this value will replace any previously configured customizers. * this value will replace any previously configured customizers.
* @param taskSchedulerCustomizers the customizers to set * @param customizers the customizers to set
* @return a new builder instance * @return a new builder instance
* @see #additionalCustomizers(TaskSchedulerCustomizer...) * @see #additionalCustomizers(TaskSchedulerCustomizer...)
*/ */
public TaskSchedulerBuilder customizers( public TaskSchedulerBuilder customizers(
Collection<? extends TaskSchedulerCustomizer> taskSchedulerCustomizers) { Iterable<TaskSchedulerCustomizer> customizers) {
Assert.notNull(taskSchedulerCustomizers, Assert.notNull(customizers, "Customizers must not be null");
"TaskSchedulerCustomizers must not be null");
return new TaskSchedulerBuilder(this.poolSize, this.threadNamePrefix, return new TaskSchedulerBuilder(this.poolSize, this.threadNamePrefix,
Collections.unmodifiableSet(new LinkedHashSet<TaskSchedulerCustomizer>( append(null, customizers));
taskSchedulerCustomizers)));
} }
/** /**
* Add {@link TaskSchedulerCustomizer taskSchedulerCustomizers} that should be applied * Add {@link TaskSchedulerCustomizer taskSchedulerCustomizers} that should be applied
* to the {@link ThreadPoolTaskScheduler}. Customizers are applied in the order that * to the {@link ThreadPoolTaskScheduler}. Customizers are applied in the order that
* they were added after builder configuration has been applied. * they were added after builder configuration has been applied.
* @param taskSchedulerCustomizers the customizers to add * @param customizers the customizers to add
* @return a new builder instance * @return a new builder instance
* @see #customizers(TaskSchedulerCustomizer...) * @see #customizers(TaskSchedulerCustomizer...)
*/ */
public TaskSchedulerBuilder additionalCustomizers( public TaskSchedulerBuilder additionalCustomizers(
TaskSchedulerCustomizer... taskSchedulerCustomizers) { TaskSchedulerCustomizer... customizers) {
Assert.notNull(taskSchedulerCustomizers, Assert.notNull(customizers, "Customizers must not be null");
"TaskSchedulerCustomizers must not be null"); return additionalCustomizers(Arrays.asList(customizers));
return additionalCustomizers(Arrays.asList(taskSchedulerCustomizers));
} }
/** /**
* Add {@link TaskSchedulerCustomizer taskSchedulerCustomizers} that should be applied * Add {@link TaskSchedulerCustomizer taskSchedulerCustomizers} that should be applied
* to the {@link ThreadPoolTaskScheduler}. Customizers are applied in the order that * to the {@link ThreadPoolTaskScheduler}. Customizers are applied in the order that
* they were added after builder configuration has been applied. * they were added after builder configuration has been applied.
* @param taskSchedulerCustomizers the customizers to add * @param customizers the customizers to add
* @return a new builder instance * @return a new builder instance
* @see #customizers(TaskSchedulerCustomizer...) * @see #customizers(TaskSchedulerCustomizer...)
*/ */
public TaskSchedulerBuilder additionalCustomizers( public TaskSchedulerBuilder additionalCustomizers(
Collection<? extends TaskSchedulerCustomizer> taskSchedulerCustomizers) { Iterable<TaskSchedulerCustomizer> customizers) {
Assert.notNull(taskSchedulerCustomizers, Assert.notNull(customizers, "Customizers must not be null");
"TaskSchedulerCustomizers must not be null");
return new TaskSchedulerBuilder(this.poolSize, this.threadNamePrefix, return new TaskSchedulerBuilder(this.poolSize, this.threadNamePrefix,
append(this.taskSchedulerCustomizers, taskSchedulerCustomizers)); append(this.customizers, customizers));
} }
/** /**
@ -169,18 +159,15 @@ public class TaskSchedulerBuilder {
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(this.poolSize).to(taskScheduler::setPoolSize); map.from(this.poolSize).to(taskScheduler::setPoolSize);
map.from(this.threadNamePrefix).to(taskScheduler::setThreadNamePrefix); map.from(this.threadNamePrefix).to(taskScheduler::setThreadNamePrefix);
if (!CollectionUtils.isEmpty(this.customizers)) {
if (!CollectionUtils.isEmpty(this.taskSchedulerCustomizers)) { this.customizers.forEach((customizer) -> customizer.customize(taskScheduler));
for (TaskSchedulerCustomizer customizer : this.taskSchedulerCustomizers) {
customizer.customize(taskScheduler);
}
} }
return taskScheduler; return taskScheduler;
} }
private static <T> Set<T> append(Set<T> set, Collection<? extends T> additions) { private <T> Set<T> append(Set<T> set, Iterable<? extends T> additions) {
Set<T> result = new LinkedHashSet<>((set != null) ? set : Collections.emptySet()); Set<T> result = new LinkedHashSet<>((set != null) ? set : Collections.emptySet());
result.addAll(additions); additions.forEach(result::add);
return Collections.unmodifiableSet(result); return Collections.unmodifiableSet(result);
} }

@ -47,13 +47,6 @@ public class TaskExecutorBuilderTests {
private TaskExecutorBuilder builder = new TaskExecutorBuilder(); private TaskExecutorBuilder builder = new TaskExecutorBuilder();
@Test
public void createWhenCustomizersAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("TaskExecutorCustomizers must not be null");
new TaskExecutorBuilder((TaskExecutorCustomizer[]) null);
}
@Test @Test
public void poolSettingsShouldApply() { public void poolSettingsShouldApply() {
ThreadPoolTaskExecutor executor = this.builder.queueCapacity(10).corePoolSize(4) ThreadPoolTaskExecutor executor = this.builder.queueCapacity(10).corePoolSize(4)
@ -85,14 +78,14 @@ public class TaskExecutorBuilderTests {
@Test @Test
public void customizersWhenCustomizersAreNullShouldThrowException() { public void customizersWhenCustomizersAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("TaskExecutorCustomizers must not be null"); this.thrown.expectMessage("Customizers must not be null");
this.builder.customizers((TaskExecutorCustomizer[]) null); this.builder.customizers((TaskExecutorCustomizer[]) null);
} }
@Test @Test
public void customizersCollectionWhenCustomizersAreNullShouldThrowException() { public void customizersCollectionWhenCustomizersAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("TaskExecutorCustomizers must not be null"); this.thrown.expectMessage("Customizers must not be null");
this.builder.customizers((Set<TaskExecutorCustomizer>) null); this.builder.customizers((Set<TaskExecutorCustomizer>) null);
} }
@ -135,14 +128,14 @@ public class TaskExecutorBuilderTests {
@Test @Test
public void additionalCustomizersWhenCustomizersAreNullShouldThrowException() { public void additionalCustomizersWhenCustomizersAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("TaskExecutorCustomizers must not be null"); this.thrown.expectMessage("Customizers must not be null");
this.builder.additionalCustomizers((TaskExecutorCustomizer[]) null); this.builder.additionalCustomizers((TaskExecutorCustomizer[]) null);
} }
@Test @Test
public void additionalCustomizersCollectionWhenCustomizersAreNullShouldThrowException() { public void additionalCustomizersCollectionWhenCustomizersAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("TaskExecutorCustomizers must not be null"); this.thrown.expectMessage("Customizers must not be null");
this.builder.additionalCustomizers((Set<TaskExecutorCustomizer>) null); this.builder.additionalCustomizers((Set<TaskExecutorCustomizer>) null);
} }

@ -43,13 +43,6 @@ public class TaskSchedulerBuilderTests {
private TaskSchedulerBuilder builder = new TaskSchedulerBuilder(); private TaskSchedulerBuilder builder = new TaskSchedulerBuilder();
@Test
public void createWhenCustomizersAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("TaskSchedulerCustomizers must not be null");
new TaskSchedulerBuilder((TaskSchedulerCustomizer[]) null);
}
@Test @Test
public void poolSettingsShouldApply() { public void poolSettingsShouldApply() {
ThreadPoolTaskScheduler scheduler = this.builder.poolSize(4).build(); ThreadPoolTaskScheduler scheduler = this.builder.poolSize(4).build();
@ -66,14 +59,14 @@ public class TaskSchedulerBuilderTests {
@Test @Test
public void customizersWhenCustomizersAreNullShouldThrowException() { public void customizersWhenCustomizersAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("TaskSchedulerCustomizers must not be null"); this.thrown.expectMessage("Customizers must not be null");
this.builder.customizers((TaskSchedulerCustomizer[]) null); this.builder.customizers((TaskSchedulerCustomizer[]) null);
} }
@Test @Test
public void customizersCollectionWhenCustomizersAreNullShouldThrowException() { public void customizersCollectionWhenCustomizersAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("TaskSchedulerCustomizers must not be null"); this.thrown.expectMessage("Customizers must not be null");
this.builder.customizers((Set<TaskSchedulerCustomizer>) null); this.builder.customizers((Set<TaskSchedulerCustomizer>) null);
} }
@ -108,14 +101,14 @@ public class TaskSchedulerBuilderTests {
@Test @Test
public void additionalCustomizersWhenCustomizersAreNullShouldThrowException() { public void additionalCustomizersWhenCustomizersAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("TaskSchedulerCustomizers must not be null"); this.thrown.expectMessage("Customizers must not be null");
this.builder.additionalCustomizers((TaskSchedulerCustomizer[]) null); this.builder.additionalCustomizers((TaskSchedulerCustomizer[]) null);
} }
@Test @Test
public void additionalCustomizersCollectionWhenCustomizersAreNullShouldThrowException() { public void additionalCustomizersCollectionWhenCustomizersAreNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("TaskSchedulerCustomizers must not be null"); this.thrown.expectMessage("Customizers must not be null");
this.builder.additionalCustomizers((Set<TaskSchedulerCustomizer>) null); this.builder.additionalCustomizers((Set<TaskSchedulerCustomizer>) null);
} }

Loading…
Cancel
Save