JMX简介

参考文章:

http://download.oracle.com/javase/tutorial/jmx/index.html


The Java Management Extensions (JMX)是java标准平台的一部分。最早出现在J2EE中,就是实现一些企业级的管理。

The JMX technology provides a simple, standard way of managing resources such as applications, devices, and services. Because the JMX technology is dynamic, you can use it to monitor and manage resources as they are created, installed and implemented. You can also use the JMX technology to monitor and manage the Java Virtual Machine (Java VM).

举个简单的例子,现在有3个独立的应用程序在production运行之中。我希望有一个平台,能集中监控这3个程序的运行状态,比如内存使用,取得程序的log。甚至可以通过该平台启停应用程序。这些就可以通过JMX实现。

而我见到的应用是实现integration test framework。其中使用了JMX来取得被测试程序的性能信息,日志;并且在测试当中对被测试程序启停并检查响应的状态;运行itf test case并检查程序的返回结果。

JMX还有一个功能是管理java虚机。这部分就不讲了。

JMX技术可以被分为以下三个层次, as follows:

  • Instrumentation
  • JMX agent
  • Remote management

1,Using the JMX technology, a given resource (可以理解为被监控的程序或device等)is instrumented by one or more Java objects known as Managed Beans, or MBeans. These MBeans are registered in a core-managed object server, known as an MBean server. The MBean server acts as a management agent and can run on most devices that have been enabled for the Java programming language. Instrumentation指的就是注册好的MBeans。

(这里加一句,instrument有被强制控制,执行的意思。)

2,A JMX agent consists of an MBean server, in which MBeans are registered, and a set of services for handling the MBeans. In this way, JMX agents directly control resources and make them available to remote management applications.

The core component of a JMX agent is the MBean server, a managed object server in which MBeans are registered. A JMX agent also includes a set of services to manage MBeans, and at least one communications adaptor or connector to allow access by a management application.

 

所以更简单一点的说法就是,被控制的程序被包装成MBeans,之后注册到MBean server。JMX agent实现对beans的使用执行等。最后在远程控制台对程序进行监控。


The JMX technology defines standard connectors (known as JMX connectors) that enable you to access JMX agents from remote management applications.JMX connectors using different protocols provide the same management interface.


所以要实现JMX的第一步就是要对源程序代码进行修改,将程序包装,注册成MBean。
A standard MBean is composed of an MBean interface and a class. The MBean interface lists the methods for all exposed attributes and operations. The class implements this interface and provides the functionality of the instrumented resource.
也就是说MBean的interface和class定义了被控制的程序希望暴露出来的方法和属性。比如,可以暴露方法让JMX实现对程序的开启关闭等操作。用attributes来查看程序的一些状态,如memory usage,log etc(可以写成get或也可以set)。

An example of a basic MBean interface,follows:

 
 
package com.example;

public interface HelloMBean {
 public void sayHello();
 public int add(int x, int y);
 public String getName();
 public int getCacheSize();
 public void setCacheSize(int size); 
}

之后写一个实现了该接口的class就可以了,名为Hello。具体实现就不写了。这样就建好了一个MBean。接下来的这段代码很重要,在实际的使用中也是这么用的。

package com.example; 

import java.lang.management.*; 
import javax.management.*;
public class Main { 
  public static void main(String[] args) throws Exception {
  MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  ObjectName name = new ObjectName("com.example:type=Hello");
  Hello mbean = new Hello(); mbs.registerMBean(mbean, name);
  System.out.println("Waiting forever...");
  Thread.sleep(Long.MAX_VALUE); 
 } 
}

The object name is an instance of the JMX class ObjectName and must conform to the syntax defined by the JMX specification. Namely, the object name must contain a domain and a list of key-properties. In the object name defined by Main, the domain is com.example (the package in which the example MBean is contained). In addition, the key-property declares that this object is of the type Hello。

With the Hello MBean registered in the MBean server, Main simply waits for management operations to be performed on Hello. In this example, these management operations are invoking sayHello() and add(), and getting and setting the attribute values. 这里有一个实际的例子,可直接运行: http://download.oracle.com/javase/tutorial/jmx/mbeans/standard.html 最后提到的一个概念是Notification。The JMX API defines a mechanism to enable MBeans to generate notifications, for example, to signal a state change, a detected event, or a problem. 当然notification可以在remote app中看到。 在我看到的实际使用中没有使用notification功能,而是自己定制了一个报表用于显示被控制程序的状态。 To generate notifications, an MBean must implement the interface NotificationEmitter or extend NotificationBroadcasterSupport. To send a notification, you need to construct an instance of the class javax.management.Notification or a subclass (such as AttributeChangedNotification), and pass the instance to NotificationBroadcasterSupport.sendNotification. 参考文章是:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值