系列
- Arthas入门篇
- Arthas功能介绍
- Arthas 启动过程分析
- Arthas使用Idea调试
- Arthas Command处理流程
- Arthas类查找和反编译原理
- Arthas内存动态编译原理
- Arthas动态重新加载类
- Arthas导出加载类
- Arthas classloader类加载器
- Arthas 查看当前JVM信息
- Arthas thread查看线程信息
开篇
- Arthas提供sysprop命令查看当前JVM的系统属性(System Property)。
- Arthas提供sysenv查看当前JVM的环境属性(System Environment Variables)。
- Arthas提供vmoption查看更新VM诊断相关的参数。
原理介绍
在安装JDK的时候需要配置环境变量,安装完之后我们需要查看JDK的版本,我们就需要使用查看系统属性。Java提供了System类的静态方法getenv()方法和getProperty()方法,用于返回系统相关的环境变量与系统属性。
- getenv()方法返回的变量大多与操作系统相关。
- getProperty()方法返回的变量大多与Java程序有关。
- 通过HotSpotDiagnosticMXBean的getVMOption来获取VMOption参数。
源码分析
public class SystemEnvCommand extends AnnotatedCommand {
private String envName;
@Argument(index = 0, argName = "env-name", required = false)
@Description("env name")
public void setOptionName(String envName) {
this.envName = envName;
}
@Override
public void process(CommandProcess process) {
try {
SystemEnvModel result = new SystemEnvModel();
if (StringUtils.isBlank(envName)) {
// show all system env
result.putAll(System.getenv());
} else {
// view the specified system env
String value = System.getenv(envName);
result.put(envName, value);
}
process.appendResult(result);
process.end();
} catch (Throwable t) {
process.end(-1, "Error during setting system env: " + t.getMessage());
}
}
}
- System.getenv()来获取环境变量。
public class SystemPropertyCommand extends AnnotatedCommand {
private String propertyName;
private String propertyValue;
@Override
public void process(CommandProcess process) {
try {
if (StringUtils.isBlank(propertyName) && StringUtils.isBlank(propertyValue)) {
// show all system properties
process.appendResult(new SystemPropertyModel(System.getProperties()));
} else if (StringUtils.isBlank(propertyValue)) {
// view the specified system property
String value = System.getProperty(propertyName);
if (value == null) {
process.end(1, "There is no property with the key " + propertyName);
return;
} else {
process.appendResult(new SystemPropertyModel(propertyName, value));
}
} else {
// change system property
System.setProperty(propertyName, propertyValue);
process.appendResult(new MessageModel("Successfully changed the system property."));
process.appendResult(new SystemPropertyModel(propertyName, System.getProperty(propertyName)));
}
process.end();
} catch (Throwable t) {
process.end(-1, "Error during setting system property: " + t.getMessage());
}
}
}
- 通过System.getProperty来获取属性。
public class VMOptionCommand extends AnnotatedCommand {
private String name;
private String value;
private static void run(CommandProcess process, String name, String value) {
try {
HotSpotDiagnosticMXBean hotSpotDiagnosticMXBean = ManagementFactory
.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
if (StringUtils.isBlank(name) && StringUtils.isBlank(value)) {
// show all options
process.appendResult(new VMOptionModel(hotSpotDiagnosticMXBean.getDiagnosticOptions()));
} else if (StringUtils.isBlank(value)) {
// view the specified option
VMOption option = hotSpotDiagnosticMXBean.getVMOption(name);
if (option == null) {
process.end(-1, "In order to change the system properties, you must specify the property value.");
return;
} else {
process.appendResult(new VMOptionModel(Arrays.asList(option)));
}
} else {
VMOption vmOption = hotSpotDiagnosticMXBean.getVMOption(name);
String originValue = vmOption.getValue();
// change vm option
hotSpotDiagnosticMXBean.setVMOption(name, value);
process.appendResult(new MessageModel("Successfully updated the vm option."));
process.appendResult(new VMOptionModel(new ChangeResultVO(name, originValue,
hotSpotDiagnosticMXBean.getVMOption(name).getValue())));
}
process.end();
} catch (Throwable t) {
}
}
}
- 通过ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)来获取HotSpotDiagnosticMXBean对象。
- 通过hotSpotDiagnosticMXBean.getDiagnosticOptions()来获取VMOption属性。
网友评论