Commit d5eaaf6e authored by Johnny Lim's avatar Johnny Lim Committed by Stephane Nicoll

Polish

Closes gh-14049
parent d0f27296
...@@ -65,20 +65,15 @@ public class TaskExecutorAutoConfiguration { ...@@ -65,20 +65,15 @@ public class TaskExecutorAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public TaskExecutorBuilder taskExecutorBuilder() { public TaskExecutorBuilder taskExecutorBuilder() {
TaskExecutorBuilder builder = new TaskExecutorBuilder();
TaskProperties.Pool pool = this.properties.getPool(); TaskProperties.Pool pool = this.properties.getPool();
builder = builder.queueCapacity(pool.getQueueCapacity()) return new TaskExecutorBuilder().queueCapacity(pool.getQueueCapacity())
.corePoolSize(pool.getCoreSize()).maxPoolSize(pool.getMaxSize()) .corePoolSize(pool.getCoreSize()).maxPoolSize(pool.getMaxSize())
.allowCoreThreadTimeOut(pool.isAllowCoreThreadTimeout()) .allowCoreThreadTimeOut(pool.isAllowCoreThreadTimeout())
.keepAlive(pool.getKeepAlive()); .keepAlive(pool.getKeepAlive())
builder = builder.threadNamePrefix(this.properties.getThreadNamePrefix()); .threadNamePrefix(this.properties.getThreadNamePrefix())
builder = builder.customizers( .customizers(this.taskExecutorCustomizers.stream()
this.taskExecutorCustomizers.stream().collect(Collectors.toList())); .collect(Collectors.toList()))
TaskDecorator taskDecorator = this.taskDecorator.getIfUnique(); .taskDecorator(this.taskDecorator.getIfUnique());
if (taskDecorator != null) {
builder = builder.taskDecorator(taskDecorator);
}
return builder;
} }
@Bean(name = APPLICATION_TASK_EXECUTOR_BEAN_NAME) @Bean(name = APPLICATION_TASK_EXECUTOR_BEAN_NAME)
......
...@@ -50,8 +50,8 @@ public class TaskProperties { ...@@ -50,8 +50,8 @@ public class TaskProperties {
public static class Pool { public static class Pool {
/** /**
* Queue capacity. A unbounded capacity does not increase the pool and therefore * Queue capacity. An unbounded capacity does not increase the pool and therefore
* ignores the "max-size" parameter. * ignores the "max-size" property.
*/ */
private int queueCapacity = Integer.MAX_VALUE; private int queueCapacity = Integer.MAX_VALUE;
......
...@@ -109,19 +109,19 @@ public class TaskExecutorAutoConfigurationTests { ...@@ -109,19 +109,19 @@ public class TaskExecutorAutoConfigurationTests {
} }
@Test @Test
public void taskExecutorWhenHasCustomTaskExecutorShouldBAckOff() { public void taskExecutorWhenHasCustomTaskExecutorShouldBackOff() {
this.contextRunner.withUserConfiguration(CustomTaskExecutorConfig.class) this.contextRunner.withUserConfiguration(CustomTaskExecutorConfig.class)
.run((context) -> { .run((context) -> {
assertThat(context).hasSingleBean(Executor.class); assertThat(context).hasSingleBean(Executor.class);
assertThat(context.getBean(Executor.class)) assertThat(context.getBean(Executor.class))
.isSameAs(context.getBean("customTaskExecutorBuilder")); .isSameAs(context.getBean("customTaskExecutor"));
}); });
} }
@Test @Test
public void taskExecutorBuilderShouldApplyCustomizer() { public void taskExecutorBuilderShouldApplyCustomizer() {
this.contextRunner.withUserConfiguration(CustomTaskExecutorConfig.class, this.contextRunner.withUserConfiguration(TaskExecutorCustomizerConfig.class)
TaskExecutorCustomizerConfig.class).run((context) -> { .run((context) -> {
TaskExecutorCustomizer customizer = context TaskExecutorCustomizer customizer = context
.getBean(TaskExecutorCustomizer.class); .getBean(TaskExecutorCustomizer.class);
ThreadPoolTaskExecutor executor = context ThreadPoolTaskExecutor executor = context
...@@ -138,8 +138,8 @@ public class TaskExecutorAutoConfigurationTests { ...@@ -138,8 +138,8 @@ public class TaskExecutorAutoConfigurationTests {
.run((context) -> { .run((context) -> {
assertThat(context).hasSingleBean(TaskExecutor.class); assertThat(context).hasSingleBean(TaskExecutor.class);
TestBean bean = context.getBean(TestBean.class); TestBean bean = context.getBean(TestBean.class);
String text = bean.echo("test").get(); String text = bean.echo("something").get();
assertThat(text).contains("executor-test-").contains("test"); assertThat(text).contains("executor-test-").contains("something");
}); });
} }
...@@ -188,7 +188,7 @@ public class TaskExecutorAutoConfigurationTests { ...@@ -188,7 +188,7 @@ public class TaskExecutorAutoConfigurationTests {
static class CustomTaskExecutorConfig { static class CustomTaskExecutorConfig {
@Bean @Bean
public Executor customTaskExecutorBuilder() { public Executor customTaskExecutor() {
return new SyncTaskExecutor(); return new SyncTaskExecutor();
} }
......
...@@ -166,7 +166,7 @@ content into your application. Rather, pick only the properties that you need. ...@@ -166,7 +166,7 @@ content into your application. Rather, pick only the properties that you need.
spring.task.pool.core-size=8 # Core number of threads. spring.task.pool.core-size=8 # Core number of threads.
spring.task.pool.keep-alive=60s # Time limit for which threads may remain idle before being terminated. spring.task.pool.keep-alive=60s # Time limit for which threads may remain idle before being terminated.
spring.task.pool.max-size= # Maximum allowed number of threads. If tasks are filling up the queue, the pool can expand up to that size to accommodate the load. Ignored if the queue is unbounded. spring.task.pool.max-size= # Maximum allowed number of threads. If tasks are filling up the queue, the pool can expand up to that size to accommodate the load. Ignored if the queue is unbounded.
spring.task.pool.queue-capacity= # Queue capacity. A unbounded capacity does not increase the pool and therefore ignores the "max-size" parameter. spring.task.pool.queue-capacity= # Queue capacity. An unbounded capacity does not increase the pool and therefore ignores the "max-size" property.
spring.task.thread-name-prefix=executor- # Prefix to use for the names of newly created threads. spring.task.thread-name-prefix=executor- # Prefix to use for the names of newly created threads.
# ---------------------------------------- # ----------------------------------------
......
...@@ -4638,7 +4638,7 @@ URLs of your server in your application.properties, as shown in the following ex ...@@ -4638,7 +4638,7 @@ URLs of your server in your application.properties, as shown in the following ex
If you need to customize connection settings, you can use the `spring.ldap.base` and If you need to customize connection settings, you can use the `spring.ldap.base` and
`spring.ldap.base-environment` properties. `spring.ldap.base-environment` properties.
A `LdapContextSource` is auto-configured based on these settings. If you need to customize An `LdapContextSource` is auto-configured based on these settings. If you need to customize
it, for instance to use a `PooledContextSource`, you can still inject the auto-configured it, for instance to use a `PooledContextSource`, you can still inject the auto-configured
`LdapContextSource`. Make sure to flag your customized `ContextSource` as `@Primary` so `LdapContextSource`. Make sure to flag your customized `ContextSource` as `@Primary` so
that the auto-configured `LdapTemplate` uses it. that the auto-configured `LdapTemplate` uses it.
...@@ -6156,7 +6156,7 @@ following example: ...@@ -6156,7 +6156,7 @@ following example:
This changes the thread pool to use a bounded queue so that when the queue is full (100 This changes the thread pool to use a bounded queue so that when the queue is full (100
tasks), the thread pool increases to maximum 16 threads. Shrinking of the pool is more tasks), the thread pool increases to maximum 16 threads. Shrinking of the pool is more
aggressive as well as threads are reclaimed when they are idle for 10 seconds (rather than aggressive as threads are reclaimed when they are idle for 10 seconds (rather than
60 seconds by default). 60 seconds by default).
...@@ -6334,8 +6334,8 @@ web application. ...@@ -6334,8 +6334,8 @@ web application.
* `RANDOM_PORT`: Loads a `WebServerApplicationContext` and provides a real web * `RANDOM_PORT`: Loads a `WebServerApplicationContext` and provides a real web
environment. Embedded servers are started and listen on a random port. environment. Embedded servers are started and listen on a random port.
* `DEFINED_PORT`: Loads a `WebServerApplicationContext` and provides a real web * `DEFINED_PORT`: Loads a `WebServerApplicationContext` and provides a real web
environment. Embedded servers are started and listen on a defined port (from your environment. Embedded servers are started and listen on a defined port (from your
`application.properties` or on the default port of `8080`). `application.properties`) or on the default port of `8080`.
* `NONE`: Loads an `ApplicationContext` by using `SpringApplication` but does not provide * `NONE`: Loads an `ApplicationContext` by using `SpringApplication` but does not provide
_any_ web environment (mock or otherwise). _any_ web environment (mock or otherwise).
......
...@@ -31,7 +31,7 @@ public abstract class JsonParserFactory { ...@@ -31,7 +31,7 @@ public abstract class JsonParserFactory {
/** /**
* Static factory for the "best" JSON parser available on the classpath. Tries * Static factory for the "best" JSON parser available on the classpath. Tries
* Jackson, then Gson, Snake YAML,and then falls back to the {@link BasicJsonParser}. * Jackson, then Gson, Snake YAML, and then falls back to the {@link BasicJsonParser}.
* @return a {@link JsonParser} * @return a {@link JsonParser}
*/ */
public static JsonParser getJsonParser() { public static JsonParser getJsonParser() {
......
...@@ -90,7 +90,7 @@ public class TaskExecutorBuilder { ...@@ -90,7 +90,7 @@ public class TaskExecutorBuilder {
} }
/** /**
* Set the capacity of the queue. A unbounded capacity does not increase the pool and * Set the capacity of the queue. An unbounded capacity does not increase the pool and
* therefore ignores {@link #maxPoolSize(int) maxPoolSize}. * therefore ignores {@link #maxPoolSize(int) maxPoolSize}.
* @param queueCapacity the queue capacity to set * @param queueCapacity the queue capacity to set
* @return a new builder instance * @return a new builder instance
...@@ -134,7 +134,7 @@ public class TaskExecutorBuilder { ...@@ -134,7 +134,7 @@ public class TaskExecutorBuilder {
/** /**
* Set whether core threads are allow to time out. When enabled, this enables dynamic * Set whether core threads are allow to time out. When enabled, this enables dynamic
* growing and shrinking of the pool. * growing and shrinking of the pool.
* @param allowCoreThreadTimeOut if core thread are allowed to time out * @param allowCoreThreadTimeOut if core threads are allowed to time out
* @return a new builder instance * @return a new builder instance
*/ */
public TaskExecutorBuilder allowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) { public TaskExecutorBuilder allowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) {
...@@ -262,7 +262,7 @@ public class TaskExecutorBuilder { ...@@ -262,7 +262,7 @@ public class TaskExecutorBuilder {
* @param <T> the type of task executor * @param <T> the type of task executor
* @param taskExecutorClass the template type to create * @param taskExecutorClass the template type to create
* @return a configured {@link ThreadPoolTaskExecutor} instance. * @return a configured {@link ThreadPoolTaskExecutor} instance.
* @see TaskExecutorBuilder#build() * @see #build()
* @see #configure(ThreadPoolTaskExecutor) * @see #configure(ThreadPoolTaskExecutor)
*/ */
public <T extends ThreadPoolTaskExecutor> T build(Class<T> taskExecutorClass) { public <T extends ThreadPoolTaskExecutor> T build(Class<T> taskExecutorClass) {
...@@ -274,8 +274,8 @@ public class TaskExecutorBuilder { ...@@ -274,8 +274,8 @@ public class TaskExecutorBuilder {
* @param <T> the type of task executor * @param <T> the type of task executor
* @param taskExecutor the {@link ThreadPoolTaskExecutor} to configure * @param taskExecutor the {@link ThreadPoolTaskExecutor} to configure
* @return the task executor instance * @return the task executor instance
* @see TaskExecutorBuilder#build() * @see #build()
* @see TaskExecutorBuilder#build(Class) * @see #build(Class)
*/ */
public <T extends ThreadPoolTaskExecutor> T configure(T taskExecutor) { public <T extends ThreadPoolTaskExecutor> T configure(T taskExecutor) {
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
......
...@@ -35,7 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -35,7 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class BeanDefinitionOverrideFailureAnalyzerTests { public class BeanDefinitionOverrideFailureAnalyzerTests {
@Test @Test
public void bindExceptionWithFieldErrorsDueToValidationFailure() { public void analyzeBeanDefinitionOverrideException() {
FailureAnalysis analysis = performAnalysis(BeanOverrideConfiguration.class); FailureAnalysis analysis = performAnalysis(BeanOverrideConfiguration.class);
String description = analysis.getDescription(); String description = analysis.getDescription();
assertThat(description).contains("The bean 'testBean', defined in " assertThat(description).contains("The bean 'testBean', defined in "
......
...@@ -56,9 +56,9 @@ public class TaskExecutorBuilderTests { ...@@ -56,9 +56,9 @@ public class TaskExecutorBuilderTests {
@Test @Test
public void poolSettingsShouldApply() { public void poolSettingsShouldApply() {
ThreadPoolTaskExecutor executor = this.builder.allowCoreThreadTimeOut(true) ThreadPoolTaskExecutor executor = this.builder.queueCapacity(10).corePoolSize(4)
.queueCapacity(10).corePoolSize(4).maxPoolSize(8) .maxPoolSize(8).allowCoreThreadTimeOut(true)
.allowCoreThreadTimeOut(true).keepAlive(Duration.ofMinutes(1)).build(); .keepAlive(Duration.ofMinutes(1)).build();
DirectFieldAccessor dfa = new DirectFieldAccessor(executor); DirectFieldAccessor dfa = new DirectFieldAccessor(executor);
assertThat(dfa.getPropertyValue("queueCapacity")).isEqualTo(10); assertThat(dfa.getPropertyValue("queueCapacity")).isEqualTo(10);
assertThat(executor.getCorePoolSize()).isEqualTo(4); assertThat(executor.getCorePoolSize()).isEqualTo(4);
...@@ -107,10 +107,10 @@ public class TaskExecutorBuilderTests { ...@@ -107,10 +107,10 @@ public class TaskExecutorBuilderTests {
public void customizersShouldBeAppliedLast() { public void customizersShouldBeAppliedLast() {
TaskDecorator taskDecorator = mock(TaskDecorator.class); TaskDecorator taskDecorator = mock(TaskDecorator.class);
ThreadPoolTaskExecutor executor = spy(new ThreadPoolTaskExecutor()); ThreadPoolTaskExecutor executor = spy(new ThreadPoolTaskExecutor());
this.builder.allowCoreThreadTimeOut(true).queueCapacity(10).corePoolSize(4) this.builder.queueCapacity(10).corePoolSize(4).maxPoolSize(8)
.maxPoolSize(8).allowCoreThreadTimeOut(true) .allowCoreThreadTimeOut(true).keepAlive(Duration.ofMinutes(1))
.keepAlive(Duration.ofMinutes(1)).threadNamePrefix("test-") .threadNamePrefix("test-").taskDecorator(taskDecorator)
.taskDecorator(taskDecorator).additionalCustomizers((taskExecutor) -> { .additionalCustomizers((taskExecutor) -> {
verify(taskExecutor).setQueueCapacity(10); verify(taskExecutor).setQueueCapacity(10);
verify(taskExecutor).setCorePoolSize(4); verify(taskExecutor).setCorePoolSize(4);
verify(taskExecutor).setMaxPoolSize(8); verify(taskExecutor).setMaxPoolSize(8);
......
...@@ -440,8 +440,7 @@ public class RestTemplateBuilderTests { ...@@ -440,8 +440,7 @@ public class RestTemplateBuilderTests {
public void connectTimeoutCanBeNullToUseDefault() { public void connectTimeoutCanBeNullToUseDefault() {
ClientHttpRequestFactory requestFactory = this.builder ClientHttpRequestFactory requestFactory = this.builder
.requestFactory(SimpleClientHttpRequestFactory.class) .requestFactory(SimpleClientHttpRequestFactory.class)
.setConnectTimeout(Duration.ofSeconds(5)).setConnectTimeout(null).build() .setConnectTimeout(null).build().getRequestFactory();
.getRequestFactory();
assertThat(ReflectionTestUtils.getField(requestFactory, "connectTimeout")) assertThat(ReflectionTestUtils.getField(requestFactory, "connectTimeout"))
.isEqualTo(-1); .isEqualTo(-1);
} }
...@@ -449,9 +448,8 @@ public class RestTemplateBuilderTests { ...@@ -449,9 +448,8 @@ public class RestTemplateBuilderTests {
@Test @Test
public void readTimeoutCanBeNullToUseDefault() { public void readTimeoutCanBeNullToUseDefault() {
ClientHttpRequestFactory requestFactory = this.builder ClientHttpRequestFactory requestFactory = this.builder
.requestFactory(SimpleClientHttpRequestFactory.class) .requestFactory(SimpleClientHttpRequestFactory.class).setReadTimeout(null)
.setReadTimeout(Duration.ofSeconds(5)).setReadTimeout(null).build() .build().getRequestFactory();
.getRequestFactory();
assertThat(ReflectionTestUtils.getField(requestFactory, "readTimeout")) assertThat(ReflectionTestUtils.getField(requestFactory, "readTimeout"))
.isEqualTo(-1); .isEqualTo(-1);
} }
......
...@@ -48,8 +48,7 @@ public class SampleKafkaApplicationTests { ...@@ -48,8 +48,7 @@ public class SampleKafkaApplicationTests {
&& System.currentTimeMillis() < end) { && System.currentTimeMillis() < end) {
Thread.sleep(250); Thread.sleep(250);
} }
assertThat(this.outputCapture.toString().contains("A simple test message")) assertThat(this.outputCapture.toString()).contains("A simple test message");
.isTrue();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment