DynamicTp: Real‑time Monitoring and Dynamic Scaling for SpringBoot Thread Pools
This article introduces DynamicTp, a zero‑intrusion SpringBoot starter that provides real‑time monitoring, dynamic adjustment, and alerting of ThreadPoolExecutor parameters via popular configuration centers, supporting multiple middleware thread pools, various metrics exporters, and extensible SPI interfaces for enterprise‑grade thread‑pool management.
Background
ThreadPoolExecutor offers set/get methods for core parameters, but developers often face uncertainty about optimal values, lack of runtime visibility, and the need to restart services after configuration changes. By leveraging high‑availability configuration centers, dynamic adjustment of thread‑pool parameters becomes feasible and reliable.
Introduction
DynamicTp extends ThreadPoolExecutor with the following goals:
Enable dynamic modification of thread‑pool parameters at runtime with immediate effect.
Provide real‑time monitoring and trigger configurable alert strategies, pushing notifications to office platforms.
Collect thread‑pool metrics periodically for visualization tools such as Grafana.
Key Features (v1.0.9)
Zero‑intrusion : All configurations are managed via the configuration center without modifying business code.
Lightweight : Implemented as a SpringBoot starter; integration requires only four simple steps.
Highly extensible : Core functions expose SPI interfaces for custom implementations (configuration parsing, notification, monitoring, task wrapping, etc.).
Proven at scale : Inspired by Meituan’s thread‑pool practices and used in large‑scale production.
Multi‑platform alerts : Supports DingTalk, WeChat Work, Feishu, email, and custom extensions via SPI.
Monitoring : Metrics can be exported via
MicroMeter,
JsonLog, or custom
Endpointimplementations.
Task enhancement : Provides
TaskWrapperimplementations such as
MdcTaskWrapper,
TtlTaskWrapper, and
SwTraceTaskWrapperfor context propagation.
Compatibility : Works with JUC thread pools and Spring’s
ThreadPoolTaskExecutorusing the
@DynamicTpannotation.
Reliability : Implements Spring lifecycle hooks to gracefully process queued tasks before container shutdown.
Multi‑mode support : Includes an
EagerDtpExecutorfor IO‑intensive scenarios.
Broad configuration‑center support : Compatible with Nacos, Apollo, Zookeeper, Consul, Etcd, and others.
Middleware thread‑pool management : Integrated management for Tomcat, Jetty, Undertow, Dubbo, RocketMQ, Hystrix, gRPC, etc.
Architecture Design
The framework consists of the following modules:
Configuration change listener
Internal service thread‑pool management
Third‑party component thread‑pool management
Monitoring
Notification and alerting
Code Structure
Configuration Change Listener Module
1. Listens to configuration files in supported centers (Nacos, Apollo, Zookeeper, Consul, Etcd) via SPI extensions.
2. Parses YAML, properties, and JSON files, with SPI hooks for additional formats.
3. Notifies the thread‑pool management module to refresh parameters.
Service Internal Thread‑Pool Management Module
1. On service startup, pulls configurations from the center, creates thread‑pool instances, and registers them in an internal registry and the Spring context.
2. Responds to refresh events from the listener to update pool parameters.
3. Threads can be obtained via dependency injection or
DtpRegistry.getDtpExecutor()using the pool name.
Third‑Party Component Thread‑Pool Management
Manages thread pools of common middleware (Tomcat, Jetty, Undertow, Dubbo, RocketMQ, Hystrix, gRPC) and handles parameter refresh, metric collection, and alerting.
Monitoring Module
Provides three default metric exporters:
JsonLog: Writes metrics to disk for custom processing.
MicroMeter: Exposes metrics for Grafana dashboards.
Endpoint(e.g.,
dynamic-tp): HTTP endpoint for real‑time access.
Notification & Alert Module
Integrates with office platforms to send alerts for parameter changes, queue capacity thresholds, pool activity thresholds, rejection events, task execution timeouts, and task waiting timeouts. Supports DingTalk, WeChat Work, Feishu, email, and custom SPI extensions.
Project Links
Gitee: https://gitee.com/dromara/dynamic-tp
GitHub: https://github.com/dromara/dynamic-tp
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.