Easily Visualize and Manage Spring Boot Thread Pools with zimug-monitor
This article introduces an open‑source Spring Boot starter that lets developers integrate, configure, monitor, and visualize thread pools without altering Spring Boot's core implementation, addressing common issues of unmanaged resources and difficult runtime adjustments.
When developing Spring Boot applications, asynchronous tasks and thread pools are frequently used, and developers often define custom thread pools directly in code.
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(config.getCorePoolSize());
executor.setMaxPoolSize(config.getMaxPoolSize());
executor.setQueueCapacity(config.getQueueCapacity());
executor.setThreadNamePrefix("TaskExecutePool-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.initialize();
return executor;
}Using this approach presents several problems:
Developers define thread pools arbitrarily, leading to unplanned resource usage and higher maintenance costs.
When pool size or resources become insufficient, adjustments require code changes and redeployment.
In large teams, it is difficult to track how many thread pools exist and where they are used.
Without tracking, it is hard to know whether resources are wasted or tasks are waiting.
To solve these issues, an open‑source Spring Boot Starter (zimug-monitor-threadpool) was created, enabling visualization, observation, easy configuration, and simple usage of thread pools without modifying Spring Boot's core implementation.
Note: zimug-monitor-threadpool does not change Spring Boot's thread‑pool implementation; it only adds automated configuration loading at initialization and runtime monitoring.
1. Easy Integration and Configuration
Clone the source code, install it to the local Maven repository, and add the dependency:
<dependency>
<groupId>com.zimug</groupId>
<artifactId>zimug-monitor-threadpool</artifactId>
<version>1.0</version>
</dependency>Configure the thread pools in application.yml:
thread-pool:
enable: true
poolLists:
- poolId: test
poolName: Test 1
coreSize: 5
maxSize: 10
queueCapacity: 10
- poolId: test2
poolName: Test 2
coreSize: 5
maxSize: 10
queueCapacity: 10Understanding the configuration:
When active tasks fill the core size, new tasks are placed in the waiting queue (queueCapacity=10).
If the waiting queue is also full, the pool expands up to maxSize.
If both the queue and maxSize are full, tasks are blocked.
2. Easy Usage
Usage mirrors standard Spring Boot custom thread pools. Annotate methods with @Async("test") to execute them in the test pool:
@Component
public class TestTask {
@Async("test") // "test" is the poolId defined above
public Future<String> test() throws Exception {
System.out.println("Current thread: " + Thread.currentThread().getName());
return new AsyncResult<>("Test task");
}
}3. Visual Monitoring
After adding zimug-monitor-threadpool, access /pool.html to obtain static configuration and runtime status, including pool ID, description, core and max sizes, queue capacity, current pool size, active thread count, peak active threads, and remaining queue capacity.
4. Implementation Principle
The starter loads the YAML configuration at startup, creates a ThreadPoolTaskExecutor for each pool, and registers it as a singleton bean using the poolId:
configurableBeanFactory.registerSingleton(pool.getPoolId(), taskExecutor);When runtime monitoring is needed, the executor bean is retrieved to gather statistics:
ThreadPoolTaskExecutor memThreadPool = (ThreadPoolTaskExecutor) applicationContext.getBean(poolModel.getPoolId());Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Sanyou's Java Diary
Passionate about technology, though not great at solving problems; eager to share, never tire of learning!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
