1
1
/*
2
- * Copyright 2012-2020 the original author or authors.
2
+ * Copyright 2012-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
25
25
import java .nio .file .attribute .PosixFilePermission ;
26
26
import java .util .Set ;
27
27
28
+ import org .apache .commons .logging .Log ;
29
+ import org .apache .commons .logging .LogFactory ;
30
+
31
+ import org .springframework .core .log .LogMessage ;
28
32
import org .springframework .util .Assert ;
29
33
import org .springframework .util .ObjectUtils ;
30
34
36
40
*/
37
41
public class ApplicationPid {
38
42
43
+ private static final Log logger = LogFactory .getLog (ApplicationPid .class );
44
+
39
45
private static final PosixFilePermission [] WRITE_PERMISSIONS = { PosixFilePermission .OWNER_WRITE ,
40
46
PosixFilePermission .GROUP_WRITE , PosixFilePermission .OTHERS_WRITE };
41
47
48
+ private static final long JVM_NAME_RESOLVE_THRESHOLD = 200 ;
49
+
42
50
private final String pid ;
43
51
44
52
public ApplicationPid () {
@@ -51,14 +59,36 @@ protected ApplicationPid(String pid) {
51
59
52
60
private String getPid () {
53
61
try {
54
- String jvmName = ManagementFactory . getRuntimeMXBean (). getName ();
62
+ String jvmName = resolveJvmName ();
55
63
return jvmName .split ("@" )[0 ];
56
64
}
57
65
catch (Throwable ex ) {
58
66
return null ;
59
67
}
60
68
}
61
69
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
+
62
92
@ Override
63
93
public boolean equals (Object obj ) {
64
94
if (obj == this ) {
0 commit comments