Backend Development 13 min read

How to Quickly Add a Lightweight Dynamic Thread Pool with Hippo4j in Spring Boot

This article explains how Hippo4j 1.1.0 introduces a configuration‑center‑driven lightweight dynamic thread pool for Spring Boot applications, covering core features, alarm strategies, web‑container updates, code examples, and migration tips.

macrozheng
macrozheng
macrozheng
How to Quickly Add a Lightweight Dynamic Thread Pool with Hippo4j in Spring Boot

1. Introduction

Hippo4j was created to improve and guarantee thread‑pool behavior for online applications, which leads to a strong dependency on Hippo4j Server.

Since version 1.0.0 users have asked how to use a lightweight dynamic thread pool.

GitHub: https://github.com/acmenlt/dynamic-threadpool Gitee: https://gitee.com/acmenlt/dynamic-threadpool

Version 1.1.0 adds a new usage mode that relies on a configuration center, splitting the source code into two modes while sharing the core module “Hippo4j Core”.

2. Hippo4j Core

The core module provides the following capabilities:

Include

hippo4j-core-spring-boot-starter

in a project that already uses a configuration center to enable all features.

1. Dynamic thread‑pool parameter updates

When the client starts, it requests configuration from the config center, creates a

DynamicThreadPool

, and listens for changes to adjust parameters in real time.

Example log when parameters change:

<code>[MESSAGE-CONSUME] Changed thread pool.
    coreSize :: [1 => 10]
    maxSize :: [1 => 20]
    queueType :: [ResizableCapacityLinkedBlockIngQueue => ResizableCapacityLinkedBlockIngQueue]
    capacity :: [1024 => 2048]
    keepAliveTime :: [1000 => 1000]
    executeTimeOut :: [600 => 600]
    rejectedType :: [DiscardOldestPolicy => DiscardOldestPolicy]
    allowCoreThreadTimeOut :: [false => false]
</code>

Notifications can be sent to DingTalk, WeChat Work, or Lark; an example of a WeChat Work robot is shown below.

2. Web thread‑pool parameter updates

Hippo4j Core supports updating core parameters of Tomcat, Jetty, and Undertow containers:

corePoolSize

,

maximumPoolSize

,

keepAliveTime

.

Reasons for dynamic Web pool updates:

During load testing, adjusting the container thread count without redeploying saves time.

If a SpringBoot application becomes slow while the server load is low, increasing the Web pool can improve response time.

Use this feature cautiously in production.

3. Dynamic thread‑pool alarm strategies

Hippo4j provides four customizable alarm types: active‑level, queue‑capacity, reject‑task, and execution‑time. Alarms are throttled so that only one notification per pool and alarm type is sent within the configured interval.

Example: a pool with ID “message‑consume” and a 5‑minute alarm interval will send at most one alert of each type within that period.

Supported notification platforms include DingTalk, WeChat Work, and Lark; an example of a WeChat Work robot configuration is shown.

3. Code Example

Add the Maven dependency:

<code><dependency>
    <groupId>cn.hippo4j</groupId>
    <artifactId>hippo4j-core-spring-boot-starter</artifactId>
    <version>1.1.0</version>
</dependency>
</code>

Enable the pool with

@EnableDynamicThreadPool

on the SpringBoot application class.

<code>@SpringBootApplication
@EnableDynamicThreadPool
public class ExampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(ExampleApplication.class, args);
    }
}
</code>

Configure the pool in the configuration center (YAML example):

<code>spring:
  dynamic:
    thread-pool:
      enable: true
      banner: true
      collect: true
      check-state-interval: 3
      notify-platforms:
        - platform: 'WECHAT'
          secret-key: 1d307bfa-...
        - platform: 'DING'
          secret-key: 56417e...
        - platform: 'LARK'
          secret-key: 2cbf28...
      nacos:
        data-id: xxx
        group: xxx
      apollo:
        namespace: xxxx
      config-file-type: yml
      executors:
        - thread-pool-id: 'message-consume'
          core-pool-size: 1
          maximum-pool-size: 1
          queue-capacity: 1
          execute-time-out: 1000
          blocking-queue: 'LinkedBlockingQueue'
          rejected-handler: 'AbortPolicy'
          keep-alive-time: 1024
          allow-core-thread-time-out: true
          thread-name-prefix: 'message-consume'
          notify:
            is-alarm: true
            active-alarm: 80
            capacity-alarm: 80
            interval: 8
            receives:
              DING: 'xxx'
              WECHAT: 'xxx'
              LARK: 'xxx'
</code>

Build a dynamic pool programmatically:

<code>import cn.hippo4j.core.executor.DynamicThreadPool;
import cn.hippo4j.core.executor.support.ThreadPoolBuilder;

@Bean
@DynamicThreadPool
public ThreadPoolExecutor dynamicThreadPoolExecutor() {
    String poolId = "message-consume";
    return ThreadPoolBuilder.builder()
            .threadFactory(poolId)
            .dynamicPool()
            .build();
}
</code>

Inject and use the pool as a Spring bean.

<code>@Resource
private ThreadPoolExecutor dynamicThreadPoolExecutor;

dynamicThreadPoolExecutor.execute(() -> xxx);
</code>

Shutdown handling can be configured with

waitForTasksToCompleteOnShutdown

and

awaitTerminationMillis

to ensure graceful termination.

<code>return ThreadPoolBuilder.builder()
        .threadFactory(poolId)
        .waitForTasksToCompleteOnShutdown(true)
        .awaitTerminationMillis(5000L)
        .dynamicPool()
        .build();
</code>

A

TaskDecorator

can be supplied to propagate context information to tasks.

<code>return ThreadPoolBuilder.builder()
        .threadFactory(poolId)
        .taskDecorator(new TaskDecoratorTest.ContextCopyingDecorator())
        .dynamicPool()
        .build();
</code>

Switching from Hippo4j Core to Hippo4j Server only requires changing the Maven coordinates and moving configuration to the Hippo4j console.

4. Frequently Asked Questions

1. How to ensure all tasks finish when the application shuts down?

Set

waitForTasksToCompleteOnShutdown

to true and define

awaitTerminationMillis

for the maximum wait time.

2. Can the dynamic pool carry context parameters?

Yes, by implementing

TaskDecorator

and providing it during pool construction.

3. Is migrating from Core to Server difficult?

No code changes are required; only the Maven dependency and moving configuration to the server console.

5. Conclusion

The article introduces a new usage mode of Hippo4j that relies on a configuration center to provide a lightweight dynamic thread pool. Users can choose between Hippo4j Core (lighter) and Hippo4j Server (more features) according to their needs.

JavaSpring BootConfiguration Centerdynamic thread poolHippo4j
macrozheng
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.