You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by "mxsm (via GitHub)" <gi...@apache.org> on 2023/05/08 15:04:37 UTC

[GitHub] [rocketmq] mxsm commented on a diff in pull request #6672: [ISSUE #6671]Enhance getting PID

mxsm commented on code in PR #6672:
URL: https://github.com/apache/rocketmq/pull/6672#discussion_r1187558061


##########
common/src/main/java/org/apache/rocketmq/common/UtilAll.java:
##########
@@ -69,12 +68,25 @@ public class UtilAll {
         HEX_ARRAY = "0123456789ABCDEF".toCharArray();
         Supplier<Integer> supplier = () -> {
             // format: "pid@hostname"
-            String currentJVM = ManagementFactory.getRuntimeMXBean().getName();
             try {
-                return Integer.parseInt(currentJVM.substring(0, currentJVM.indexOf('@')));
-            } catch (Exception e) {
-                return -1;
+                final Class<?> managementFactoryClass = Class.forName("java.lang.management.ManagementFactory");
+                final Method getRuntimeMXBean = managementFactoryClass.getDeclaredMethod("getRuntimeMXBean");
+                final Class<?> runtimeMXBeanClass = Class.forName("java.lang.management.RuntimeMXBean");
+                final Method getName = runtimeMXBeanClass.getDeclaredMethod("getName");
+
+                final Object runtimeMXBean = getRuntimeMXBean.invoke(null);
+                final String name = (String) getName.invoke(runtimeMXBean);
+
+                return Integer.parseInt(name.split("@")[0]);
+            } catch (final Exception ex) {
+                try {
+                    // try a Linux-specific way
+                    return Integer.parseInt(new File("/proc/self").getCanonicalFile().getName());
+                } catch (final IOException ignoredUseDefault) {

Review Comment:
   Catching Exception might be better, I will modify it and resubmit again



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org