Skip to content

Commit

Permalink
Merge branch '2.6.x' into 2.7.x
Browse files Browse the repository at this point in the history
Closes gh-31810
  • Loading branch information
wilkinsona committed Jul 20, 2022
2 parents 84efc89 + efdd844 commit c3f3a2a
Showing 1 changed file with 32 additions and 2 deletions.
@@ -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.
Expand All @@ -25,6 +25,10 @@
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;

Expand All @@ -36,9 +40,13 @@
*/
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() {
Expand All @@ -51,14 +59,36 @@ protected ApplicationPid(String pid) {

private String getPid() {
try {
String jvmName = ManagementFactory.getRuntimeMXBean().getName();
String jvmName = resolveJvmName();
return jvmName.split("@")[0];
}
catch (Throwable ex) {
return null;
}
}

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) {
Expand Down

0 comments on commit c3f3a2a

Please sign in to comment.