62,612
社区成员
发帖
与我相关
我的任务
分享
package com.tools;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Set;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import sun.management.ConnectorAddressLink;
import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
public class AttachDemo
{
public void test(String pid) throws NumberFormatException, IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException, MalformedObjectNameException, NullPointerException, InstanceNotFoundException, IntrospectionException, ReflectionException, AttributeNotFoundException, MBeanException
{
String domainName = "Erosa";
String localJmxAddress = ConnectorAddressLink.importFrom(Integer.valueOf(pid).intValue());
if (localJmxAddress == null)
{
VirtualMachine vm = VirtualMachine.attach(pid);
String connectorAddress = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
if (connectorAddress == null)
{
String agent = vm.getSystemProperties().getProperty("java.home") + File.separator + "lib" + File.separator + "management-agent.jar";
vm.loadAgent(agent);
connectorAddress = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
}
if (connectorAddress == null)
{
System.err.println("Failed to get Local JMX Address by Pid. Exit Without Further Processing.");
System.exit(-1);
}
localJmxAddress = connectorAddress;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
JMXConnector connector = JMXConnectorFactory.newJMXConnector(new JMXServiceURL(localJmxAddress), null);
try
{
connector.connect();
MBeanServerConnection connection = connector.getMBeanServerConnection();
Set<ObjectInstance> set = connection.queryMBeans(new ObjectName(domainName + ":*"), null);
for (ObjectInstance ins : set)
{
System.out.println("==================================================================");
ObjectName name = ins.getObjectName();
System.out.println("MBean : " + name.toString());
MBeanInfo beaninfo = connection.getMBeanInfo(name);
MBeanAttributeInfo[] attributes = beaninfo.getAttributes();
System.out.println("-----------Attributes------------");
for (MBeanAttributeInfo attr : attributes)
{
System.out.println(attr.getName() + ": " + connection.getAttribute(name, attr.getName()));
}
MBeanOperationInfo[] operations = beaninfo.getOperations();
System.out.println("-----------Operations------------");
for (MBeanOperationInfo operation : operations)
{
System.out.println("operation: " + operation.getName() + " " + Arrays.toString(operation.getSignature()));
if (operation.getName().startsWith("dump"))
{
System.out.println(connection.invoke(name, operation.getName(), new Object[0], new String[0]));
}
}
}
System.out.println("exit jmx client.");
}
finally
{
connector.close();
reader.close();
}
}
}