Setup ExecutorService within springboot application
Introduction
This post demonstrates how to set up the ExecutorService
in a Spring Boot application.
Environments
- SpringBoot 1.5.12+
- Java 1.8+
The Pom.xml
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.12.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement></project>
The ExecutorService
ExecutorService
is a service that:
An Executor that provides methods to manage termination and methods that can produce a Future for tracking progress of one or more asynchronous tasks.
For example, you can use ExecutorService
to submit a Runnable
or a Callable
to execute in another thread.
Setup the bean via spring
@Configurationpublic class ExecutorServiceConfig {
@Bean("fixedThreadPool") public ExecutorService fixedThreadPool() { return Executors.newFixedThreadPool(5); }
@Bean("singleThreaded") public ExecutorService singleThreadedExecutor() { return Executors.newSingleThreadExecutor(); }
@Bean("cachedThreadPool") public ExecutorService cachedThreadPool() { return Executors.newCachedThreadPool(); }}
Here we used the @Configuration
annotation to configure the Spring beans of ExecutorService
. We set up three types of beans:
- fixedThreadPool: A fixed-size thread pool-based
ExecutorService
to run threads with a fixed size of 5. - singleThreaded: A single-threaded thread pool-based
ExecutorService
that runs all tasks with only one thread. - cachedThreadPool: A cached thread pool-based
ExecutorService
that runs tasks with cached threads. It creates threads only when needed and reuses previously constructed threads when available.
Use the ExecutorService
Autowire the Service:
@Autowired@Qualifier("fixedThreadPool")private ExecutorService executorService;
Then use it:
public <T> Future<T> executeWithResult(Callable<T> callable) { return executorService.submit(callable);}
It’s straightforward, don’t you think?
Summary
In this post, we explored how to set up and configure ExecutorService
in a Spring Boot application. We created three types of thread pools: fixedThreadPool
, singleThreaded
, and cachedThreadPool
. Each type serves different use cases, from fixed-size thread pools to dynamically resizing ones. By autowiring the ExecutorService
bean, you can easily submit tasks for asynchronous execution. This setup is essential for improving the performance of applications that handle multiple asynchronous tasks.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
- 👨💻 springboot
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!