java代码通过端口号拿到MemoryPoolMXBeans
时间: 2023-07-02 07:06:52 浏览: 83
可以使用以下的Java代码通过端口号拿到MemoryPoolMXBeans:
```java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.util.List;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.management.remote.rmi.RMIConnector;
import com.sun.management.HotSpotDiagnosticMXBean;
import com.sun.management.HotSpotDiagnosticMXBean.DumpReason;
import com.sun.management.VMOption;
public class MemoryPoolMXBeanClient {
public static void main(String[] args) throws Exception {
int portNumber = 9999;
String hostName = "localhost";
String url = "service:jmx:rmi:///jndi/rmi://" + hostName + ":" + portNumber + "/jmxrmi";
JMXServiceURL serviceUrl = new JMXServiceURL(url);
JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceUrl);
MBeanServerConnection mbeanServerConnection = jmxConnector.getMBeanServerConnection();
ObjectName memoryPoolMXBeansObjectName = new ObjectName("java.lang:type=MemoryPool,*");
List<ObjectName> memoryPoolMXBeans = mbeanServerConnection.queryNames(memoryPoolMXBeansObjectName, null);
for (ObjectName objName : memoryPoolMXBeans) {
System.out.println(objName);
}
jmxConnector.close();
}
}
```
这个程序会连接到指定的端口号上,然后从中获取`MemoryPoolMXBeans`。
阅读全文