ITPub博客

首页 > Linux操作系统 > Linux操作系统 > JMX学习笔记(一)

JMX学习笔记(一)

原创 Linux操作系统 作者:atlantisholic 时间:2011-07-02 18:57:32 0 删除 编辑

http://java.sun.com/developer/technicalArticles/J2SE/jmx.html

http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html

一.定义Mbean接口

使用JMX监控Mbean时,被监控的类需要实现相应的接口:

public interface HelloMBean {

   public void setMessage(String message);

   public String getMessage();

   public void sayHello();
}
定义实现类:
public class Hello implements HelloMBean {
   private String message = null;

   public Hello() {
      message = "Hello there";
   }

   public Hello(String message) {
      this.message = message;
   }

   public void setMessage(String message) {
      this.message = message;
   }

   public String getMessage() {
      return message;
   }

   public void sayHello() {
      System.out.println(message);
   }
}
二.将Mbean注册到MBeanServer上。
import javax.management.*;
import java.lang.management.*;

public class SimpleAgent {
   private MBeanServer mbs = null;

   public SimpleAgent() {

       mbs = ManagementFactory.getPlatformMBeanServer();

      Hello helloBean = new Hello();
      ObjectName helloName = null;

      try {
         helloName = new ObjectName("SimpleAgent:name=hellothere");
         mbs.registerMBean(helloBean, helloName);
      } catch(Exception e) {
         e.printStackTrace();
      }
   }

     private static void waitForEnterPressed() {
      try {
         System.out.println("Press  to continue...");
         System.in.read();
      } catch (Exception e) {
         e.printStackTrace();
      }
    }

   public static void main(String argv[]) {
      SimpleAgent agent = new SimpleAgent();
      System.out.println("SimpleAgent is running...");
      SimpleAgent.waitForEnterPressed();
   }
}
三.使用JMX监控方式启动java应用程序
java -Dcom.sun.management.jmxremote SimpleAgent
或者
java -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port="9004" -Dcom.sun.management.jmxremot
e.authenticate="false" -Dcom.sun.management.jmxremote.ssl="false" SimpleAgent
 
四.使用java自带JConsole工具,监控应用程序中Mbean的状态
JConsole或者JConsole 192.168.1.101:9004
 
五.使用远程连接器
import javax.management.*;
import java.lang.management.*;
import javax.management.remote.*;

public class SimpleAgent {
   private MBeanServer mbs = null;

   public SimpleAgent() {
      mbs = ManagementFactory.getPlatformMBeanServer();

      Hello helloBean = new Hello();
      ObjectName helloName = null;

      try {
         helloName = new ObjectName("SimpleAgent:name=hellothere");
         mbs.registerMBean(helloBean, helloName);

         JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server");
         JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
         cs.start();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }

   public static void main(String argv[]) {
      SimpleAgent agent = new SimpleAgent();
      System.out.println("SimpleAgent is running...");
   }
}
六.启动JConsole远程监控
 
七.使用HTML适配器,通过HTTP协议监控
import javax.management.*;
import java.lang.management.*;
import com.sun.jdmk.comm.HtmlAdaptorServer;

public class SimpleAgent {
   private MBeanServer mbs = null;

   public SimpleAgent() {
      mbs = ManagementFactory.getPlatformMBeanServer();
      HtmlAdaptorServer adapter = new HtmlAdaptorServer();

      Hello helloBean = new Hello();
      ObjectName adapterName = null;
      ObjectName helloName = null;

      try {
         helloName = new ObjectName("SimpleAgent:name=hellothere");
         mbs.registerMBean(helloBean, helloName);
         adapterName = new ObjectName("SimpleAgent:name=htmladapter,port=8000");
         adapter.setPort(8000);
         mbs.registerMBean(adapter, adapterName);
         adapter.start();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }

   public static void main(String argv[]) {
      SimpleAgent agent = new SimpleAgent();
      System.out.println("SimpleAgent is running...");
   }
}
直接访问 http://localhost:8000/即可监控 MBean的状态。

JConsole1.jpg

fig3.gif

fig4.gif

fig5.gif

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23071790/viewspace-701208/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-08-30

  • 博文量
    130
  • 访问量
    629729