From 9db9e563dc029c1361ed9b04f58340454fc2401e Mon Sep 17 00:00:00 2001 From: Tiare Balbi Bonamini Date: Mon, 7 Sep 2015 19:12:01 -0300 Subject: [PATCH 1/4] Fixes gh-1563 --- .../AsyncTaskExecutorAutoConfiguration.java | 54 ++++++++++++ .../task/AsyncTaskExecutorProperties.java | 54 ++++++++++++ .../main/resources/META-INF/spring.factories | 1 + ...syncTaskExecutorAutoConfigurationTest.java | 83 +++++++++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfiguration.java create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorProperties.java create mode 100644 spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfigurationTest.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfiguration.java new file mode 100644 index 000000000000..fd850f782b96 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfiguration.java @@ -0,0 +1,54 @@ +/* + * Copyright 2012-2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.springframework.boot.autoconfigure.task; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.task.AsyncTaskExecutor; +import org.springframework.core.task.SimpleAsyncTaskExecutor; +import org.springframework.core.task.TaskExecutor; + +/** + * {@link EnableAutoConfiguration Auto-configuration} for Async Task Executor. + * + * @author Tiarê Balbi Bonamini + * @see TaskExecutor + * @see SimpleAsyncTaskExecutor + */ +@Configuration +@ConditionalOnClass(value = SimpleAsyncTaskExecutor.class) +@EnableConfigurationProperties(AsyncTaskExecutorProperties.class) +public class AsyncTaskExecutorAutoConfiguration { + + @Autowired + private AsyncTaskExecutorProperties taskProperties; + + @Bean + @ConditionalOnMissingBean + public AsyncTaskExecutor simpleAsyncTaskExecutor() { + SimpleAsyncTaskExecutor asyncTaskExecutor = new SimpleAsyncTaskExecutor(); + asyncTaskExecutor.setConcurrencyLimit(taskProperties.getConcurrencyLimit()); + asyncTaskExecutor.setThreadNamePrefix(taskProperties.getThreadNamePrefix()); + return asyncTaskExecutor; + } +} \ No newline at end of file diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorProperties.java new file mode 100644 index 000000000000..c579f39ab450 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorProperties.java @@ -0,0 +1,54 @@ +/* + * Copyright 2012-2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.springframework.boot.autoconfigure.task; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * + * Configuration properties for TaskExecutor + * + * @author Tiarê Balbi Bonamini + */ +@ConfigurationProperties(prefix = "spring.task") +public class AsyncTaskExecutorProperties { + + private String threadNamePrefix = ""; + + private int concurrencyLimit = -1; + + public String getThreadNamePrefix() { + return threadNamePrefix; + } + + public void setThreadNamePrefix(String threadNamePrefix) { + this.threadNamePrefix = threadNamePrefix; + } + + /** + * Set the maximum number of concurrent access attempts allowed. -1 indicates + * unbounded concurrency. + */ + public int getConcurrencyLimit() { + return concurrencyLimit; + } + + public void setConcurrencyLimit(int concurrencyLimit) { + this.concurrencyLimit = concurrencyLimit; + } +} \ No newline at end of file diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index 94718ac2c0fd..5cef23824f8e 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -68,6 +68,7 @@ org.springframework.boot.autoconfigure.social.LinkedInAutoConfiguration,\ org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration,\ org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\ org.springframework.boot.autoconfigure.velocity.VelocityAutoConfiguration,\ +org.springframework.boot.autoconfigure.task.AsyncTaskExecutorAutoConfiguration,\ org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\ org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\ org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\ diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfigurationTest.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfigurationTest.java new file mode 100644 index 000000000000..d3832db98ef3 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfigurationTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2012-2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.springframework.boot.autoconfigure.task; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Test; +import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.core.task.AsyncTaskExecutor; +import org.springframework.core.task.SimpleAsyncTaskExecutor; + +/** + * Test case for {@link AsyncTaskExecutorAutoConfiguration} + * + * @author Tiarê Balbi Bonamini + * @since 1.3.0 + */ +public class AsyncTaskExecutorAutoConfigurationTest { + + private AnnotationConfigApplicationContext context; + + @After + public void close() { + + if (this.context != null) { + this.context.close(); + } + } + + @Test + public void testTaskExecutorCreated() { + this.context = new AnnotationConfigApplicationContext(); + + this.context.register(PropertyPlaceholderAutoConfiguration.class, + AsyncTaskExecutorAutoConfiguration.class); + this.context.refresh(); + + AsyncTaskExecutor bean = this.context.getBean(AsyncTaskExecutor.class); + + assertNotNull(bean); + assertTrue(bean instanceof SimpleAsyncTaskExecutor); + + } + + @Test + public void testCustomProperties() { + + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.task.concurrency-limit:30", + "spring.task.thread-name-prefix:spring"); + + this.context.register(PropertyPlaceholderAutoConfiguration.class, + AsyncTaskExecutorAutoConfiguration.class); + + this.context.refresh(); + + SimpleAsyncTaskExecutor bean = (SimpleAsyncTaskExecutor) this.context + .getBean(AsyncTaskExecutor.class); + assertEquals(30, bean.getConcurrencyLimit()); + assertEquals("spring", bean.getThreadNamePrefix()); + + } + +} \ No newline at end of file From 1df2deb0e2c1a169532fc78e700ed5a458a37131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiar=C3=AA=20Balbi=20Bonamini?= <0484.gerente@olearys.com.br> Date: Tue, 8 Sep 2015 09:51:35 -0300 Subject: [PATCH 2/4] Review Fixes gh-1563 --- .../AsyncTaskExecutorAutoConfiguration.java | 29 +++++++------- .../task/AsyncTaskExecutorProperties.java | 40 ++++++++++--------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfiguration.java index fd850f782b96..84487a2e5d95 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfiguration.java @@ -19,36 +19,37 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.task.AsyncTaskExecutor; import org.springframework.core.task.SimpleAsyncTaskExecutor; -import org.springframework.core.task.TaskExecutor; +import org.springframework.scheduling.annotation.EnableAsync; /** * {@link EnableAutoConfiguration Auto-configuration} for Async Task Executor. * * @author Tiarê Balbi Bonamini - * @see TaskExecutor + * @see EnableAsync + * @see AsyncTaskExecutor * @see SimpleAsyncTaskExecutor */ @Configuration -@ConditionalOnClass(value = SimpleAsyncTaskExecutor.class) +@ConditionalOnBean(annotation = EnableAsync.class) @EnableConfigurationProperties(AsyncTaskExecutorProperties.class) public class AsyncTaskExecutorAutoConfiguration { - @Autowired - private AsyncTaskExecutorProperties taskProperties; + @Autowired + private AsyncTaskExecutorProperties taskProperties; - @Bean - @ConditionalOnMissingBean - public AsyncTaskExecutor simpleAsyncTaskExecutor() { - SimpleAsyncTaskExecutor asyncTaskExecutor = new SimpleAsyncTaskExecutor(); - asyncTaskExecutor.setConcurrencyLimit(taskProperties.getConcurrencyLimit()); - asyncTaskExecutor.setThreadNamePrefix(taskProperties.getThreadNamePrefix()); - return asyncTaskExecutor; - } + @Bean + @ConditionalOnMissingBean + public AsyncTaskExecutor simpleAsyncTaskExecutor() { + SimpleAsyncTaskExecutor asyncTaskExecutor = new SimpleAsyncTaskExecutor(); + asyncTaskExecutor.setConcurrencyLimit(taskProperties.getConcurrencyLimit()); + asyncTaskExecutor.setThreadNamePrefix(taskProperties.getThreadNamePrefix()); + return asyncTaskExecutor; + } } \ No newline at end of file diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorProperties.java index c579f39ab450..76e870748219 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorProperties.java @@ -20,7 +20,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties; /** - * * Configuration properties for TaskExecutor * * @author Tiarê Balbi Bonamini @@ -28,27 +27,30 @@ @ConfigurationProperties(prefix = "spring.task") public class AsyncTaskExecutorProperties { - private String threadNamePrefix = ""; + /** + * Prefix to use for the names of newly created threads + */ + private String threadNamePrefix = "async-executor-task-"; - private int concurrencyLimit = -1; + /** + * Set the maximum number of concurrent access attempts allowed. + * -1 indicates unbounded concurrency. + */ + private int concurrencyLimit = -1; - public String getThreadNamePrefix() { - return threadNamePrefix; - } + public String getThreadNamePrefix() { + return threadNamePrefix; + } - public void setThreadNamePrefix(String threadNamePrefix) { - this.threadNamePrefix = threadNamePrefix; - } + public void setThreadNamePrefix(String threadNamePrefix) { + this.threadNamePrefix = threadNamePrefix; + } - /** - * Set the maximum number of concurrent access attempts allowed. -1 indicates - * unbounded concurrency. - */ - public int getConcurrencyLimit() { - return concurrencyLimit; - } + public int getConcurrencyLimit() { + return concurrencyLimit; + } - public void setConcurrencyLimit(int concurrencyLimit) { - this.concurrencyLimit = concurrencyLimit; - } + public void setConcurrencyLimit(int concurrencyLimit) { + this.concurrencyLimit = concurrencyLimit; + } } \ No newline at end of file From 49754144cccbb1eac96fc91af3637ab0bc91c882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiar=C3=AA=20Balbi=20Bonamini?= <0484.gerente@olearys.com.br> Date: Tue, 8 Sep 2015 09:57:17 -0300 Subject: [PATCH 3/4] Fixed Code Formatter --- .../AsyncTaskExecutorAutoConfiguration.java | 20 +++---- .../task/AsyncTaskExecutorProperties.java | 52 +++++++++---------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfiguration.java index 84487a2e5d95..0356bf0dd55f 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfiguration.java @@ -41,15 +41,15 @@ @EnableConfigurationProperties(AsyncTaskExecutorProperties.class) public class AsyncTaskExecutorAutoConfiguration { - @Autowired - private AsyncTaskExecutorProperties taskProperties; + @Autowired + private AsyncTaskExecutorProperties taskProperties; - @Bean - @ConditionalOnMissingBean - public AsyncTaskExecutor simpleAsyncTaskExecutor() { - SimpleAsyncTaskExecutor asyncTaskExecutor = new SimpleAsyncTaskExecutor(); - asyncTaskExecutor.setConcurrencyLimit(taskProperties.getConcurrencyLimit()); - asyncTaskExecutor.setThreadNamePrefix(taskProperties.getThreadNamePrefix()); - return asyncTaskExecutor; - } + @Bean + @ConditionalOnMissingBean + public AsyncTaskExecutor simpleAsyncTaskExecutor() { + SimpleAsyncTaskExecutor asyncTaskExecutor = new SimpleAsyncTaskExecutor(); + asyncTaskExecutor.setConcurrencyLimit(taskProperties.getConcurrencyLimit()); + asyncTaskExecutor.setThreadNamePrefix(taskProperties.getThreadNamePrefix()); + return asyncTaskExecutor; + } } \ No newline at end of file diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorProperties.java index 76e870748219..b943b581c1c2 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorProperties.java @@ -27,30 +27,30 @@ @ConfigurationProperties(prefix = "spring.task") public class AsyncTaskExecutorProperties { - /** - * Prefix to use for the names of newly created threads - */ - private String threadNamePrefix = "async-executor-task-"; - - /** - * Set the maximum number of concurrent access attempts allowed. - * -1 indicates unbounded concurrency. - */ - private int concurrencyLimit = -1; - - public String getThreadNamePrefix() { - return threadNamePrefix; - } - - public void setThreadNamePrefix(String threadNamePrefix) { - this.threadNamePrefix = threadNamePrefix; - } - - public int getConcurrencyLimit() { - return concurrencyLimit; - } - - public void setConcurrencyLimit(int concurrencyLimit) { - this.concurrencyLimit = concurrencyLimit; - } + /** + * Prefix to use for the names of newly created threads + */ + private String threadNamePrefix = "async-executor-task-"; + + /** + * Set the maximum number of concurrent access attempts allowed. -1 indicates + * unbounded concurrency. + */ + private int concurrencyLimit = -1; + + public String getThreadNamePrefix() { + return threadNamePrefix; + } + + public void setThreadNamePrefix(String threadNamePrefix) { + this.threadNamePrefix = threadNamePrefix; + } + + public int getConcurrencyLimit() { + return concurrencyLimit; + } + + public void setConcurrencyLimit(int concurrencyLimit) { + this.concurrencyLimit = concurrencyLimit; + } } \ No newline at end of file From 70cd872e49c1833cabaa235b08e976abba1d768d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiar=C3=AA=20Balbi=20Bonamini?= <0484.gerente@olearys.com.br> Date: Tue, 8 Sep 2015 10:33:57 -0300 Subject: [PATCH 4/4] Fixed test case gh-1563 --- .../AsyncTaskExecutorAutoConfigurationTest.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfigurationTest.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfigurationTest.java index d3832db98ef3..d52f8fb4e683 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfigurationTest.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/AsyncTaskExecutorAutoConfigurationTest.java @@ -24,8 +24,10 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Configuration; import org.springframework.core.task.AsyncTaskExecutor; import org.springframework.core.task.SimpleAsyncTaskExecutor; +import org.springframework.scheduling.annotation.EnableAsync; /** * Test case for {@link AsyncTaskExecutorAutoConfiguration} @@ -49,7 +51,8 @@ public void close() { public void testTaskExecutorCreated() { this.context = new AnnotationConfigApplicationContext(); - this.context.register(PropertyPlaceholderAutoConfiguration.class, + this.context.register(CustomAsyncAnnotationConfig.class, + PropertyPlaceholderAutoConfiguration.class, AsyncTaskExecutorAutoConfiguration.class); this.context.refresh(); @@ -68,7 +71,8 @@ public void testCustomProperties() { "spring.task.concurrency-limit:30", "spring.task.thread-name-prefix:spring"); - this.context.register(PropertyPlaceholderAutoConfiguration.class, + this.context.register(CustomAsyncAnnotationConfig.class, + PropertyPlaceholderAutoConfiguration.class, AsyncTaskExecutorAutoConfiguration.class); this.context.refresh(); @@ -80,4 +84,9 @@ public void testCustomProperties() { } + @Configuration + @EnableAsync + static class CustomAsyncAnnotationConfig { + } + } \ No newline at end of file