Polish "Add info contributor support for JDK 24's VirtualThreadSchedulerMXBean"
See gh-43594
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.boot.info;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.MemoryMXBean;
|
||||
import java.lang.management.MemoryUsage;
|
||||
import java.lang.management.PlatformManagedObject;
|
||||
|
||||
/**
|
||||
* Information about the process of the application.
|
||||
@@ -75,23 +76,25 @@ public class ProcessInfo {
|
||||
|
||||
/**
|
||||
* Virtual threads information for the process. These values provide details about the
|
||||
* current state of virtual threads, including the number of mounted threads, queued threads,
|
||||
* the parallelism level, and the thread pool size.
|
||||
* @return an instance of {@link VirtualThreadsInfo} containing information about virtual threads,
|
||||
* or {@code null} if the VirtualThreadSchedulerMXBean is not available.
|
||||
* current state of virtual threads, including the number of mounted threads, queued
|
||||
* threads, the parallelism level, and the thread pool size.
|
||||
* @return an instance of {@link VirtualThreadsInfo} containing information about
|
||||
* virtual threads, or {@code null} if the VirtualThreadSchedulerMXBean is not
|
||||
* available.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public VirtualThreadsInfo getVirtualThreads() {
|
||||
try {
|
||||
Class mxBeanClass = Class.forName("jdk.management.VirtualThreadSchedulerMXBean");
|
||||
Class<PlatformManagedObject> mxBeanClass = (Class<PlatformManagedObject>) Class
|
||||
.forName("jdk.management.VirtualThreadSchedulerMXBean");
|
||||
Object bean = ManagementFactory.getPlatformMXBean(mxBeanClass);
|
||||
return new VirtualThreadsInfo(
|
||||
(Integer) mxBeanClass.getMethod("getMountedVirtualThreadCount").invoke(bean),
|
||||
return new VirtualThreadsInfo((Integer) mxBeanClass.getMethod("getMountedVirtualThreadCount").invoke(bean),
|
||||
(Long) mxBeanClass.getMethod("getQueuedVirtualThreadCount").invoke(bean),
|
||||
(Integer) mxBeanClass.getMethod("getParallelism").invoke(bean),
|
||||
(Integer) mxBeanClass.getMethod("getPoolSize").invoke(bean)
|
||||
);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
(Integer) mxBeanClass.getMethod("getPoolSize").invoke(bean));
|
||||
}
|
||||
catch (ReflectiveOperationException ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,10 +17,11 @@
|
||||
package org.springframework.boot.info;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledForJreRange;
|
||||
import org.junit.jupiter.api.condition.JRE;
|
||||
|
||||
import org.springframework.boot.info.ProcessInfo.MemoryInfo.MemoryUsageInfo;
|
||||
import org.springframework.boot.info.ProcessInfo.VirtualThreadsInfo;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -58,22 +59,22 @@ class ProcessInfoTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void virtualThreadsInfoIsNullWhenMXBeanIsNotAccessible() {
|
||||
if (ClassUtils.isPresent("jdk.management.VirtualThreadSchedulerMXBean", null)) {
|
||||
ProcessInfo processInfo = new ProcessInfo();
|
||||
@EnabledForJreRange(min = JRE.JAVA_24)
|
||||
void virtualThreadsInfoIfAvailable() {
|
||||
ProcessInfo processInfo = new ProcessInfo();
|
||||
VirtualThreadsInfo virtualThreadsInfo = processInfo.getVirtualThreads();
|
||||
assertThat(virtualThreadsInfo).isNotNull();
|
||||
assertThat(virtualThreadsInfo.getMounted()).isGreaterThanOrEqualTo(0);
|
||||
assertThat(virtualThreadsInfo.getQueued()).isGreaterThanOrEqualTo(0);
|
||||
assertThat(virtualThreadsInfo.getParallelism()).isGreaterThan(0);
|
||||
assertThat(virtualThreadsInfo.getPoolSize()).isGreaterThanOrEqualTo(0);
|
||||
}
|
||||
|
||||
VirtualThreadsInfo virtualThreadsInfo = processInfo.getVirtualThreads();
|
||||
|
||||
assertThat(virtualThreadsInfo).isNotNull();
|
||||
assertThat(virtualThreadsInfo.getMounted()).isGreaterThanOrEqualTo(0);
|
||||
assertThat(virtualThreadsInfo.getQueued()).isGreaterThanOrEqualTo(0);
|
||||
assertThat(virtualThreadsInfo.getParallelism()).isGreaterThan(0);
|
||||
assertThat(virtualThreadsInfo.getPoolSize()).isGreaterThan(0);
|
||||
} else {
|
||||
ProcessInfo processInfo = new ProcessInfo();
|
||||
|
||||
assertThat(processInfo.getVirtualThreads()).isNull();
|
||||
}
|
||||
@Test
|
||||
@EnabledForJreRange(max = JRE.JAVA_23)
|
||||
void virtualThreadsInfoIfNotAvailable() {
|
||||
ProcessInfo processInfo = new ProcessInfo();
|
||||
assertThat(processInfo.getVirtualThreads()).isNull();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user