Setup ExecutorService within springboot application

Introduction

This post would demo how to setup the ExecutorService in the springboot 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

@Configuration
public 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 @Configuration annotation to config the spring beans of ExecutorService, we setup three types of beans:

  • fixedThreadPool: A fixed sized thread pool based ExecutorService to run threads with fixed size, the size is 5.

  • singleThreaded: A single threaded thread pool based ExecutorService, it run all the task with only one thread

  • cachedThreadPool: A cached thread pool based ExecutorService, which run task with cached threads, that is, it will create thread only needed, detail is as follows:

Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.

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 so easy, do you think so?

You can find detail documents about the springboot and unit testing here: