|
17 | 17 | import com.rabbitmq.stream.OffsetSpecification;
|
18 | 18 | import com.rabbitmq.stream.StreamCreator.LeaderLocator;
|
19 | 19 | import com.rabbitmq.stream.compression.Compression;
|
| 20 | +import com.sun.management.OperatingSystemMXBean; |
| 21 | +import java.lang.reflect.Method; |
20 | 22 | import java.security.cert.X509Certificate;
|
21 | 23 | import java.text.CharacterIterator;
|
22 | 24 | import java.text.StringCharacterIterator;
|
|
36 | 38 | import java.util.concurrent.ThreadFactory;
|
37 | 39 | import java.util.concurrent.atomic.AtomicLong;
|
38 | 40 | import java.util.function.BiFunction;
|
| 41 | +import java.util.function.LongSupplier; |
39 | 42 | import java.util.stream.Collectors;
|
40 | 43 | import java.util.stream.IntStream;
|
41 | 44 | import javax.net.ssl.SNIHostName;
|
|
49 | 52 | class Utils {
|
50 | 53 |
|
51 | 54 | static final X509TrustManager TRUST_EVERYTHING_TRUST_MANAGER = new TrustEverythingTrustManager();
|
| 55 | + private static final LongSupplier TOTAL_MEMORY_SIZE_SUPPLIER; |
52 | 56 | private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
|
53 | 57 | private static final String RANGE_SEPARATOR_1 = "-";
|
54 | 58 | private static final String RANGE_SEPARATOR_2 = "..";
|
55 | 59 |
|
| 60 | + // this trick avoids a deprecation warning when compiling on Java 14+ |
| 61 | + static { |
| 62 | + Method method; |
| 63 | + try { |
| 64 | + // Java 14+ |
| 65 | + method = OperatingSystemMXBean.class.getDeclaredMethod("getTotalMemorySize"); |
| 66 | + } catch (NoSuchMethodException nsme) { |
| 67 | + try { |
| 68 | + method = OperatingSystemMXBean.class.getDeclaredMethod("getTotalPhysicalMemorySize"); |
| 69 | + } catch (Exception e) { |
| 70 | + throw new RuntimeException("Error while computing method to get total memory size"); |
| 71 | + } |
| 72 | + } |
| 73 | + Method m = method; |
| 74 | + TOTAL_MEMORY_SIZE_SUPPLIER = |
| 75 | + () -> { |
| 76 | + OperatingSystemMXBean os = |
| 77 | + (OperatingSystemMXBean) |
| 78 | + java.lang.management.ManagementFactory.getOperatingSystemMXBean(); |
| 79 | + try { |
| 80 | + return (long) m.invoke(os); |
| 81 | + } catch (Exception e) { |
| 82 | + throw new RuntimeException("Could not retrieve total memory size", e); |
| 83 | + } |
| 84 | + }; |
| 85 | + } |
| 86 | + |
56 | 87 | static void writeLong(byte[] array, long value) {
|
57 | 88 | // from Guava Longs
|
58 | 89 | for (int i = 7; i >= 0; i--) {
|
@@ -123,10 +154,7 @@ static String formatByte(double bytes) {
|
123 | 154 |
|
124 | 155 | static long physicalMemory() {
|
125 | 156 | try {
|
126 |
| - com.sun.management.OperatingSystemMXBean os = |
127 |
| - (com.sun.management.OperatingSystemMXBean) |
128 |
| - java.lang.management.ManagementFactory.getOperatingSystemMXBean(); |
129 |
| - return os.getTotalPhysicalMemorySize(); |
| 157 | + return TOTAL_MEMORY_SIZE_SUPPLIER.getAsLong(); |
130 | 158 | } catch (Throwable e) {
|
131 | 159 | // we can get NoClassDefFoundError, so we catch from Throwable and below
|
132 | 160 | LOGGER.warn("Could not get physical memory", e);
|
|
0 commit comments