Skip to content

Commit 821af70

Browse files
committed
Polish "Add info contributor support for JDK 24's VirtualThreadSchedulerMXBean"
See gh-43594
1 parent 104397f commit 821af70

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/info/ProcessInfo.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.lang.management.ManagementFactory;
2020
import java.lang.management.MemoryMXBean;
2121
import java.lang.management.MemoryUsage;
22+
import java.lang.management.PlatformManagedObject;
2223

2324
/**
2425
* Information about the process of the application.
@@ -75,23 +76,25 @@ public MemoryInfo getMemory() {
7576

7677
/**
7778
* Virtual threads information for the process. These values provide details about the
78-
* current state of virtual threads, including the number of mounted threads, queued threads,
79-
* the parallelism level, and the thread pool size.
80-
* @return an instance of {@link VirtualThreadsInfo} containing information about virtual threads,
81-
* or {@code null} if the VirtualThreadSchedulerMXBean is not available.
79+
* current state of virtual threads, including the number of mounted threads, queued
80+
* threads, the parallelism level, and the thread pool size.
81+
* @return an instance of {@link VirtualThreadsInfo} containing information about
82+
* virtual threads, or {@code null} if the VirtualThreadSchedulerMXBean is not
83+
* available.
8284
* @since 3.5.0
8385
*/
86+
@SuppressWarnings("unchecked")
8487
public VirtualThreadsInfo getVirtualThreads() {
8588
try {
86-
Class mxBeanClass = Class.forName("jdk.management.VirtualThreadSchedulerMXBean");
89+
Class<PlatformManagedObject> mxBeanClass = (Class<PlatformManagedObject>) Class
90+
.forName("jdk.management.VirtualThreadSchedulerMXBean");
8791
Object bean = ManagementFactory.getPlatformMXBean(mxBeanClass);
88-
return new VirtualThreadsInfo(
89-
(Integer) mxBeanClass.getMethod("getMountedVirtualThreadCount").invoke(bean),
92+
return new VirtualThreadsInfo((Integer) mxBeanClass.getMethod("getMountedVirtualThreadCount").invoke(bean),
9093
(Long) mxBeanClass.getMethod("getQueuedVirtualThreadCount").invoke(bean),
9194
(Integer) mxBeanClass.getMethod("getParallelism").invoke(bean),
92-
(Integer) mxBeanClass.getMethod("getPoolSize").invoke(bean)
93-
);
94-
} catch (ReflectiveOperationException e) {
95+
(Integer) mxBeanClass.getMethod("getPoolSize").invoke(bean));
96+
}
97+
catch (ReflectiveOperationException ex) {
9598
return null;
9699
}
97100
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/info/ProcessInfoTests.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,10 +17,11 @@
1717
package org.springframework.boot.info;
1818

1919
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.condition.EnabledForJreRange;
21+
import org.junit.jupiter.api.condition.JRE;
2022

2123
import org.springframework.boot.info.ProcessInfo.MemoryInfo.MemoryUsageInfo;
2224
import org.springframework.boot.info.ProcessInfo.VirtualThreadsInfo;
23-
import org.springframework.util.ClassUtils;
2425

2526
import static org.assertj.core.api.Assertions.assertThat;
2627

@@ -58,22 +59,22 @@ void memoryInfoIsAvailable() {
5859
}
5960

6061
@Test
61-
void virtualThreadsInfoIsNullWhenMXBeanIsNotAccessible() {
62-
if (ClassUtils.isPresent("jdk.management.VirtualThreadSchedulerMXBean", null)) {
63-
ProcessInfo processInfo = new ProcessInfo();
64-
65-
VirtualThreadsInfo virtualThreadsInfo = processInfo.getVirtualThreads();
66-
67-
assertThat(virtualThreadsInfo).isNotNull();
68-
assertThat(virtualThreadsInfo.getMounted()).isGreaterThanOrEqualTo(0);
69-
assertThat(virtualThreadsInfo.getQueued()).isGreaterThanOrEqualTo(0);
70-
assertThat(virtualThreadsInfo.getParallelism()).isGreaterThan(0);
71-
assertThat(virtualThreadsInfo.getPoolSize()).isGreaterThan(0);
72-
} else {
73-
ProcessInfo processInfo = new ProcessInfo();
62+
@EnabledForJreRange(min = JRE.JAVA_24)
63+
void virtualThreadsInfoIfAvailable() {
64+
ProcessInfo processInfo = new ProcessInfo();
65+
VirtualThreadsInfo virtualThreadsInfo = processInfo.getVirtualThreads();
66+
assertThat(virtualThreadsInfo).isNotNull();
67+
assertThat(virtualThreadsInfo.getMounted()).isGreaterThanOrEqualTo(0);
68+
assertThat(virtualThreadsInfo.getQueued()).isGreaterThanOrEqualTo(0);
69+
assertThat(virtualThreadsInfo.getParallelism()).isGreaterThan(0);
70+
assertThat(virtualThreadsInfo.getPoolSize()).isGreaterThanOrEqualTo(0);
71+
}
7472

75-
assertThat(processInfo.getVirtualThreads()).isNull();
76-
}
73+
@Test
74+
@EnabledForJreRange(max = JRE.JAVA_23)
75+
void virtualThreadsInfoIfNotAvailable() {
76+
ProcessInfo processInfo = new ProcessInfo();
77+
assertThat(processInfo.getVirtualThreads()).isNull();
7778
}
7879

7980
}

0 commit comments

Comments
 (0)