官方介绍:http://docs.oracle.com/javase/1.5.0/docs/guide/jmx/overview/JMXoverviewTOC.htmlhtml
JMX(Java Management Extensions) 是来管理网络,设备,应用程序等资源,它描述了一个可扩展的管理体系结构,而且提供了 JMX API 和一些预约义的 java 管理服务。java
经过JMX能够轻松地为应用程序添加管理功能,便可以在尽量少的改变原有系统的代码基础上实现对原系统的管理。windows
JMX Technology Architecture浏览器
Level安全
Description网络
Instrumentationoracle
Resources, such as applications, devices, or services, are instrumented using Java objects called Managed Beans (MBeans). MBeans expose their management interfaces, composed of attributes and operations, through a JMX agent for remote management and monitoring.app
Agentless
The main component of a JMX agent is the MBean server. This is a core managed object server in which MBeans are registered. A JMX agent also includes a set of services for handling MBeans. JMX agents directly control resources and make them available to remote management agents.socket
Remote Management
Protocol adaptors and standard connectors make a JMX agent accessible from remote management applications outside the agent’s Java Virtual Machine (JVM).
Managing Resources Remotely
JMX API instrumentation can be accessed in many different ways, either through existing management protocols such as the Simple Network Management Protocol (SNMP), or through proprietary protocols. The MBean server relies on protocol adaptors and connectors to make a JMX agent accessible from management applications outside the agent’s Java Virtual Machine (JVM).
Each adaptor provides a view through a specific protocol of all MBeans registered in the MBean server. For example, an HTML adaptor could display an MBean in a Web browser.
Connectors provide a manager-side interface that handles the communication between manager and JMX agent. Each connector provides the same remote management interface though a different protocol. When a remote management application uses this interface, it can connect to a JMX agent transparently through the network, regardless of the protocol.
JMX technology provides a standard solution for exporting JMX API instrumentation to remote applications, based on Remote Method Invocation (RMI). In addition, the JMX Remote API defines an optional protocol based directly on TCP sockets, called the JMX Messaging Protocol (JMXMP). An implementation of the JMX Remote API does not have to support this optional protocol. The J2SE platform, version 5.0, does not include the optional protocol. See Appendix A, "JMX Technology Versions"for further information.
The JMX Remote API specification describes how you can advertise and find JMX agents using existing discovery and lookup infrastructures. Examples of how to do this are provided and are described in the Java Management Extensions (JMX) Technology Tutorial. The specification does not define its own discovery and lookup service. The use of existing discovery and lookup services is optional: alternatively you can encode the addresses of your JMX API agents in the form of URLs, and communicate these URLs to the manager.
得到 MBeanServer 的实例
MBeanServer是MBean的容器,能够经过多种方式得到MBeanServer的实例,如:
MBeanServer server = MBeanServerFactory.createMBeanServer();
或
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
其中,经过下面的方式得到的实例能在jconsole中使用,而上面的不能。
建立 MBean
为了可以管理 Web 应用的资源,首先要使资源可以被管理,按照 JMX 规范的要求,咱们将资源封装成 MBean,实际上也就是为 Web 应用添加可管理性。
MBean就是遵照JMX规范的普通Class。
JMX中经常使用的概念:
Manageable resource:
可被管理的资源能够是应用程序,设备或者存在的可以被java程序所访问或者包装的实体。经过JMX能够管理这些资源。应用程序可以暴露本身的组件,API或者附加的资源,使得JMX可以管理应用程序。可被管理的资源甚至能够是网络上的设备,例如打印机。可被管理的资源做为一个实体被JMX MBean所管理。
MBean:
MBean(managed bean)是一个Java类,符合JXM specification所规定的命名和继承规范。实例化的MBeans是Java对象,其中所暴露出来的接口(management interface)可以操做和访问manageable resources。这些接口是由MBean的属性和操做组成。
Management application经过访问MBean来访问属性和调用操做。MBean分三种类型:Standard,Dynamic和Model MBean.每一种类型都是针对于特定的manageable resource来使用的。
JMX agent:
JMX agent是一个Java process,可以为管理MBean的集合提供服务,是MBean Server的容器。这些服务能够是创建MBean的之间的关系,动态加载类,监控服务,做为计时器。
Management application
一个management application能够是任何的用户程序,用于和任意多的JMX agent之间创建接口。对于一些设计好的符合JMX技术的management appliction,JMX agents可以创建和该management application的联系,JMX agents也可以创建和那些先前没有考虑用JMX技术的management application创建联系。
传输和安全性
JMX 指定了在 MBeanServer 和 JMX 客户之间通讯所使用的协议,协议能够在各类传输机制上运行。可使用针对本地链接的内置传输,及经过 RMI、socket 或 SSL 的远程传输(能够经过 JMX Connector API 建立新的传输)。认证是由传输执行的;本地传输容许用相同的用户 ID 链接到运行在本地系统上的 JVM;远程传输能够用口令或证书进行认证。本地传输在 Java 6 下默认就是启用的。要在 Java 5.0 下启用它,须要在 JVM 启动时定义系统属性 com.sun.management.jmxremote。“Monitoring and Management using JMX” 这份文档(请参阅参考资料)描述了启用和配置传输的配置步骤。
The MBean server relies on protocol adaptors and connectors to make a JMX agent accessible from management applications outside the agent’s Java Virtual Machine (JVM).
下面给出一个JMX开发示例程序:
1.首先定义一个MBean接口
Java代码 收藏代码
public interface ControllerMBean {
//属性
public void setName(String name);
public String getName();
//操做
/**
* 获取当前信息
* @return
*/
public String status();
public void start();
public void stop();
}
2.而后实现这个接口
Java代码 收藏代码
public class Controller implements ControllerMBean {
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
private String name;
public String status() {
return "this is a Controller MBean,name is " + this.name;
}
public void start() {
// TODO Auto-generated method stub
}
public void stop() {
// TODO Auto-generated method stub
}
}
3.在被管理的程序中加入这个管理对象
Java代码 收藏代码
import java.lang.management.ManagementFactory;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.swing.JDialog;
import jmx.Controller;
import jmx.ControllerMBean;
import com.sun.jdmk.comm.HtmlAdaptorServer;
public class Main {
/**
* @param args
* @throws NullPointerException
* @throws MalformedObjectNameException
* @throws NotCompliantMBeanException
* @throws MBeanRegistrationException
* @throws InstanceAlreadyExistsException
*/
public static void main(String[] args)
throws InstanceAlreadyExistsException, MBeanRegistrationException,
NotCompliantMBeanException, MalformedObjectNameException,
NullPointerException {
//得到MBeanServer实例
// MBeanServer mbs = MBeanServerFactory.createMBeanServer();//不能在jconsole中使用
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();//可在jconsole中使用
//建立MBean
ControllerMBean controller = new Controller();
//将MBean注册到MBeanServer中
mbs.registerMBean(controller, new ObjectName("MyappMBean:name=controller"));
//建立适配器,用于可以经过浏览器访问MBean
HtmlAdaptorServer adapter = new HtmlAdaptorServer();
adapter.setPort(9797);
mbs.registerMBean(adapter, new ObjectName(
"MyappMBean:name=htmladapter,port=9797"));
adapter.start();
//因为是为了演示保持程序处于运行状态,建立一个图形窗口
javax.swing.JDialog dialog = new JDialog();
dialog.setName("jmx test");
dialog.setVisible(true);
}
}
运行上面的程序
4.在windows下进入控制台(win+r->cmd),而后输入jconsole命令,稍等片刻打开jconsole的图形界面,在“本地”中选择刚才运行的程序,而后进入MBean面板页,便可看到MyappMBean一项,下面就是具体的MBean,可展开这些MBean对其操做。
因为上面的程序启用了html协议适配器,所以能够在浏览器中执行如同jconsole的操做,在浏览器中输入:http://localhost:9797便可
后面再研究connector有关身份认证的问题,这样就能以安全的方式链接到MBeanServer上了。