You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by rg...@apache.org on 2019/11/22 20:38:30 UTC

[qpid-broker-j] branch master updated: QPID-8376 : Fix earlier change to work on JDK 11 as well as JDK 8

This is an automated email from the ASF dual-hosted git repository.

rgodfrey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/qpid-broker-j.git


The following commit(s) were added to refs/heads/master by this push:
     new 039e348  QPID-8376 : Fix earlier change to work on JDK 11 as well as JDK 8
039e348 is described below

commit 039e34899c229164a557c8e6debec3e11f5db6bc
Author: Robert Godfrey <rg...@apache.org>
AuthorDate: Fri Nov 22 21:34:31 2019 +0100

    QPID-8376 : Fix earlier change to work on JDK 11 as well as JDK 8
---
 .../qpid/server/model/BrokerAttributeInjector.java | 185 ++++++++++++---------
 1 file changed, 106 insertions(+), 79 deletions(-)

diff --git a/broker-core/src/main/java/org/apache/qpid/server/model/BrokerAttributeInjector.java b/broker-core/src/main/java/org/apache/qpid/server/model/BrokerAttributeInjector.java
index 8208973..dc4fa3a 100644
--- a/broker-core/src/main/java/org/apache/qpid/server/model/BrokerAttributeInjector.java
+++ b/broker-core/src/main/java/org/apache/qpid/server/model/BrokerAttributeInjector.java
@@ -54,14 +54,16 @@ public class BrokerAttributeInjector implements ConfiguredObjectAttributeInjecto
 
     private final Class<?> _hotSpotDiagnosticMXBeanClass;
     private final PlatformManagedObject _hotSpotDiagnosticMXBean;
+    private final Class<?> _operatingSystemMXBeanClass;
+    private final OperatingSystemMXBean _operatingSystemMXBean;
 
     public BrokerAttributeInjector()
     {
+        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
         Class<?> hotSpotDiagnosticMXBeanClass = null;
         PlatformManagedObject hotSpotDiagnosticMXBean = null;
         try
         {
-            ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
             hotSpotDiagnosticMXBeanClass =
                     Class.forName("com.sun.management.HotSpotDiagnosticMXBean", true, systemClassLoader);
             hotSpotDiagnosticMXBean =
@@ -75,6 +77,26 @@ public class BrokerAttributeInjector implements ConfiguredObjectAttributeInjecto
 
         _hotSpotDiagnosticMXBeanClass = hotSpotDiagnosticMXBeanClass;
         _hotSpotDiagnosticMXBean = hotSpotDiagnosticMXBean;
+
+
+        Class<?> operatingSystemMXBeanClass = null;
+        PlatformManagedObject operatingSystemMXBean = null;
+        try
+        {
+            operatingSystemMXBeanClass =
+                    Class.forName("com.sun.management.OperatingSystemMXBean", true, systemClassLoader);
+            operatingSystemMXBean =
+                    ManagementFactory.getPlatformMXBean((Class<? extends PlatformManagedObject>) operatingSystemMXBeanClass);
+
+        }
+        catch (IllegalArgumentException | ClassNotFoundException e)
+        {
+            LOGGER.debug("com.sun.management.OperatingSystemMXBean MXBean: " + e);
+        }
+
+        _operatingSystemMXBeanClass = operatingSystemMXBeanClass;
+        _operatingSystemMXBean = (OperatingSystemMXBean) operatingSystemMXBean;
+
     }
 
     @Override
@@ -165,7 +187,8 @@ public class BrokerAttributeInjector implements ConfiguredObjectAttributeInjecto
                         new ConfiguredObjectInjectedStatistic<>(jvmGCCollectionTimeStatisticName,
                                                                 getGCCollectionTime,
                                                                 new Object[]{garbageCollectorMXBean},
-                                                                "Cumulative time in ms taken to perform collections for GC " + garbageCollectorMXBean.getName(),
+                                                                "Cumulative time in ms taken to perform collections for GC "
+                                                                + garbageCollectorMXBean.getName(),
                                                                 _typeValidator,
                                                                 StatisticUnit.COUNT,
                                                                 StatisticType.CUMULATIVE,
@@ -187,7 +210,8 @@ public class BrokerAttributeInjector implements ConfiguredObjectAttributeInjecto
                         new ConfiguredObjectInjectedStatistic<>(jvmGCCollectionCountStatisticName,
                                                                 getGCCollectionCount,
                                                                 new Object[]{garbageCollectorMXBean},
-                                                                "Cumulative number of collections for GC " + garbageCollectorMXBean.getName(),
+                                                                "Cumulative number of collections for GC "
+                                                                + garbageCollectorMXBean.getName(),
                                                                 _typeValidator,
                                                                 StatisticUnit.COUNT,
                                                                 StatisticType.CUMULATIVE,
@@ -201,94 +225,97 @@ public class BrokerAttributeInjector implements ConfiguredObjectAttributeInjecto
             }
         }
 
-        OperatingSystemMXBean osMXBean = ManagementFactory.getOperatingSystemMXBean();
-        try
+        if (_operatingSystemMXBean != null)
         {
-            Method method = osMXBean.getClass().getDeclaredMethod("getProcessCpuTime");
-            method.setAccessible(true);
-            ToLongFunction<Broker> supplier  = broker -> {
-                try
-                {
-                    final Object returnValue = method.invoke(osMXBean);
+            try
+            {
+                Method method = _operatingSystemMXBeanClass.getDeclaredMethod("getProcessCpuTime");
 
-                    if(returnValue instanceof Number)
+                ToLongFunction<Broker> supplier = broker -> {
+                    try
                     {
-                        return ((Number)returnValue).longValue();
+                        final Object returnValue = method.invoke(_operatingSystemMXBean);
+
+                        if (returnValue instanceof Number)
+                        {
+                            return ((Number) returnValue).longValue();
+                        }
                     }
-                }
-                catch (IllegalAccessException | InvocationTargetException e)
-                {
-                    LOGGER.warn("Unable to get cumulative process CPU time");
-                }
-                return -1L;
-            };
-
-            Method getLongValue = BrokerAttributeInjector.class.getDeclaredMethod("getLongValue",
-                                                                                  Broker.class,
-                                                                                  ToLongFunction.class);
-
-            final ConfiguredObjectInjectedStatistic<?, ?> injectedStatistic =
-                    new ConfiguredObjectInjectedStatistic<>("processCpuTime",
-                                                            getLongValue,
-                                                            new Object[]{supplier},
-                                                            "Cumulative process CPU time",
-                                                            _typeValidator,
-                                                            StatisticUnit.TIME_DURATION,
-                                                            StatisticType.CUMULATIVE,
-                                                            osMXBean.getName()
-                                                            + " Process CPU Time");
-            statistics.add(injectedStatistic);
+                    catch (IllegalAccessException | InvocationTargetException e)
+                    {
+                        LOGGER.warn("Unable to get cumulative process CPU time");
+                    }
+                    return -1L;
+                };
 
-        }
-        catch (NoSuchMethodException e)
-        {
-            LOGGER.warn("Failed to inject statistic 'getProcessCpuTime'", e);
-        }
+                Method getLongValue = BrokerAttributeInjector.class.getDeclaredMethod("getLongValue",
+                                                                                      Broker.class,
+                                                                                      ToLongFunction.class);
 
-        try
-        {
-            Method method = osMXBean.getClass().getDeclaredMethod("getProcessCpuLoad");
-            method.setAccessible(true);
-            Function<Broker, BigDecimal> supplier  = broker -> {
-                try
-                {
-                    final Object returnValue = method.invoke(osMXBean);
+                final ConfiguredObjectInjectedStatistic<?, ?> injectedStatistic =
+                        new ConfiguredObjectInjectedStatistic<>("processCpuTime",
+                                                                getLongValue,
+                                                                new Object[]{supplier},
+                                                                "Cumulative process CPU time",
+                                                                _typeValidator,
+                                                                StatisticUnit.TIME_DURATION,
+                                                                StatisticType.CUMULATIVE,
+                                                                _operatingSystemMXBeanClass.getName()
+                                                                + " Process CPU Time");
+                statistics.add(injectedStatistic);
+
+            }
+            catch (NoSuchMethodException | SecurityException e)
+            {
+                LOGGER.warn("Failed to inject statistic 'getProcessCpuTime'");
+                LOGGER.debug("Exception:",e);
+            }
 
-                    if(returnValue instanceof Number)
+            try
+            {
+                Method method = _operatingSystemMXBeanClass.getDeclaredMethod("getProcessCpuLoad");
+                method.setAccessible(true);
+                Function<Broker, BigDecimal> supplier = broker -> {
+                    try
                     {
-                        return BigDecimal.valueOf(((Number) returnValue).doubleValue()).setScale(4,
-                                                                                                 RoundingMode.HALF_UP);
+                        final Object returnValue = method.invoke(_operatingSystemMXBean);
+
+                        if (returnValue instanceof Number)
+                        {
+                            return BigDecimal.valueOf(((Number) returnValue).doubleValue()).setScale(4,
+                                                                                                     RoundingMode.HALF_UP);
+                        }
                     }
-                }
-                catch (IllegalAccessException | InvocationTargetException e)
-                {
-                    LOGGER.warn("Unable to get current process CPU load");
-                }
-                return BigDecimal.valueOf(Double.NaN);
-            };
+                    catch (IllegalAccessException | InvocationTargetException e)
+                    {
+                        LOGGER.warn("Unable to get current process CPU load", e);
+                    }
+                    return BigDecimal.valueOf(-1L);
+                };
 
-            Method getBigDecimalValue = BrokerAttributeInjector.class.getDeclaredMethod("getBigDecimalValue",
-                                                                                      Broker.class,
-                                                                                      Function.class);
-
-            final ConfiguredObjectInjectedStatistic<?, ?> injectedStatistic =
-                    new ConfiguredObjectInjectedStatistic<>("processCpuLoad",
-                                                            getBigDecimalValue,
-                                                            new Object[]{supplier},
-                                                            "Current process CPU load",
-                                                            _typeValidator,
-                                                            StatisticUnit.COUNT,
-                                                            StatisticType.POINT_IN_TIME,
-                                                            osMXBean.getName()
-                                                            + " Process CPU Load");
-            statistics.add(injectedStatistic);
+                Method getBigDecimalValue = BrokerAttributeInjector.class.getDeclaredMethod("getBigDecimalValue",
+                                                                                            Broker.class,
+                                                                                            Function.class);
 
-        }
-        catch (NoSuchMethodException e)
-        {
-            LOGGER.warn("Failed to inject statistic 'getProcessCpuLoad'", e);
-        }
+                final ConfiguredObjectInjectedStatistic<?, ?> injectedStatistic =
+                        new ConfiguredObjectInjectedStatistic<>("processCpuLoad",
+                                                                getBigDecimalValue,
+                                                                new Object[]{supplier},
+                                                                "Current process CPU load",
+                                                                _typeValidator,
+                                                                StatisticUnit.COUNT,
+                                                                StatisticType.POINT_IN_TIME,
+                                                                _operatingSystemMXBean.getName()
+                                                                + " Process CPU Load");
+                statistics.add(injectedStatistic);
 
+            }
+            catch (NoSuchMethodException e)
+            {
+                LOGGER.warn("Failed to inject statistic 'getProcessCpuLoad'");
+                LOGGER.debug("Exception:",e);
+            }
+        }
         return statistics;
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org