-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Add auto-configuration for general use TaskExecutor #5628
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...n/java/org/springframework/boot/autoconfigure/task/JndiTaskExecutorAutoConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2012-2016 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.autoconfigure.AutoConfigureBefore; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.task.TaskExecutor; | ||
import org.springframework.scheduling.concurrent.DefaultManagedTaskExecutor; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for a JNDI located | ||
* {@link TaskExecutor}. | ||
* | ||
* @author Vedran Pavic | ||
* @since 1.4.0 | ||
*/ | ||
@Configuration | ||
@AutoConfigureBefore(TaskExecutorAutoConfiguration.class) | ||
@ConditionalOnProperty(prefix = "spring.task", name = "jndi-name") | ||
@EnableConfigurationProperties(TaskExecutorProperties.class) | ||
public class JndiTaskExecutorAutoConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public TaskExecutor taskExecutor(TaskExecutorProperties properties) { | ||
DefaultManagedTaskExecutor taskExecutor = new DefaultManagedTaskExecutor(); | ||
taskExecutor.setJndiName(properties.getJndiName()); | ||
return taskExecutor; | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
.../main/java/org/springframework/boot/autoconfigure/task/TaskExecutorAutoConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2012-2016 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 java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; | ||
|
||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
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.TaskExecutor; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for {@link TaskExecutor}. | ||
* | ||
* @author Vedran Pavic | ||
* @since 1.4.0 | ||
*/ | ||
@Configuration | ||
@EnableConfigurationProperties(TaskExecutorProperties.class) | ||
public class TaskExecutorAutoConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public TaskExecutor taskExecutor(TaskExecutorProperties properties) { | ||
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); | ||
taskExecutor.setMaxPoolSize(properties.getPool().getMaxSize()); | ||
taskExecutor.setThreadNamePrefix("task-executor-"); | ||
taskExecutor.setRejectedExecutionHandler(new CallerRunsPolicy()); | ||
return taskExecutor; | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
...ure/src/main/java/org/springframework/boot/autoconfigure/task/TaskExecutorProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2012-2016 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 the task executor. | ||
* | ||
* @author Vedran Pavic | ||
* @since 1.4.0 | ||
*/ | ||
@ConfigurationProperties(prefix = "spring.task") | ||
public class TaskExecutorProperties { | ||
|
||
/** | ||
* JNDI location of the executor to delegate to. | ||
*/ | ||
private String jndiName; | ||
|
||
private Pool pool = new Pool(); | ||
|
||
public String getJndiName() { | ||
return this.jndiName; | ||
} | ||
|
||
public void setJndiName(String jndiName) { | ||
this.jndiName = jndiName; | ||
} | ||
|
||
public Pool getPool() { | ||
return this.pool; | ||
} | ||
|
||
public static class Pool { | ||
|
||
/** | ||
* Maximum pool size for the executor. | ||
*/ | ||
private Integer maxSize = 10; | ||
|
||
public Integer getMaxSize() { | ||
return this.maxSize; | ||
} | ||
|
||
public void setMaxSize(Integer maxSize) { | ||
this.maxSize = maxSize; | ||
} | ||
|
||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2012-2016 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. | ||
*/ | ||
|
||
/** | ||
* Auto-configuration for TaskExecutor. | ||
*/ | ||
package org.springframework.boot.autoconfigure.task; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...a/org/springframework/boot/autoconfigure/task/JndiTaskExecutorAutoConfigurationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 2012-2016 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 java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
import javax.naming.Context; | ||
import javax.naming.NamingException; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import org.springframework.boot.autoconfigure.jndi.JndiPropertiesHidingClassLoader; | ||
import org.springframework.boot.autoconfigure.jndi.TestableInitialContextFactory; | ||
import org.springframework.boot.test.util.EnvironmentTestUtils; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.core.task.TaskExecutor; | ||
import org.springframework.scheduling.concurrent.DefaultManagedTaskExecutor; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link JndiTaskExecutorAutoConfiguration}. | ||
* | ||
* @author Vedran Pavic | ||
*/ | ||
public class JndiTaskExecutorAutoConfigurationTests { | ||
|
||
private final AnnotationConfigApplicationContext context = | ||
new AnnotationConfigApplicationContext(); | ||
|
||
private String initialContextFactory; | ||
|
||
private ClassLoader threadContextClassLoader; | ||
|
||
@Before | ||
public void setupJndi() { | ||
this.initialContextFactory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); | ||
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, | ||
TestableInitialContextFactory.class.getName()); | ||
} | ||
|
||
@Before | ||
public void setupThreadContextClassLoader() { | ||
this.threadContextClassLoader = Thread.currentThread().getContextClassLoader(); | ||
Thread.currentThread().setContextClassLoader( | ||
new JndiPropertiesHidingClassLoader(getClass().getClassLoader())); | ||
} | ||
|
||
@After | ||
public void close() { | ||
TestableInitialContextFactory.clearAll(); | ||
if (this.initialContextFactory != null) { | ||
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, | ||
this.initialContextFactory); | ||
} | ||
else { | ||
System.clearProperty(Context.INITIAL_CONTEXT_FACTORY); | ||
} | ||
if (this.context != null) { | ||
this.context.close(); | ||
} | ||
Thread.currentThread().setContextClassLoader(this.threadContextClassLoader); | ||
} | ||
|
||
@Test | ||
public void taskExecutorIsAvailableFromJndi() | ||
throws IllegalStateException, NamingException { | ||
ExecutorService executorService = Executors.newSingleThreadExecutor(); | ||
configureJndi("foo", executorService); | ||
|
||
EnvironmentTestUtils.addEnvironment(this.context, "spring.task.jndi-name=foo"); | ||
registerAndRefresh(JndiTaskExecutorAutoConfiguration.class); | ||
|
||
assertThat(this.context.getBean(TaskExecutor.class)) | ||
.isInstanceOf(DefaultManagedTaskExecutor.class); | ||
} | ||
|
||
private void configureJndi(String name, ExecutorService executorService) | ||
throws IllegalStateException, NamingException { | ||
TestableInitialContextFactory.bind(name, executorService); | ||
} | ||
|
||
private void registerAndRefresh(Class<?>... annotatedClasses) { | ||
this.context.register(annotatedClasses); | ||
this.context.refresh(); | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
.../java/org/springframework/boot/autoconfigure/task/TaskExecutorAutoConfigurationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2012-2016 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.junit.After; | ||
import org.junit.Test; | ||
|
||
import org.springframework.boot.test.util.EnvironmentTestUtils; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.core.task.TaskExecutor; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link TaskExecutorAutoConfiguration}. | ||
* | ||
* @author Vedran Pavic | ||
*/ | ||
public class TaskExecutorAutoConfigurationTests { | ||
|
||
private final AnnotationConfigApplicationContext context = | ||
new AnnotationConfigApplicationContext(); | ||
|
||
@After | ||
public void close() { | ||
if (this.context != null) { | ||
this.context.close(); | ||
} | ||
} | ||
|
||
@Test | ||
public void defaultTaskExecutorExists() { | ||
registerAndRefresh(TaskExecutorAutoConfiguration.class); | ||
|
||
assertThat(this.context.getBean(TaskExecutor.class)) | ||
.isInstanceOf(ThreadPoolTaskExecutor.class); | ||
} | ||
|
||
@Test | ||
public void customMaxPoolSize() { | ||
EnvironmentTestUtils.addEnvironment(this.context, "spring.task.pool.max-size=5"); | ||
registerAndRefresh(TaskExecutorAutoConfiguration.class); | ||
|
||
ThreadPoolTaskExecutor taskExecutor = | ||
this.context.getBean(ThreadPoolTaskExecutor.class); | ||
assertThat(taskExecutor.getMaxPoolSize()).isEqualTo(5); | ||
} | ||
|
||
private void registerAndRefresh(Class<?>... annotatedClasses) { | ||
this.context.register(annotatedClasses); | ||
this.context.refresh(); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We probably want a better default for this. Not sure what it should be.
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.
I just went with the most generic one for starters, due to lack of better ideas.
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.
Yeah I meant to add that as a reminder. I don't have a better idea myself. Maybe we should be adding a configuration option.