获取System Properties的方法

我们可以通过如下两种方式获取System Properties信息: 

1. System.getProperties() 
2. ManagementFactory.getRuntimeMXBean().getSystemProperties()
 

ManagementFactory.getRuntimeMXBean().getSystemProperties方法采用System.getProperties()来完成的,如果在本地运行程序去获取某些系统属性的值,结果是一样的。 

它们的一个区别, RuntimeMXBean能够去获取远程 VM上的System Properties 。 

ManagementFactory的newPlatformMXBeanProxy方法提供了访问远程VM的方法。可采取如下方式来访问远程VM,参考 https://blogs.oracle.com/jmxetc/entry/how_to_retrieve_remote_jvm  

      
Java代码   收藏代码
  1. // Connect to target   
  2.         final JMXConnector connector = JMXConnectorFactory.connect(target);  
  3.           
  4.         // Get an MBeanServerConnection on the remote VM.  
  5.         final MBeanServerConnection remote =   
  6.                 connector.getMBeanServerConnection();  
  7.           
  8.         final RuntimeMXBean remoteRuntime =   
  9.                 ManagementFactory.newPlatformMXBeanProxy(  
  10.                     remote,  
  11.                     ManagementFactory.RUNTIME_MXBEAN_NAME,  
  12.                     RuntimeMXBean.class);  




RuntimeMXBean的具体内容如下: 

Java代码   收藏代码
  1. /* 
  2.  * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved. 
  3.  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 
  4.  * 
  5.  * This code is free software; you can redistribute it and/or modify it 
  6.  * under the terms of the GNU General Public License version 2 only, as 
  7.  * published by the Free Software Foundation.  Sun designates this 
  8.  * particular file as subject to the "Classpath" exception as provided 
  9.  * by Sun in the LICENSE file that accompanied this code. 
  10.  * 
  11.  * This code is distributed in the hope that it will be useful, but WITHOUT 
  12.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  13.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
  14.  * version 2 for more details (a copy is included in the LICENSE file that 
  15.  * accompanied this code). 
  16.  * 
  17.  * You should have received a copy of the GNU General Public License version 
  18.  * 2 along with this work; if not, write to the Free Software Foundation, 
  19.  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 
  20.  * 
  21.  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 
  22.  * CA 95054 USA or visit www.sun.com if you need additional information or 
  23.  * have any questions. 
  24.  */  
  25.   
  26. package sun.management;  
  27.   
  28. import java.lang.management.RuntimeMXBean;  
  29.   
  30. import java.util.List;  
  31. import java.util.HashMap;  
  32. import java.util.Map;  
  33. import java.util.Set;  
  34. import java.util.Properties;  
  35. import javax.management.openmbean.CompositeData;  
  36. import javax.management.openmbean.CompositeDataSupport;  
  37. import javax.management.openmbean.CompositeType;  
  38. import javax.management.openmbean.OpenType;  
  39. import javax.management.openmbean.SimpleType;  
  40. import javax.management.openmbean.OpenDataException;  
  41.   
  42. /** 
  43.  * Implementation class for the runtime subsystem. 
  44.  * Standard and committed hotspot-specific metrics if any. 
  45.  * 
  46.  * ManagementFactory.getRuntimeMXBean() returns an instance 
  47.  * of this class. 
  48.  */  
  49. class RuntimeImpl implements RuntimeMXBean {  
  50.   
  51.     private final VMManagement jvm;  
  52.     private final long vmStartupTime;  
  53.   
  54.     /** 
  55.      * Constructor of RuntimeImpl class. 
  56.      */  
  57.     RuntimeImpl(VMManagement vm) {  
  58.         this.jvm = vm;  
  59.         this.vmStartupTime = jvm.getStartupTime();  
  60.     }  
  61.   
  62.     public String getName() {  
  63.         return jvm.getVmId();  
  64.     }  
  65.   
  66.     public String getManagementSpecVersion() {  
  67.         return jvm.getManagementVersion();  
  68.     }  
  69.   
  70.     public String getVmName() {  
  71.         return jvm.getVmName();  
  72.     }  
  73.   
  74.     public String getVmVendor() {  
  75.         return jvm.getVmVendor();  
  76.     }   
  77.   
  78.     public String getVmVersion() {  
  79.         return jvm.getVmVersion();  
  80.     }  
  81.   
  82.     public String getSpecName() {  
  83.         return jvm.getVmSpecName();  
  84.     }  
  85.   
  86.     public String getSpecVendor() {  
  87.         return jvm.getVmSpecVendor();  
  88.     }  
  89.   
  90.     public String getSpecVersion() {  
  91.         return jvm.getVmSpecVersion();  
  92.     }  
  93.   
  94.     public String getClassPath() {  
  95.         return jvm.getClassPath();  
  96.     }  
  97.   
  98.     public String getLibraryPath() {  
  99.         return jvm.getLibraryPath();  
  100.     }  
  101.   
  102.     public String getBootClassPath() {  
  103.         if (!isBootClassPathSupported()) {  
  104.             throw new UnsupportedOperationException(  
  105.                 "Boot class path mechanism is not supported");  
  106.         }  
  107.         ManagementFactory.checkMonitorAccess();  
  108.         return jvm.getBootClassPath();  
  109.     }  
  110.   
  111.     public List<String> getInputArguments() {  
  112.         ManagementFactory.checkMonitorAccess();  
  113.         return jvm.getVmArguments();  
  114.     }  
  115.   
  116.     public long getUptime() {         
  117.         long current = System.currentTimeMillis();  
  118.   
  119.         // TODO: If called from client side when we support  
  120.         // MBean proxy to read performance counters from shared memory,   
  121.         // need to check if the monitored VM exitd.  
  122.         return (current - vmStartupTime);  
  123.     }  
  124.   
  125.     public long getStartTime() {  
  126.         return vmStartupTime;  
  127.     }  
  128.   
  129.     public boolean isBootClassPathSupported() {  
  130.         return jvm.isBootClassPathSupported();  
  131.     }  
  132.   
  133.     public Map<String,String> getSystemProperties() {  
  134.         Properties sysProps = System.getProperties();  
  135.         Map<String,String> map = new HashMap<String, String>();  
  136.   
  137.         // Properties.entrySet() does not include the entries in  
  138.         // the default properties.  So use Properties.stringPropertyNames()  
  139.         // to get the list of property keys including the default ones.  
  140.         Set<String> keys = sysProps.stringPropertyNames();  
  141.         for (String k : keys) {  
  142.             String value = sysProps.getProperty(k);  
  143.             map.put(k, value);  
  144.         }  
  145.   
  146.         return map;  
  147.     }  
  148. }  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值