![](/img/trans.png)
[英]jmeter: How to set the thread count for the existing jmx file in java code?
[英]How to Monitor an existing Java class with JMX?
我有一個現有的Java類,如下所示,我想使用JMX監視此類中每個方法的方法調用次數。 我該怎么做? 我試過谷歌,但我無法看到整個事物如何連接的大局。 如果我能看到一些代碼示例,那就太好了
Public class RPCServer {
public void storeSchema() { // want to count number of method invocations
System.out.println("storeSchema");
}
public void getSchema() { // want to count number of method invocations
System.out.println("getSchema");
}
public void storeRow() { // want to count number of method invocations
System.out.println("storeRow");
}
public void getRow() { //want to count number of method invocations
System.out.println("getRow");
}
}
我想看看有多少時間通過JMX執行某些方法,我提出了這個解決方案
首先,您需要一個適合您班級的界面。 只有JMX可以看到此接口的方法:
public interface RPCServerInterface {
int countMethodInvocation(String method);
}
然后在課堂上存儲每個函數調用的時間。
public class RPCServer implements RPCServerInterface{
private int row;
private Map<String,Integer> countByMethod = new HashMap<String,Integer>();
// +1 to the number of time of execution of this method
private void sumMethodInvocation(String method) {
if ( countByMethod.containsKey(method) ) {
int n = countByMethod.get(method);
countByMethod.put(method, n+1);
} else {
countByMethod.put(method,1);
}
}
// how many time the method has been invoked
@Override
public int countMethodInvocation(String method){
return countByMethod.containsKey(method)?countByMethod.get(method):0;
}
public void setRow(int i) {
// register each time is executed
this.sumMethodInvocation("setRow");
this.row = i;
}
public int getRow() {
// register each time is executed
this.sumMethodInvocation("getRow");
return row;
}
}}
}
然后你必須注冊你的Bean:
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
RPCServer rpcServer = new RPCServer();
ObjectName objectName = new ObjectName("org.foo.RPCServer.jmx:type=RPCServerInterface");
StandardMBean standardMBean = new StandardMBean(rpcServer,RPCServerInterface.class);
mBeanServer.registerMBean(standardMBean, objectName);
路徑org.foo.RPCServer.jmx是任意的。
然后運行jconsole,找到正在運行的進程。
然后,您可以運行命令countMethodInvocation,您可以獲得執行時間。
像這樣:
本教程非常有用:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.