-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DCJ-457] Stairway can be built with a provided ThreadPoolTaskExecutor (
#149) * [DCJ-457] Stairway can be built with a provided ThreadPoolTaskExecutor This allows calling services to bring their own instrumented executor to serve as their Stairway's threadpool. If unspecified in StairwayBuilder, a DefaultThreadPoolTaskExecutor will be constructed and used. DefaultThreadPoolTaskExecutor is a public class: calling services may elect to instantiate it themselves (e.g. a Spring Boot service would register this as a Bean for automatic actuator instrumentation). * Also assert that DefaultThreadPoolTaskExecutors are running * Docstring references default maxParallelFlights directly If this value were to change in the future, the javadoc rendered view would reflect the change. * StairwayImpl constructor executor init follows existing convention Mainly because my usage of Optional.ofNullable…orElse… constructs a new default executor every time, and throws it away if the builder has one set. This is discouraged. I decided to adhere with the existing convention for initializing instance variables from the StairwayBuilder object, which makes the constructor easier to follow. If it's updated in the future with Optional handling, that would be acceptable as long as it's updated completely. * Update mavenLocal development docs This matches TDR's current build file -- the useMavenLocal flag makes it a little easier to leverage locally published artifacts on demand.
- Loading branch information
1 parent
c5a9f98
commit c4e3b26
Showing
8 changed files
with
155 additions
and
64 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
31 changes: 31 additions & 0 deletions
31
stairway/src/main/java/bio/terra/stairway/DefaultThreadPoolTaskExecutor.java
This file contains 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,31 @@ | ||
package bio.terra.stairway; | ||
|
||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
public class DefaultThreadPoolTaskExecutor extends ThreadPoolTaskExecutor { | ||
|
||
static final int DEFAULT_MAX_PARALLEL_FLIGHTS = 20; | ||
|
||
/** | ||
* An initialized {@link ThreadPoolTaskExecutor}. | ||
* | ||
* @param maxParallelFlights the desired pool size, honored if a positive integer, defaults to | ||
* DEFAULT_MAX_PARALLEL_FLIGHTS otherwise. | ||
*/ | ||
public DefaultThreadPoolTaskExecutor(Integer maxParallelFlights) { | ||
super(); | ||
int poolSize = getPoolSize(maxParallelFlights); | ||
super.setCorePoolSize(poolSize); | ||
super.setMaxPoolSize(poolSize); | ||
super.setKeepAliveSeconds(0); | ||
super.setThreadNamePrefix("stairway-thread-"); | ||
super.initialize(); | ||
} | ||
|
||
private int getPoolSize(Integer maxParallelFlights) { | ||
if (maxParallelFlights == null || maxParallelFlights <= 0) { | ||
return DEFAULT_MAX_PARALLEL_FLIGHTS; | ||
} | ||
return maxParallelFlights; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
35 changes: 35 additions & 0 deletions
35
stairway/src/test/java/bio/terra/stairway/DefaultThreadPoolTaskExecutorTest.java
This file contains 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,35 @@ | ||
package bio.terra.stairway; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
@Tag("unit") | ||
class DefaultThreadPoolTaskExecutorTest { | ||
|
||
private static Stream<Arguments> defaultThreadPoolTaskExecutor() { | ||
return Stream.of( | ||
Arguments.of(null, DefaultThreadPoolTaskExecutor.DEFAULT_MAX_PARALLEL_FLIGHTS), | ||
Arguments.of(-1, DefaultThreadPoolTaskExecutor.DEFAULT_MAX_PARALLEL_FLIGHTS), | ||
Arguments.of(0, DefaultThreadPoolTaskExecutor.DEFAULT_MAX_PARALLEL_FLIGHTS), | ||
Arguments.of(1, 1), | ||
Arguments.of(50, 50)); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
void defaultThreadPoolTaskExecutor(Integer maxParallelFlights, Integer expectedPoolSize) { | ||
var executor = new DefaultThreadPoolTaskExecutor(maxParallelFlights); | ||
assertThat(executor.getCorePoolSize(), equalTo(expectedPoolSize)); | ||
assertThat(executor.getMaxPoolSize(), equalTo(expectedPoolSize)); | ||
assertThat(executor.getKeepAliveSeconds(), equalTo(0)); | ||
assertThat(executor.getThreadNamePrefix(), equalTo("stairway-thread-")); | ||
assertTrue(executor.isRunning()); | ||
} | ||
} |
Oops, something went wrong.