Skip to content

Commit efdd844

Browse files
committed
Log a warning if determining the app's PID takes too long
Closes gh-31572
1 parent bcbe072 commit efdd844

File tree

1 file changed

+32
-2
lines changed
  • spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system

1 file changed

+32
-2
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationPid.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -25,6 +25,10 @@
2525
import java.nio.file.attribute.PosixFilePermission;
2626
import java.util.Set;
2727

28+
import org.apache.commons.logging.Log;
29+
import org.apache.commons.logging.LogFactory;
30+
31+
import org.springframework.core.log.LogMessage;
2832
import org.springframework.util.Assert;
2933
import org.springframework.util.ObjectUtils;
3034

@@ -36,9 +40,13 @@
3640
*/
3741
public class ApplicationPid {
3842

43+
private static final Log logger = LogFactory.getLog(ApplicationPid.class);
44+
3945
private static final PosixFilePermission[] WRITE_PERMISSIONS = { PosixFilePermission.OWNER_WRITE,
4046
PosixFilePermission.GROUP_WRITE, PosixFilePermission.OTHERS_WRITE };
4147

48+
private static final long JVM_NAME_RESOLVE_THRESHOLD = 200;
49+
4250
private final String pid;
4351

4452
public ApplicationPid() {
@@ -51,14 +59,36 @@ protected ApplicationPid(String pid) {
5159

5260
private String getPid() {
5361
try {
54-
String jvmName = ManagementFactory.getRuntimeMXBean().getName();
62+
String jvmName = resolveJvmName();
5563
return jvmName.split("@")[0];
5664
}
5765
catch (Throwable ex) {
5866
return null;
5967
}
6068
}
6169

70+
private String resolveJvmName() {
71+
long startTime = System.currentTimeMillis();
72+
String jvmName = ManagementFactory.getRuntimeMXBean().getName();
73+
long elapsed = System.currentTimeMillis() - startTime;
74+
if (elapsed > JVM_NAME_RESOLVE_THRESHOLD) {
75+
logger.warn(LogMessage.of(() -> {
76+
StringBuilder warning = new StringBuilder();
77+
warning.append("ManagementFactory.getRuntimeMXBean().getName() took ");
78+
warning.append(elapsed);
79+
warning.append(" milliseconds to respond.");
80+
warning.append(" This may be due to slow host name resolution.");
81+
warning.append(" Please verify your network configuration");
82+
if (System.getProperty("os.name").toLowerCase().contains("mac")) {
83+
warning.append(" (macOS machines may need to add entries to /etc/hosts)");
84+
}
85+
warning.append(".");
86+
return warning;
87+
}));
88+
}
89+
return jvmName;
90+
}
91+
6292
@Override
6393
public boolean equals(Object obj) {
6494
if (obj == this) {

0 commit comments

Comments
 (0)