我正在使用JBoss运行客户端/服务器应用程序。
我如何连接到服务器JVM的MBeanServer?我想使用MemoryMX MBean跟踪内存消耗。
我可以使用JNDI查找连接到JBoss MBeanServer,但是java.lang.MemoryMX MBean未注册到JBoss MBeanServer。
编辑:要求从客户端以编程方式访问内存使用情况。
我写了一个类,就像这样:
import javax.management.remote.JMXServiceURL;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
public class JVMRuntimeClient
{
static void main(String[] args) throws Exception
{
if (args == null)
{
System.out.println("Usage: java JVMRuntimeClient HOST PORT");
}
if(args.length < 2)
{
System.out.println("Usage: java JVMRuntimeClient HOST PORT");
}
try
{
JMXServiceURL target = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://"+args[0]+":"+args[1]+"/jmxrmi");
JMXConnector connector = JMXConnectorFactory.connect(target);
MBeanServerConnection remote = connector.getMBeanServerConnection();
/**
* this is the part where you MUST know which MBean to get
* com.digitalscripter.search.statistics:name=requestStatistics,type=RequestStatistics
* YOURS WILL VARY!
*/
ObjectName bean = new ObjectName("com.digitalscripter.search.statistics:name=requestStatistics,type=RequestStatistics");
MBeanInfo info = remote.getMBeanInfo(bean);
MBeanAttributeInfo[] attributes = info.getAttributes();
for (MBeanAttributeInfo attr : attributes)
{
System.out.println(attr.getDescription() + " " + remote.getAttribute(bean,attr.getName()));
}
connector.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
System.exit(0);
}
}
}
MBeanServerConnection serverConn;
try {
//connect to a remote VM using JMX RMI
JMXServiceURL url = new JMXServiceURL( "service:jmx:rmi:///jndi/rmi://<addr>");
JMXConnector jmxConnector = JMXConnectorFactory.connect(url);
serverConn = jmxConnector.getMBeanServerConnection();
ObjectName objName = new
ObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME);
// Get standard attribute "VmVendor"
String vendor =
(String) serverConn.getAttribute(objName, "VmVendor");
} catch (...) { }
The following code lists all mbeans of a given (jmx enabled) java application with their attributes and operations grouped by the domain. Just start the java app you wanna monitor with a fixed jmx port, e.g. by using these vm parameters:
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9000
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
Then run this main:
import javax.management.*;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.io.IOException;
public class JmxListAll {
public static void main(String[] args) throws IOException, MalformedObjectNameException, IntrospectionException, InstanceNotFoundException, ReflectionException {
/*
1. JMXServiceURL.
*/
String jmxHost = "localhost:9000"; // exactly like jconsole localhost:9026
String url = "service:jmx:rmi:///jndi/rmi://" + jmxHost + "/jmxrmi";
JMXServiceURL serviceURL = new JMXServiceURL(url);
/*
2. JMXConnector and the actual serverConnection
*/
JMXConnector connector = JMXConnectorFactory.connect(serviceURL);
MBeanServerConnection serverConnection = connector.getMBeanServerConnection();
/*
3. Walk through the domains and their objects
*/
System.out.println("\n Now we have a look at " + serverConnection.getMBeanCount() + " mbeans!");
int objectCount = 0;
for (String domain : serverConnection.getDomains()) {
System.out.println("\n***********************************************************************************");
System.out.println("DOMAIN: " + domain);
// query all the beans for this domain using a wildcard filter
for (ObjectName objectName : serverConnection.queryNames(new ObjectName(domain + ":*"), null)) {
System.out.println(" objectName " + ++objectCount + ": " + objectName);
MBeanInfo info = serverConnection.getMBeanInfo(objectName);
for (MBeanAttributeInfo attr : info.getAttributes()) {
System.out.print(" attr: " + attr.getDescription());
try {
String val = serverConnection.getAttribute(objectName, attr.getName()).toString();
System.out.println(" -> " + abbreviate(val));
} catch (Exception e) {
System.out.println(" FAILED: " + e);
}
}
for (MBeanOperationInfo op : info.getOperations()) {
System.out.println(" op: " + op.getName());
}
}
}
}
static String abbreviate(String text) {
if (text != null && text.length() > 42) {
return text.substring(0, 42) + "...";
} else {
return text;
}
}
}
As you should see, in the java.lang domain are several memory related mbeans. Pick the one you need.
你尝试启动JConsole
(位于$JAVA_HOME/bin
)并连接服务器了吗?你应该能够从那里查看内存统计信息。