官方介绍:http://docs.oracle.com/javase/1.5.0/docs/guide/jmx/overview/JMXoverviewTOC.html
JMX(Java Management Extensions) 是来管理网络,设备,应用程序等资源,它描述了一个可扩展的管理体系结构,并且提供了 JMX API 和一些预定义的 java 管理服务。
通过JMX可以轻松地为应用程序添加管理功能,即可以在尽可能少的改变原有系统的代码基础上实现对原系统的管理。
JMX Technology Architecture
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接口
- public interface ControllerMBean {
- //属性
- public void setName(String name);
- public String getName();
- //操作
- /**
- * 获取当前信息
- * @return
- */
- public String status();
- public void start();
- public void stop();
- }
2.然后实现这个接口
- 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.在被管理的程序中加入这个管理对象
- 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上了。