Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JMX监控现有的Java类?_Java_Jmx - Fatal编程技术网

如何使用JMX监控现有的Java类?

如何使用JMX监控现有的Java类?,java,jmx,Java,Jmx,我有一个现有的Java类,如下所示,我想使用JMX监控这个类中每个方法的方法调用数量。我该怎么做?我试过谷歌,但我看不出整个事情是如何联系在一起的。如果我能看到一些代码示例,那就太好了 Public class RPCServer { public void storeSchema() { // want to count number of method invocations System.out.println("storeSchema"); } pub

我有一个现有的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;
  }
}}
} 
路径org.foo.RPCServer.jmx是任意的

然后运行jconsole,找到正在运行的进程

然后,您可以运行命令countMethodInvocation,并获得执行时间

像这样:

本教程非常有用:


如果您想知道某些方法通过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;
  }
}}
} 
路径org.foo.RPCServer.jmx是任意的

然后运行jconsole,找到正在运行的进程

然后,您可以运行命令countMethodInvocation,并获得执行时间

像这样:

本教程非常有用:


嗨!!非常感谢你这么做。我应该把注册bean的代码放在哪里?放在应用程序的某个地方,可能就在main之后。如果你觉得我回答了你的问题,请检查我的答案:pLast question。为什么需要从现有类创建现有方法的接口?就像这个例子中的setRow,getRow?能给我一个countMethodInvocation吗?嗨!jmx控制台教程链接已断开,但我可以通过发布我的应用程序的jconsole pid_搜索并启动jconsole,并且我可以看到屏幕上countMethodInvocation的位置,但当我传递getRow时,jconsole抛出“调用countMethodInvocation:java.lang.NullPointerException的问题”。有什么想法吗?嗨!!非常感谢你这么做。我应该把注册bean的代码放在哪里?放在应用程序的某个地方,可能就在main之后。如果你觉得我回答了你的问题,请检查我的答案:pLast question。为什么需要从现有类创建现有方法的接口?就像这个例子中的setRow,getRow?能给我一个countMethodInvocation吗?嗨!jmx控制台教程链接已断开,但我可以通过发布我的应用程序的jconsole pid_搜索并启动jconsole,并且我可以看到屏幕上countMethodInvocation的位置,但当我传递getRow时,jconsole抛出“调用countMethodInvocation:java.lang.NullPointerException的问题”。有什么想法吗?