发现查询的对象名称没有问题。事实上,它与 JConsole 中的相同;屏幕截图右侧显示的完整语法:
另外,我需要在 Eclipse 运行配置中添加一些系统属性:
-Dcom.sun.management.jmxremote.rmi.port=8700 -Dcom.sun.management.jmxremote.port=8600 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
然后我可以使用端口 8600 连接。在代码中:
package com.awgtek.miscpocs.lognfetch.client;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
public class TestJMXConn {
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:8600/jmxrmi");
JMXConnector connect = JMXConnectorFactory.connect(url);
MBeanServerConnection mbsc = connect.getMBeanServerConnection();
// ObjectName o = new ObjectName("com.netflix.servo:name=countSuccess,type=HystrixCommand,instance=LogAndFetchRestServicePostCommand");
ObjectName o = new ObjectName("com.netflix.servo:name=countSuccess,type=HystrixCommand,instance=LogAndFetchRestServiceGetCommand");
Object value = mbsc.getAttribute(o, "value");
System.out.println("the value: " + value);
connect.close();
}
}