jmx入门及开发示例 jmx程序管理

来源:互联网 发布:windows%20live 编辑:程序博客网 时间:2024/05/21 05:06

jmx入门及开发示例

jmx程序管理 

官方介绍: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

Level

Description

Instrumentation

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.

 

Agent

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.

 

Remote Management

Protocol adaptors and standard connectors make a JMX agent accessible from remote management applications outside the agents 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 agents 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 的实例 

MBeanServerMBean的容器,可以通过多种方式获得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所规定的命名和继承规范。实例化的MBeansJava对象,其中所暴露出来的接口(management interface)能够操作和访问manageable resources。这些接口是由MBean的属性和操作组成。

Management application通过访问MBean来访问属性和调用操作。MBean分三种类型:Standard,DynamicModel MBean.每一种类型都是针对于特定的manageable resource来使用的。

 

JMX agent:

JMX agent是一个Java process,能够为管理MBean的集合提供服务,是MBean Server的容器。这些服务可以是建立MBean的之间的关系,动态加载类,监控服务,作为计时器。

 

Management application

一个management application可以是任何的用户程序,用于和任意多的JMX agent之间建立接口。对于一些设计好的符合JMX技术的management applictionJMX agents能够建立和该management application的联系,JMX agents也能够建立和那些先前没有考虑用JMX技术的management application建立联系。

 

传输和安全性

JMX 指定了在 MBeanServer 和 JMX 客户之间通信所使用的协议,协议可以在各种传输机制上运行。可以使用针对本地连接的内置传输,及通过 RMIsocket 或 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 agents Java Virtual Machine (JVM).

 

 

下面给出一个JMX开发示例程序:

 

1.首先定义一个MBean接口

 

Java代码  收藏代码
  1. public interface ControllerMBean {  
  2.     //属性  
  3.     public void setName(String name);  
  4.     public String getName();  
  5.       
  6.       
  7.     //操作  
  8.     /** 
  9.      * 获取当前信息 
  10.      * @return 
  11.      */  
  12.     public String status();  
  13.     public void start();  
  14.     public void stop();  
  15.       
  16. }  

 2.然后实现这个接口

 

Java代码  收藏代码
  1. public class Controller implements ControllerMBean {  
  2.   
  3.     public void setName(String name) {  
  4.         this.name = name;  
  5.     }  
  6.   
  7.     public String getName() {  
  8.         return this.name;  
  9.     }  
  10.       
  11.     private String name;  
  12.   
  13.     public String status() {  
  14.         return "this is a Controller MBean,name is " + this.name;  
  15.     }  
  16.   
  17.     public void start() {  
  18.         // TODO Auto-generated method stub  
  19.     }  
  20.   
  21.   
  22.     public void stop() {  
  23.         // TODO Auto-generated method stub  
  24.     }  
  25. }  

 3.在被管理的程序中加入这个管理对象

 

Java代码  收藏代码
  1. import java.lang.management.ManagementFactory;  
  2.   
  3. import javax.management.InstanceAlreadyExistsException;  
  4. import javax.management.MBeanRegistrationException;  
  5. import javax.management.MBeanServer;  
  6. import javax.management.MBeanServerFactory;  
  7. import javax.management.MalformedObjectNameException;  
  8. import javax.management.NotCompliantMBeanException;  
  9. import javax.management.ObjectName;  
  10. import javax.swing.JDialog;  
  11.   
  12. import jmx.Controller;  
  13. import jmx.ControllerMBean;  
  14.   
  15. import com.sun.jdmk.comm.HtmlAdaptorServer;  
  16.   
  17. public class Main {  
  18.     /** 
  19.      * @param args 
  20.      * @throws NullPointerException 
  21.      * @throws MalformedObjectNameException 
  22.      * @throws NotCompliantMBeanException 
  23.      * @throws MBeanRegistrationException 
  24.      * @throws InstanceAlreadyExistsException 
  25.      */  
  26.     public static void main(String[] args)  
  27.             throws InstanceAlreadyExistsException, MBeanRegistrationException,  
  28.             NotCompliantMBeanException, MalformedObjectNameException,  
  29.             NullPointerException {  
  30.         //获得MBeanServer实例  
  31. //      MBeanServer mbs = MBeanServerFactory.createMBeanServer();//不能在jconsole中使用  
  32.         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();//可在jconsole中使用  
  33.         //创建MBean  
  34.         ControllerMBean controller = new Controller();  
  35.         //将MBean注册到MBeanServer中  
  36.         mbs.registerMBean(controller, new ObjectName("MyappMBean:name=controller"));  
  37.           
  38.         //创建适配器,用于能够通过浏览器访问MBean  
  39.         HtmlAdaptorServer adapter = new HtmlAdaptorServer();  
  40.         adapter.setPort(9797);  
  41.         mbs.registerMBean(adapter, new ObjectName(  
  42.                 "MyappMBean:name=htmladapter,port=9797"));  
  43.         adapter.start();  
  44.           
  45.         //由于是为了演示保持程序处于运行状态,创建一个图形窗口  
  46.         javax.swing.JDialog dialog = new JDialog();  
  47.         dialog.setName("jmx test");  
  48.         dialog.setVisible(true);  
  49.     }  
  50. }  

 

运行上面的程序

 

4.在windows下进入控制台(win+r->cmd),然后输入jconsole命令,稍等片刻打开jconsole的图形界面,在“本地”中选择刚才运行的程序,然后进入MBean面板页,即可看到MyappMBean一项,下面就是具体的MBean,可展开这些MBean对其操作。

 

由于上面的程序启用了html协议适配器,因此可以在浏览器中执行如同jconsole的操作,在浏览器中输入:http://localhost:9797即可

 

 

后面再研究connector有关身份认证的问题,这样就能以安全的方式连接到MBeanServer上了。

原创粉丝点击