标签:jmx
The Java Management Extensions (JMX) API is a standard API for management and monitoring of resources such as applications, devices, services, and the Java virtual machine.
一句话:对系统资源进行管理的规范API.
ypical uses of the JMX technology include:
一句话 典型应用:改变、查看程序配置;收集计算程序行为数据;通知、告警。
An MBean is a managed Java object, similar to a JavaBeanTM, that follows the design patterns set forth in the instrumentation level of the JMX specification.
(4 kinds MBean)
- standard MBeans
- dynamic MBeans
- open MBeans
- model MBeans.
MBeans expose a management interface: a set of readable and/or writable attributes and a set of invokable operations, along with a self-description. The management interface does not change throughout the life of an MBean instance.
一句话 MBean是JMX的核心,MBean类似与javaBean,我们通过MBean暴漏出的接口去获取MBean所检测程序的各种信息。
A standard MBean is defined by writing a Java interface called SomethingMBean and a Java class called Something that implements that interface.
A standard MBean is composed of the MBean interface which lists the methods for all exposed attributes and operations, and the class which implements this interface and provides the functionality of the instrumented resource.
一句话,standard MBean 就是 一个实现了 接口:***MBean 的实现类(如TimerMBean,自己去看API docs),这里暴漏出获取信息的方法,以便活动你要的各种信息。
例子:
/* HelloMBean.java - MBean interface describing the management
operations and attributes for the Hello World MBean. In this case
there are two operations, "sayHello" and "add", and two attributes,
"Name" and "CacheSize". */
package com.example.mbeans;
public interface HelloMBean {
// operations
public void sayHello();
public int add(int x, int y);
// attributes
// a read-only attribute called Name of type String
public String getName();
// a read-write attribute called CacheSize of type int
public int getCacheSize();
public void setCacheSize(int size);
}
/* Hello.java - MBean implementation for the Hello World MBean.
This class must implement all the Java methods declared in the
HelloMBean interface, with the appropriate behavior for each one. */
package com.example.mbeans;
public class Hello implements HelloMBean {
public void sayHello() {
System.out.println("hello, world");
}
public int add(int x, int y) {
return x + y;
}
/* Getter for the Name attribute. The pattern shown here is
frequent: the getter returns a private field representing the
attribute value. In our case, the attribute value never
changes, but for other attributes it might change as the
application runs. Consider an attribute representing
statistics such as uptime or memory usage, for example. Being
read-only just means that it can‘t be changed through the
management interface. */
public String getName() {
return this.name;
}
/* Getter for the CacheSize attribute. The pattern shown here is
frequent: the getter returns a private field representing the
attribute value, and the setter changes that field. */
public int getCacheSize() {
return this.cacheSize;
}
/* Setter for the CacheSize attribute. To avoid problems with
stale values in multithreaded situations, it is a good idea
for setters to be synchronized. */
public synchronized void setCacheSize(int size) {
this.cacheSize = size;
/* In a real application, changing the attribute would
typically have effects beyond just modifying the cacheSize
field. For example, resizing the cache might mean
discarding entries or allocating new ones. The logic for
these effects would be here. */
System.out.println("Cache size now " + this.cacheSize);
}
private final String name = "Reginald";
private int cacheSize = DEFAULT_CACHE_SIZE;
private static final int DEFAULT_CACHE_SIZE = 200;
}
/* Main.java - main class for Hello World example. Create the
HelloWorld MBean, register it, then wait forever (or until the
program is interrupted). */
package com.example.mbeans;
import java.lang.management.*;
import javax.management.*;
public class Main {
/* For simplicity, we declare "throws Exception". Real programs
will usually want finer-grained exception handling. */
public static void main(String[] args) throws Exception {
// Get the Platform MBean Server attention!
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
// Construct the ObjectName for the MBean we will register
ObjectName name = new ObjectName("com.example.mbeans:type=Hello");
// Create the Hello World MBean
Hello mbean = new Hello();
// Register the Hello World MBean attention!
mbs.registerMBean(mbean, name);
// Wait forever
System.out.println("Waiting forever...");
Thread.sleep(Long.MAX_VALUE);
}
}
According to the JMX specification, an MBean interface consists of named and typed attributes that are readable and possibly writable, and named and typed operations that can be invoked by the applications that are managed by the MBean.
**一句话:
接口 以 **MBean 定义,并且这个MBean接口要写明 属性(如 , Name , CacheSize) 名称,类型,并且暴漏出去获取或设置他们的方法以及一些操作。如例子中add() and sayHello()。
用一个JMX agent去管理 MBean:JMX agent 的核心组件是the MBean server,管理之前要先注册进去。
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
通过调用java.lang.management.ManagementFactory.getPlatformMBeanServer()方法 获取运行在platform上的MBean Server.如果platform没有,则会自动获取一个(通过MBeanServerFactory.createMBeanServer())。
MBeans can generate notifications, for example to signal a state change, a detected event, or a problem.
不要忘了,MBean可以监测一个事件,问题,状态,并因此发送消息通知。
待续…….
标签:jmx
原文地址:http://blog.csdn.net/lemon89/article/details/45603183