监控运行中jar包的jvm信息
程序员文章站
2022-04-30 22:17:06
...
开启jar包 运行端口 修改java -jar命令 加入如下命令参数(端口可以任意,但不要与其他应用端口冲突)
-Dcom.sun.management.jmxremote.port=8996
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
下面脚本是启动jar的脚本 功能在于启动jar 并且把属性传入到main函数中 初始化作用 仅供参考
#MonCenter start shell
#set fileformat=unix
ps -ef | grep mon.jar | grep -v grep | awk '{print $2}' | xargs kill -9
pros()
{
propties=''
arr=()
i=0
while read line; do
arr[i++]=$line
done < $1
for data in ${arr[@]}
do
propties="$propties""$data""\r"
done
}
pros /soc/etc/jmxremote.properties
jmxremote=$propties
echo "======jmxremote.properties========="
echo $jmxremote
#start MonCenter
nohup java -Xmx200m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/soc/var/ -Djava.library.path=/soc/lib/sigar/ -Dcom.sun.management.jmxremote.port=8996 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -jar /soc/lib/mon.jar $jmxremote >/soc/var/nli/monstart.file 2>&1 &
tail -100f /soc/var/nli/monstart.file
获取jmx连接方法 url格式例如 String jmxUrl = "service:jmx:rmi:///jndi/rmi://" + ip + ":" + port+ "/jmxrmi";
/**
* 获得MBeanServer 的连接
*
* @param jmxUrl
* @return
* @throws IOException
*/
public static JMXConnector getMBeanServerConnection(String jmxUrl)
throws IOException {
JMXServiceURL url = new JMXServiceURL(jmxUrl);
JMXConnector jmxc = null;
try{
jmxc = JMXConnectorFactory.connect(url, null);
}catch (Exception e) {
logger.error("getMBeanServerConnection JMXConnector Exception ");
return null;
}
return jmxc;
}
获取连接
MBeanServerConnection mbsc = jmxConnection.getMBeanServerConnection();
获取线程信息例子
/**
* 获得jvm 线程信息
*/
public void getThreadInfo(JvmInfoBean jib,MBeanServerConnection jmxConnection){
ThreadMXBean op;
try {
op = ManagementFactory.newPlatformMXBeanProxy(jmxConnection, "java.lang:type=Threading", ThreadMXBean.class);
System.out.println("ObjectName="+op.getObjectName());
System.out.println("仍活动的线程总数="+op.getThreadCount());
System.out.println("峰值="+op.getPeakThreadCount());
System.out.println("线程总数(被创建并执行过的线程总数)="+op.getTotalStartedThreadCount());
System.out.println("当初仍活动的守护线程(daemonThread)总数="+op.getDaemonThreadCount());
// long[] deadlockedIds = op.findDeadlockedThreads();
// if(deadlockedIds != null && deadlockedIds.length > 0){
// ThreadInfo[] deadlockInfos = op.getThreadInfo(deadlockedIds);
// System.out.println("死锁线程信息:");
// System.out.println("\t\t线程名称\t\t状态\t\t");
// for(ThreadInfo deadlockInfo : deadlockInfos){
// System.out.println("\t\t"+deadlockInfo.getThreadName()+"\t\t"+deadlockInfo.getThreadState()
// +"\t\t"+deadlockInfo.getBlockedTime()+"\t\t"+deadlockInfo.getWaitedTime()
// +"\t\t"+deadlockInfo.getStackTrace().toString());
// }
// }
} catch (IOException e) {
e.printStackTrace();
}
}
其他参数可以通过 jconsole进行获取