如何记录基于 spring boot (tomcat) 的应用程序中可用/已用线程的数量?

Awa*_*wad 0 java spring-boot

是否有一个 Spring Boot 配置,您可以在其中记录应用程序服务器当前拥有的可用/已使用线程的数量?

Sim*_*lli 5

您可以使用执行器框架。

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

然后,您必须启用 tomcat JXM 指标application.properties并公开指标端点

server.tomcat.mbeanregistry.enabled=true
management.endpoints.web.exposure.include=info,health,metrics
Run Code Online (Sandbox Code Playgroud)

访问 http://localhost:8080/actuator/metrics/,您将看到所有可用的指标。

例如当前线程http://localhost:8080/actuator/metrics/tomcat.threads.current