diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationPid.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationPid.java index ed86416c8a..78d444d1e9 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationPid.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationPid.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 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. @@ -25,6 +25,10 @@ import java.nio.file.Files; import java.nio.file.attribute.PosixFilePermission; import java.util.Set; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.springframework.core.log.LogMessage; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; @@ -36,9 +40,13 @@ import org.springframework.util.ObjectUtils; */ public class ApplicationPid { + private static final Log logger = LogFactory.getLog(ApplicationPid.class); + private static final PosixFilePermission[] WRITE_PERMISSIONS = { PosixFilePermission.OWNER_WRITE, PosixFilePermission.GROUP_WRITE, PosixFilePermission.OTHERS_WRITE }; + private static final long JVM_NAME_RESOLVE_THRESHOLD = 200; + private final String pid; public ApplicationPid() { @@ -51,7 +59,7 @@ public class ApplicationPid { private String getPid() { try { - String jvmName = ManagementFactory.getRuntimeMXBean().getName(); + String jvmName = resolveJvmName(); return jvmName.split("@")[0]; } catch (Throwable ex) { @@ -59,6 +67,28 @@ public class ApplicationPid { } } + private String resolveJvmName() { + long startTime = System.currentTimeMillis(); + String jvmName = ManagementFactory.getRuntimeMXBean().getName(); + long elapsed = System.currentTimeMillis() - startTime; + if (elapsed > JVM_NAME_RESOLVE_THRESHOLD) { + logger.warn(LogMessage.of(() -> { + StringBuilder warning = new StringBuilder(); + warning.append("ManagementFactory.getRuntimeMXBean().getName() took "); + warning.append(elapsed); + warning.append(" milliseconds to respond."); + warning.append(" This may be due to slow host name resolution."); + warning.append(" Please verify your network configuration"); + if (System.getProperty("os.name").toLowerCase().contains("mac")) { + warning.append(" (macOS machines may need to add entries to /etc/hosts)"); + } + warning.append("."); + return warning; + })); + } + return jvmName; + } + @Override public boolean equals(Object obj) { if (obj == this) {