-
Notifications
You must be signed in to change notification settings - Fork 41.3k
Fixes gh-1563 #3919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes gh-1563 #3919
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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.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.scheduling.annotation.EnableAsync; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for Async Task Executor. | ||
* | ||
* @author Tiarê Balbi Bonamini | ||
* @see EnableAsync | ||
* @see AsyncTaskExecutor | ||
* @see SimpleAsyncTaskExecutor | ||
*/ | ||
@Configuration | ||
@ConditionalOnBean(annotation = EnableAsync.class) | ||
@EnableConfigurationProperties(AsyncTaskExecutorProperties.class) | ||
public class AsyncTaskExecutorAutoConfiguration { | ||
|
||
@Autowired | ||
private AsyncTaskExecutorProperties taskProperties; | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public AsyncTaskExecutor simpleAsyncTaskExecutor() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's the simple version. If you look at the original issue, there might be other environment where we could grab the task executor. We can take it from there and improve it but I just want to be sure you didn't miss that. |
||
SimpleAsyncTaskExecutor asyncTaskExecutor = new SimpleAsyncTaskExecutor(); | ||
asyncTaskExecutor.setConcurrencyLimit(taskProperties.getConcurrencyLimit()); | ||
asyncTaskExecutor.setThreadNamePrefix(taskProperties.getThreadNamePrefix()); | ||
return asyncTaskExecutor; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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 { | ||
|
||
/** | ||
* Prefix to use for the names of newly created threads | ||
*/ | ||
private String threadNamePrefix = "async-executor-task-"; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you really intend the default name prefix to be empty? |
||
/** | ||
* 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add meta-data description on the fields instead (as we use that for content assistance). Check other There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, i'll be updating everything, thanks for the review. |
||
return concurrencyLimit; | ||
} | ||
|
||
public void setConcurrencyLimit(int concurrencyLimit) { | ||
this.concurrencyLimit = concurrencyLimit; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* 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.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} | ||
* | ||
* @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(CustomAsyncAnnotationConfig.class, | ||
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(CustomAsyncAnnotationConfig.class, | ||
PropertyPlaceholderAutoConfiguration.class, | ||
AsyncTaskExecutorAutoConfiguration.class); | ||
|
||
this.context.refresh(); | ||
|
||
SimpleAsyncTaskExecutor bean = (SimpleAsyncTaskExecutor) this.context | ||
.getBean(AsyncTaskExecutor.class); | ||
assertEquals(30, bean.getConcurrencyLimit()); | ||
assertEquals("spring", bean.getThreadNamePrefix()); | ||
|
||
} | ||
|
||
@Configuration | ||
@EnableAsync | ||
static class CustomAsyncAnnotationConfig { | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, you're creating that bean unconditionally in practice:
SimpleAsyncTaskExecutor
is inspring-core
so it will always be available. The condition is still good IMO but you should add a condition that checks if@EnableAsync
has been specified in the configuration.Usually, when an
@EnableXyz
annotation is added, a post processor is registered in the context. You should look for that.