From c3de4c84f24b4eb8c16eb6434dfeebc6b007b178 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 5 Sep 2018 12:54:12 -0700 Subject: [PATCH] Polish --- ...DefaultEndpointObjectNameFactoryTests.java | 1 - .../http/codec/CodecsAutoConfiguration.java | 6 +- ...aStreamsAnnotationDrivenConfiguration.java | 7 +- .../task/TaskExecutionAutoConfiguration.java | 19 ++--- .../task/TaskSchedulingAutoConfiguration.java | 10 +-- .../main/asciidoc/spring-boot-features.adoc | 3 +- .../boot/task/TaskExecutorBuilder.java | 81 ++++++++----------- .../boot/task/TaskSchedulerBuilder.java | 65 ++++++--------- .../boot/task/TaskExecutorBuilderTests.java | 15 +--- .../boot/task/TaskSchedulerBuilderTests.java | 15 +--- 10 files changed, 88 insertions(+), 134 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/jmx/DefaultEndpointObjectNameFactoryTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/jmx/DefaultEndpointObjectNameFactoryTests.java index 19522be1066..f7413707d24 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/jmx/DefaultEndpointObjectNameFactoryTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/jmx/DefaultEndpointObjectNameFactoryTests.java @@ -101,7 +101,6 @@ public class DefaultEndpointObjectNameFactoryTests { public void generateObjectNameWithUniqueNamesDeprecatedPropertyMismatchMainProperty() { this.environment.setProperty("spring.jmx.unique-names", "false"); this.properties.setUniqueNames(true); - this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("spring.jmx.unique-names"); this.thrown.expectMessage("management.endpoints.jmx.unique-names"); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/codec/CodecsAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/codec/CodecsAutoConfiguration.java index 3a1cb6f4692..7e6ce073e28 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/codec/CodecsAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/codec/CodecsAutoConfiguration.java @@ -72,10 +72,8 @@ public class CodecsAutoConfiguration { @Bean public CodecCustomizer loggingCodecCustomizer(InsightsProperties properties) { - return (configurer) -> { - configurer.defaultCodecs().enableLoggingRequestDetails( - properties.getWeb().isLogRequestDetails()); - }; + return (configurer) -> configurer.defaultCodecs().enableLoggingRequestDetails( + properties.getWeb().isLogRequestDetails()); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/KafkaStreamsAnnotationDrivenConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/KafkaStreamsAnnotationDrivenConfiguration.java index 7e48a87ceed..e6eeca66a85 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/KafkaStreamsAnnotationDrivenConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/KafkaStreamsAnnotationDrivenConfiguration.java @@ -56,15 +56,12 @@ class KafkaStreamsAnnotationDrivenConfiguration { Map streamsProperties = this.properties.buildStreamsProperties(); if (this.properties.getStreams().getApplicationId() == null) { String applicationName = environment.getProperty("spring.application.name"); - if (applicationName != null) { - streamsProperties.put(StreamsConfig.APPLICATION_ID_CONFIG, - applicationName); - } - else { + if (applicationName == null) { throw new InvalidConfigurationPropertyValueException( "spring.kafka.streams.application-id", null, "This property is mandatory and fallback 'spring.application.name' is not set either."); } + streamsProperties.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationName); } return new KafkaStreamsConfiguration(streamsProperties); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfiguration.java index 62709f5d019..94c4dcbfafd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfiguration.java @@ -17,7 +17,6 @@ package org.springframework.boot.autoconfigure.task; import java.util.concurrent.Executor; -import java.util.stream.Collectors; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -66,14 +65,16 @@ public class TaskExecutionAutoConfiguration { @ConditionalOnMissingBean public TaskExecutorBuilder taskExecutorBuilder() { TaskExecutionProperties.Pool pool = this.properties.getPool(); - return new TaskExecutorBuilder().queueCapacity(pool.getQueueCapacity()) - .corePoolSize(pool.getCoreSize()).maxPoolSize(pool.getMaxSize()) - .allowCoreThreadTimeOut(pool.isAllowCoreThreadTimeout()) - .keepAlive(pool.getKeepAlive()) - .threadNamePrefix(this.properties.getThreadNamePrefix()) - .customizers(this.taskExecutorCustomizers.stream() - .collect(Collectors.toList())) - .taskDecorator(this.taskDecorator.getIfUnique()); + TaskExecutorBuilder builder = new TaskExecutorBuilder(); + builder = builder.queueCapacity(pool.getQueueCapacity()); + builder = builder.corePoolSize(pool.getCoreSize()); + builder = builder.maxPoolSize(pool.getMaxSize()); + builder = builder.allowCoreThreadTimeOut(pool.isAllowCoreThreadTimeout()); + builder = builder.keepAlive(pool.getKeepAlive()); + builder = builder.threadNamePrefix(this.properties.getThreadNamePrefix()); + builder = builder.customizers(this.taskExecutorCustomizers); + builder = builder.taskDecorator(this.taskDecorator.getIfUnique()); + return builder; } @Bean(name = APPLICATION_TASK_EXECUTOR_BEAN_NAME) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfiguration.java index d31617ac939..6c02a634277 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskSchedulingAutoConfiguration.java @@ -16,8 +16,6 @@ package org.springframework.boot.autoconfigure.task; -import java.util.stream.Collectors; - import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; @@ -55,9 +53,11 @@ public class TaskSchedulingAutoConfiguration { @ConditionalOnMissingBean public TaskSchedulerBuilder taskSchedulerBuilder(TaskSchedulingProperties properties, ObjectProvider taskSchedulerCustomizers) { - return new TaskSchedulerBuilder().poolSize(properties.getPool().getSize()) - .threadNamePrefix(properties.getThreadNamePrefix()).customizers( - taskSchedulerCustomizers.stream().collect(Collectors.toList())); + TaskSchedulerBuilder builder = new TaskSchedulerBuilder(); + builder = builder.poolSize(properties.getPool().getSize()); + builder = builder.threadNamePrefix(properties.getThreadNamePrefix()); + builder = builder.customizers(taskSchedulerCustomizers); + return builder; } } diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 1b7af6b5106..2c3904f8627 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -5879,9 +5879,9 @@ The following code shows a typical example: ---- + [[boot-features-webclient-runtime]] === WebClient Runtime - Spring Boot will auto-detect which `ClientHttpConnector` to drive `WebClient`, depending 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]. + [[boot-features-webclient-customization]] === WebClient Customization There are three main approaches to `WebClient` customization, depending on how broadly you diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskExecutorBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskExecutorBuilder.java index 2e959f28646..eced7b6f95c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskExecutorBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskExecutorBuilder.java @@ -18,7 +18,6 @@ package org.springframework.boot.task; import java.time.Duration; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; @@ -59,11 +58,9 @@ public class TaskExecutorBuilder { private final TaskDecorator taskDecorator; - private final Set taskExecutorCustomizers; + private final Set customizers; - public TaskExecutorBuilder(TaskExecutorCustomizer... taskExecutorCustomizers) { - Assert.notNull(taskExecutorCustomizers, - "TaskExecutorCustomizers must not be null"); + public TaskExecutorBuilder() { this.queueCapacity = null; this.corePoolSize = null; this.maxPoolSize = null; @@ -71,14 +68,13 @@ public class TaskExecutorBuilder { this.keepAlive = null; this.threadNamePrefix = null; this.taskDecorator = null; - this.taskExecutorCustomizers = Collections.unmodifiableSet( - new LinkedHashSet<>(Arrays.asList(taskExecutorCustomizers))); + this.customizers = null; } - public TaskExecutorBuilder(Integer queueCapacity, Integer corePoolSize, + private TaskExecutorBuilder(Integer queueCapacity, Integer corePoolSize, Integer maxPoolSize, Boolean allowCoreThreadTimeOut, Duration keepAlive, String threadNamePrefix, TaskDecorator taskDecorator, - Set taskExecutorCustomizers) { + Set customizers) { this.queueCapacity = queueCapacity; this.corePoolSize = corePoolSize; this.maxPoolSize = maxPoolSize; @@ -86,7 +82,7 @@ public class TaskExecutorBuilder { this.keepAlive = keepAlive; this.threadNamePrefix = threadNamePrefix; this.taskDecorator = taskDecorator; - this.taskExecutorCustomizers = taskExecutorCustomizers; + this.customizers = customizers; } /** @@ -98,7 +94,7 @@ public class TaskExecutorBuilder { public TaskExecutorBuilder queueCapacity(int queueCapacity) { return new TaskExecutorBuilder(queueCapacity, this.corePoolSize, this.maxPoolSize, 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) { return new TaskExecutorBuilder(this.queueCapacity, corePoolSize, this.maxPoolSize, 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) { return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, maxPoolSize, 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) { return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, 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) { return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, 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) { return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, 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) { return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, 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 * that they were added after builder configuration has been applied. Setting this * value will replace any previously configured customizers. - * @param taskExecutorCustomizers the customizers to set + * @param customizers the customizers to set * @return a new builder instance * @see #additionalCustomizers(TaskExecutorCustomizer...) */ - public TaskExecutorBuilder customizers( - TaskExecutorCustomizer... taskExecutorCustomizers) { - Assert.notNull(taskExecutorCustomizers, - "TaskExecutorCustomizers must not be null"); - return customizers(Arrays.asList(taskExecutorCustomizers)); + public TaskExecutorBuilder customizers(TaskExecutorCustomizer... customizers) { + Assert.notNull(customizers, "Customizers must not be null"); + return customizers(Arrays.asList(customizers)); } /** @@ -197,52 +191,46 @@ public class TaskExecutorBuilder { * applied to the {@link ThreadPoolTaskExecutor}. Customizers are applied in the order * that they were added after builder configuration has been applied. Setting this * value will replace any previously configured customizers. - * @param taskExecutorCustomizers the customizers to set + * @param customizers the customizers to set * @return a new builder instance * @see #additionalCustomizers(TaskExecutorCustomizer...) */ - public TaskExecutorBuilder customizers( - Collection taskExecutorCustomizers) { - Assert.notNull(taskExecutorCustomizers, - "TaskExecutorCustomizers must not be null"); + public TaskExecutorBuilder customizers(Iterable customizers) { + Assert.notNull(customizers, "Customizers must not be null"); return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive, - this.threadNamePrefix, this.taskDecorator, - Collections.unmodifiableSet(new LinkedHashSet( - taskExecutorCustomizers))); + this.threadNamePrefix, this.taskDecorator, append(null, customizers)); } /** * Add {@link TaskExecutorCustomizer TaskExecutorCustomizers} that should be applied * to the {@link ThreadPoolTaskExecutor}. Customizers are applied in the order that * 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 * @see #customizers(TaskExecutorCustomizer...) */ public TaskExecutorBuilder additionalCustomizers( - TaskExecutorCustomizer... taskExecutorCustomizers) { - Assert.notNull(taskExecutorCustomizers, - "TaskExecutorCustomizers must not be null"); - return additionalCustomizers(Arrays.asList(taskExecutorCustomizers)); + TaskExecutorCustomizer... customizers) { + Assert.notNull(customizers, "Customizers must not be null"); + return additionalCustomizers(Arrays.asList(customizers)); } /** * Add {@link TaskExecutorCustomizer TaskExecutorCustomizers} that should be applied * to the {@link ThreadPoolTaskExecutor}. Customizers are applied in the order that * 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 * @see #customizers(TaskExecutorCustomizer...) */ public TaskExecutorBuilder additionalCustomizers( - Collection taskExecutorCustomizers) { - Assert.notNull(taskExecutorCustomizers, - "TaskExecutorCustomizers must not be null"); + Iterable customizers) { + Assert.notNull(customizers, "Customizers must not be null"); return new TaskExecutorBuilder(this.queueCapacity, this.corePoolSize, this.maxPoolSize, this.allowCoreThreadTimeOut, this.keepAlive, this.threadNamePrefix, this.taskDecorator, - append(this.taskExecutorCustomizers, taskExecutorCustomizers)); + append(this.customizers, customizers)); } /** @@ -288,18 +276,15 @@ public class TaskExecutorBuilder { map.from(this.threadNamePrefix).whenHasText() .to(taskExecutor::setThreadNamePrefix); map.from(this.taskDecorator).to(taskExecutor::setTaskDecorator); - - if (!CollectionUtils.isEmpty(this.taskExecutorCustomizers)) { - for (TaskExecutorCustomizer customizer : this.taskExecutorCustomizers) { - customizer.customize(taskExecutor); - } + if (!CollectionUtils.isEmpty(this.customizers)) { + this.customizers.forEach((customizer) -> customizer.customize(taskExecutor)); } return taskExecutor; } - private static Set append(Set set, Collection additions) { + private Set append(Set set, Iterable additions) { Set result = new LinkedHashSet<>((set != null) ? set : Collections.emptySet()); - result.addAll(additions); + additions.forEach(result::add); return Collections.unmodifiableSet(result); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskSchedulerBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskSchedulerBuilder.java index ed4f71916c7..ca8b74613ce 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskSchedulerBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskSchedulerBuilder.java @@ -17,7 +17,6 @@ package org.springframework.boot.task; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; @@ -45,22 +44,19 @@ public class TaskSchedulerBuilder { private final String threadNamePrefix; - private final Set taskSchedulerCustomizers; + private final Set customizers; - public TaskSchedulerBuilder(TaskSchedulerCustomizer... taskSchedulerCustomizers) { - Assert.notNull(taskSchedulerCustomizers, - "TaskSchedulerCustomizers must not be null"); + public TaskSchedulerBuilder() { this.poolSize = null; this.threadNamePrefix = null; - this.taskSchedulerCustomizers = Collections.unmodifiableSet( - new LinkedHashSet<>(Arrays.asList(taskSchedulerCustomizers))); + this.customizers = null; } public TaskSchedulerBuilder(Integer poolSize, String threadNamePrefix, Set taskSchedulerCustomizers) { this.poolSize = poolSize; this.threadNamePrefix = threadNamePrefix; - this.taskSchedulerCustomizers = taskSchedulerCustomizers; + this.customizers = taskSchedulerCustomizers; } /** @@ -70,7 +66,7 @@ public class TaskSchedulerBuilder { */ public TaskSchedulerBuilder poolSize(int poolSize) { return new TaskSchedulerBuilder(poolSize, this.threadNamePrefix, - this.taskSchedulerCustomizers); + this.customizers); } /** @@ -80,7 +76,7 @@ public class TaskSchedulerBuilder { */ public TaskSchedulerBuilder threadNamePrefix(String 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 * order that they were added after builder configuration has been applied. Setting * 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 * @see #additionalCustomizers(TaskSchedulerCustomizer...) */ - public TaskSchedulerBuilder customizers( - TaskSchedulerCustomizer... taskSchedulerCustomizers) { - Assert.notNull(taskSchedulerCustomizers, - "TaskSchedulerCustomizers must not be null"); - return customizers(Arrays.asList(taskSchedulerCustomizers)); + public TaskSchedulerBuilder customizers(TaskSchedulerCustomizer... customizers) { + Assert.notNull(customizers, "Customizers must not be null"); + return customizers(Arrays.asList(customizers)); } /** @@ -104,48 +98,44 @@ public class TaskSchedulerBuilder { * applied to the {@link ThreadPoolTaskScheduler}. Customizers are applied in the * order that they were added after builder configuration has been applied. Setting * 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 * @see #additionalCustomizers(TaskSchedulerCustomizer...) */ public TaskSchedulerBuilder customizers( - Collection taskSchedulerCustomizers) { - Assert.notNull(taskSchedulerCustomizers, - "TaskSchedulerCustomizers must not be null"); + Iterable customizers) { + Assert.notNull(customizers, "Customizers must not be null"); return new TaskSchedulerBuilder(this.poolSize, this.threadNamePrefix, - Collections.unmodifiableSet(new LinkedHashSet( - taskSchedulerCustomizers))); + append(null, customizers)); } /** * Add {@link TaskSchedulerCustomizer taskSchedulerCustomizers} that should be applied * to the {@link ThreadPoolTaskScheduler}. Customizers are applied in the order that * 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 * @see #customizers(TaskSchedulerCustomizer...) */ public TaskSchedulerBuilder additionalCustomizers( - TaskSchedulerCustomizer... taskSchedulerCustomizers) { - Assert.notNull(taskSchedulerCustomizers, - "TaskSchedulerCustomizers must not be null"); - return additionalCustomizers(Arrays.asList(taskSchedulerCustomizers)); + TaskSchedulerCustomizer... customizers) { + Assert.notNull(customizers, "Customizers must not be null"); + return additionalCustomizers(Arrays.asList(customizers)); } /** * Add {@link TaskSchedulerCustomizer taskSchedulerCustomizers} that should be applied * to the {@link ThreadPoolTaskScheduler}. Customizers are applied in the order that * 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 * @see #customizers(TaskSchedulerCustomizer...) */ public TaskSchedulerBuilder additionalCustomizers( - Collection taskSchedulerCustomizers) { - Assert.notNull(taskSchedulerCustomizers, - "TaskSchedulerCustomizers must not be null"); + Iterable customizers) { + Assert.notNull(customizers, "Customizers must not be null"); 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(); map.from(this.poolSize).to(taskScheduler::setPoolSize); map.from(this.threadNamePrefix).to(taskScheduler::setThreadNamePrefix); - - if (!CollectionUtils.isEmpty(this.taskSchedulerCustomizers)) { - for (TaskSchedulerCustomizer customizer : this.taskSchedulerCustomizers) { - customizer.customize(taskScheduler); - } + if (!CollectionUtils.isEmpty(this.customizers)) { + this.customizers.forEach((customizer) -> customizer.customize(taskScheduler)); } return taskScheduler; } - private static Set append(Set set, Collection additions) { + private Set append(Set set, Iterable additions) { Set result = new LinkedHashSet<>((set != null) ? set : Collections.emptySet()); - result.addAll(additions); + additions.forEach(result::add); return Collections.unmodifiableSet(result); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/TaskExecutorBuilderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/TaskExecutorBuilderTests.java index 506fcaa7eb4..b373c8b811c 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/TaskExecutorBuilderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/TaskExecutorBuilderTests.java @@ -47,13 +47,6 @@ public class TaskExecutorBuilderTests { 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 public void poolSettingsShouldApply() { ThreadPoolTaskExecutor executor = this.builder.queueCapacity(10).corePoolSize(4) @@ -85,14 +78,14 @@ public class TaskExecutorBuilderTests { @Test public void customizersWhenCustomizersAreNullShouldThrowException() { 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); } @Test public void customizersCollectionWhenCustomizersAreNullShouldThrowException() { 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) null); } @@ -135,14 +128,14 @@ public class TaskExecutorBuilderTests { @Test public void additionalCustomizersWhenCustomizersAreNullShouldThrowException() { 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); } @Test public void additionalCustomizersCollectionWhenCustomizersAreNullShouldThrowException() { 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) null); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/TaskSchedulerBuilderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/TaskSchedulerBuilderTests.java index 39a9a5bf632..8ae498b207d 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/TaskSchedulerBuilderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/task/TaskSchedulerBuilderTests.java @@ -43,13 +43,6 @@ public class TaskSchedulerBuilderTests { 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 public void poolSettingsShouldApply() { ThreadPoolTaskScheduler scheduler = this.builder.poolSize(4).build(); @@ -66,14 +59,14 @@ public class TaskSchedulerBuilderTests { @Test public void customizersWhenCustomizersAreNullShouldThrowException() { 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); } @Test public void customizersCollectionWhenCustomizersAreNullShouldThrowException() { 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) null); } @@ -108,14 +101,14 @@ public class TaskSchedulerBuilderTests { @Test public void additionalCustomizersWhenCustomizersAreNullShouldThrowException() { 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); } @Test public void additionalCustomizersCollectionWhenCustomizersAreNullShouldThrowException() { 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) null); }