JMX指南

来源:互联网 发布:mac book air 11.6 编辑:程序博客网 时间:2024/05/16 00:51
JMX(java管理扩展)可以用来管理网络,设备,应用程序等资源,写这篇文章时规范为1.1版。


JMX的结构

在JMX中共分为三层
1:Instrumentation
2:Agent
3:Distributed Services

JMX的优点:

1:可以非常容易的使应用程序具有被管理的功能
2:提供具有高度伸缩性的架构
每个JMX Agent服务可以很容易的放入到Agent中,每个JMX的实现都提供几个核心的Agent服务,你也可以自己编写服务,服务可以很容易的部署,取消部署。
3:集成了现有的一些管理解决方案,如SNMP
4:非常容易的利用其他java技术
5:主要提供接口,允许有不同的实现

在JMX的规范中,只讲述了前两部分,分布式服务并没有涉及到,jmx规范还在不断完善中

jmx
上面的这个图是jmx in action中的一副JMX的结构图,通过上面这个图,我们可以将JMX的层次分的更清楚一点。
通常我们需要写一个Bean(Managed bean,简称MBean),利用这个bean去管理资源。

基本概念:

1:MBean
通常是一个java类,它提供接口可以使这个类具有管理功能(如standard MBean,接口中定义的方法使MBean具有管理功能)。
2:MBean server
是管理MBean的一个java类,你需要向MBean server注册一个MBean后,这个MBean才会具有管理功能,MBean server还提供了查询功能和注册监听器的功能,sun提供的只是接口,不同的jmx实现中的MBean server实现也不同。
3:JMX agent
agent是为了管理一系列的MBean,而提供的一系列的服务,如上图所示,通常有MBean relationships, dynamically loading classes, simple monitoring services, timers。agent可以利用Protocol adapters(例如HTTP 和SNMP)和connectors(RMI 和Jini)使不同的客户端可以访问MBean。
4:Protocol adapters 和connectors
适配器和连接器主要使不同的协议和客户端可以使用这个agent,一个agent中可以有多个Protocol adapters 和connectors,这样管理起MBean来就更方便了(有多种类型的客户端和协议可以操作MBean)。注意,Protocol adapters 和connectors通常也是MBean。

MBean的类型

JMX1.1相对于1.0有了很大改进,在1.1中提供4种类型的MBean,3,4两种MBean都是特殊类型的dynamic MBean。
1:standard MBean
2:dynamic MBean
3:open MBean
4:model MBean
类型 描述 standard MBean 这种类型的MBean最简单,它能管理的资源(包括属性,方法,时间)必须定义在接口中,然后MBean必须实现这个接口。它的命名也必须遵循一定的规范,例如我们的MBean为Hello,则接口必须为HelloMBean。 dynamic MBean 必须实现javax.management.DynamicMBean接口,所有的属性,方法都在运行时定义 open MBean 此MBean的规范还不完善,正在改进中 model MBean 与标准和动态MBean相比,你可以不用写MBean类,只需使用 javax.management.modelmbean.RequiredModelMBean即可。RequiredModelMBean实现了 ModelMBean接口,而ModelMBean扩展了DynamicMBean接口,因此与DynamicMBean相似,Model MBean的管理资源也是在运行时定义的。与DynamicMBean不同的是,DynamicMBean管理的资源一般定义在DynamicMBean 中(运行时才决定管理那些资源),而model MBean管理的资源并不在MBean中,而是在外部(通常是一个类),只有在运行时,才通过set方法将其加入到model MBean中。后面的例子会有详细介绍

 

Standard MBean编程简介


上面我们已经提到了编写这种类型MBean的规则
1:编写接口
2:编写MBean
3:编写agent
下面看接口的代码,very easy,主要定义get/get方法和其它你想使用的方法

public interface HelloWorldMBean
{
public void setGreeting( String greeting );

public String getGreeting();

public void printGreeting();
}

再看MBean的代码,跟传统的Bean非常相似,加入某个属性只有get方法,则说明这个属性是只读的,get/set都有的话,说明这个属性是可读可写的,此MBean只定义了一个属性 greeting。另外还定义了一个方法printGreeting。 public class HelloWorld implements HelloWorldMBean
{

private String greeting = null;

public HelloWorld()
{
this.greeting = "Hello World! I am a Standard MBean";
}

public HelloWorld( String greeting )
{
this.greeting = greeting;
}

public void setGreeting( String greeting )
{
this.greeting = greeting;
}

public String getGreeting()
{
return greeting;
}

public void printGreeting()
{
System.out.println( greeting );
}
}
下面编写agent
1:创建 MBeanServer
2:创建一个adapter
3:将MBean注册到MBeanServer,同时将apapter也注册到MBeanServer
4: 启动adapter import javax.management.*;
import com.sun.jdmk.comm.*;

public class HelloAgent
{
private MBeanServer mbs = null;

public HelloAgent()
{
mbs = MBeanServerFactory.createMBeanServer( "HelloAgent" );

HtmlAdaptorServer adapter = new HtmlAdaptorServer();

HelloWorld hw = new HelloWorld();

ObjectName adapterName = null;
ObjectName helloWorldName = null;

try
{

helloWorldName = new ObjectName( "HelloAgent:name=helloWorld1" );
mbs.registerMBean( hw, helloWorldName );
adapterName = new ObjectName( "HelloAgent:name=htmladapter,port=9092" );
adapter.setPort( 9092 );
mbs.registerMBean( adapter, adapterName );
adapter.start();

}
catch( Exception e )
{
e.printStackTrace();
}

}

public static void main( String args[] )
{
System.out.println( "HelloAgent is running" );
HelloAgent agent = new HelloAgent();
}

}

上面的代码使用了sun的一个adapter:HtmlAdaptorServer,使得html客户端可以访问MBean,注意ObjectName类,它的书写规则是这样的:"Domain names:key=value,key2=value2",因为createMBeanServer方法中我们使用了参数"HelloAgent",因此后面的Domain names必须为HelloAgent。
运行
将jmx的包放到classpath中,运行HelloAgent类,然后通过浏览器访问http://localhost:9092即可

使用通知(notification)

通知其实就是一个监听器,我们可以为MBean注册一个监听器,当满足一定的条件时,MBean会发消息给监听器,然后监听器进行必要的处理。
一个MBean可以通过两种方法实现notification。
1:实现javax.management.NotificationBroadcaster接口
2:扩展javax.management.NotificationBroadcasterSupport类(这个类也实现了上面的接口)

 



要编写监听器,必须实现javax.management。NotificationListener接口
下面看一个简单的通知的例子,利用方式2实现
MBean的代码:

import java.io.*;
import javax.management.*;

public class HelloWorld extends NotificationBroadcasterSupport
implements HelloWorldMBean
{

public HelloWorld()
{
this.greeting = "Hello World! I am a Standard MBean";
}

public HelloWorld( String greeting )
{
this.greeting = greeting;
}
public void setGreeting( String greeting )
{
this.greeting = greeting;
Notification notification = new Notification(
"jmxbook.ch2.helloWorld.test", this, -1,
System.currentTimeMillis(), greeting );
sendNotification( notification );
}

public String getGreeting()
{
return greeting;
}

public void printGreeting()
{
System.out.println( greeting );
}
private String greeting;
}

上面的代码仅仅用了一个最简单的通知类:Notification

public Notification(java.lang.String type, java.lang.Object source,
long sequenceNumber,long timeStamp,
java.lang.String message)

type用来标示通知, source为产生通知的MBean,sequenceNumber为一系列通知中的序号,timeStamp为通知创建的时间,message为具体的通知消息。
由于从javax.management.NotificationBroadcasterSupport继承,所以编写起来容易些。

下面看agent类

import javax.management.*;
import com.sun.jdmk.comm.HtmlAdaptorServer;

public class HelloAgent implements NotificationListener

{
private MBeanServer mbs = null;

public HelloAgent ( )
{
mbs = MBeanServerFactory.createMBeanServer( "HelloAgent" );
HtmlAdaptorServer adapter = new HtmlAdaptorServer();
HelloWorld hw = new HelloWorld();

ObjectName adapterName = null;
ObjectName helloWorldName = null;

try
{
adapterName = new ObjectName(
"HelloAgent:name=htmladapter,port=9092" );
mbs.registerMBean( adapter, adapterName );
adapter.start();

helloWorldName = new ObjectName(
"HelloAgent:name=helloWorld1" );
mbs.registerMBean( hw, helloWorldName );

hw.addNotificationListener( this, null, null );

}
catch( Exception e )
{
e.printStackTrace();
}

}//constructor

public void handleNotification(
Notification notif, Object handback )
{
System.out.println( "Receiving notification..." );
System.out.println( notif.getType() );
System.out.println( notif.getMessage() );
}

public static void main( String args[] )
{
HelloAgent agent = new HelloAgent();
}

}

在agent类中
hw.addNotificationListener( this, null, null );将agent作为helloworld MBean的监听器,并且agent类实现了NotificationListener 接口,方法handleNotification处理具体的通知到达的情况。

动态MBean


前面讲的标准MBean只能用来管理静态的资源,即所有要管理的资源必须是已知的。但是我们要管理的资源并不是一成不变的,随着时间的变化,我们要管理的资源也会随之而变(例如,管理的范围扩大了)。在这种情况下,标准MBean就不适合我们的要求了。在这种情况下,动态MBean就比较适合我们的要求。动态MBean要管理的资源并不是早就定义好的,而是在运行期才定义的。动态MBean主要利用一些辅助类来完成这个功能,后面的ModelMBean也是如此。所有的动态MBean必须实现javax.management.DynamicMBean 接口。下图是动态MBean的一个结构图。


jmx
下面看一下DynamicMBean接口的定义:
package javax.management;

public interface DynamicMBean
{
public Object getAttribute( String attribute )
throws AttributeNotFoundException, MBeanException,
ReflectionException;

public void setAttribute( Attribute attribute )
throws AttributeNotFoundException,
InvalidAttributeValueException,
MBeanException,
ReflectionException;

public AttributeList getAttributes( String[] attributes );

public AttributeList setAttributes( AttributeList attributes );

public Object invoke( String actionName, Object[] params,
String[] signature ) throws MBeanException,
ReflectionExceptionn

public MBeanInfo getMBeanInfo();
}

 


上面的前5个方法用于操作属性,方法,构建器等,getMBeanInfo方法用于提供给agent一个关于此MBean的描述,MBeanInfo类是一个基本的辅助类,用于容纳MBean的各种信息。
注意invoke方法:actionName为方法名,params为参数值,signature为参数的类型(例如java,lang.String).
至于辅助类MBeanInfo这里就不说了,还有其他很多辅助类,可以参见jmx规范.
下面看一个简单的动态MBean,这是sun参考实现中的一个例子

import java.lang.reflect.Constructor;
import java.util.Iterator;
import javax.management.*;

/**
*简单动态MBean
* - "State"属性:可读可写
* - "NbChanges"属性:只读
* - "reset()" :方法
*注意:动态MBean必须定义一个public的构建器,且必须通过MBeanInfo来管理
*/

public class SimpleDynamic implements DynamicMBean {

public SimpleDynamic() {

// 建立辅助信息
//
buildDynamicMBeanInfo();
}


public Object getAttribute(String attribute_name)
throws AttributeNotFoundException,
MBeanException,
ReflectionException {

// 检查属性是否为空
if (attribute_name == null) {
throw new RuntimeOperationsException(new IllegalArgumentException("Attribute name cannot be null"),
"Cannot invoke a getter of " + dClassName + " with null attribute name");
}
// 检查已知属性,调用已知方法
if (attribute_name.equals("State")) {
return getState();
}
if (attribute_name.equals("NbChanges")) {
return getNbChanges();
}
// 如果属性不可识别,抛出异常
throw(new AttributeNotFoundException("Cannot find " + attribute_name + " attribute in " + dClassName));
}
public void setAttribute(Attribute attribute)
throws AttributeNotFoundException,
InvalidAttributeValueException,
MBeanException,
ReflectionException {
if (attribute == null) {
throw new RuntimeOperationsException(new IllegalArgumentException("Attribute cannot be null"),
"Cannot invoke a setter of " + dClassName + " with null attribute");
}
String name = attribute.getName();
Object value = attribute.getValue();

if (name == null) {
throw new RuntimeOperationsException(new IllegalArgumentException("Attribute name cannot be null"),
"Cannot invoke the setter of " + dClassName + " with null attribute name");
}

if (name.equals("State")) {
if (value == null) {
try {
setState( null );
} catch (Exception e) {
throw(new InvalidAttributeValueException("Cannot set attribute "+ name +" to null"));
}
}
else {
try {
if ((Class.forName("java.lang.String")).isAssignableFrom(value.getClass())) {
setState((String) value);
}
else {
throw(new InvalidAttributeValueException("Cannot set attribute "+ name +" to a " +
value.getClass().getName() + " object, String expected"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
// 由于"NbChanges" 属性是只读的,所以抛出异常
else if (name.equals("NbChanges")) {
throw(new AttributeNotFoundException("Cannot set attribute "+ name +" because it is read-only"));
}
else {
throw(new AttributeNotFoundException("Attribute " + name +
" not found in " + this.getClass().getName()));
}
}


public AttributeList getAttributes(String[] attributeNames) {
if (attributeNames == null) {
throw new RuntimeOperationsException(new IllegalArgumentException("attributeNames[] cannot be null"),
"Cannot invoke a getter of " + dClassName);
}
AttributeList resultList = new AttributeList();


if (attributeNames.length == 0)
return resultList;

for (int i=0 ; i<attributeNames.length ; i++){
try {
Object value = getAttribute((String) attributeNames[i]);
resultList.add(new Attribute(attributeNames[i],value));
} catch (Exception e) {
e.printStackTrace();
}
}
return(resultList);
}


public AttributeList setAttributes(AttributeList attributes) {


if (attributes == null) {
throw new RuntimeOperationsException(new IllegalArgumentException("AttributeList attributes cannot be null"),
"Cannot invoke a setter of " + dClassName);
}
AttributeList resultList = new AttributeList();


if (attributes.isEmpty())
return resultList;


for (Iterator i = attributes.iterator(); i.hasNext();) {
Attribute attr = (Attribute) i.next();
try {
setAttribute(attr);
String name = attr.getName();
Object value = getAttribute(name);
resultList.add(new Attribute(name,value));
} catch(Exception e) {
e.printStackTrace();
}
}
return(resultList);
}

/**
* 设置操作
*/
public Object invoke(String operationName, Object params[], String signature[])
throws MBeanException,
ReflectionException {

//检查方法名是否为空
if (operationName == null) {
throw new RuntimeOperationsException(new IllegalArgumentException("Operation name cannot be null"),
"Cannot invoke a null operation in " + dClassName);
}
if (operationName.equals("reset")){
reset();
return null;
} else {
throw new ReflectionException(new NoSuchMethodException(operationName),
"Cannot find the operation " + operationName + " in " + dClassName);
}
}


public MBeanInfo getMBeanInfo() {
return(dMBeanInfo);
}


/*
* -----------------------------------------------------
* 下面是公共方法
* -----------------------------------------------------
*/
public String getState() {
return state;
}
public void setState(String s) {
state = s;
nbChanges++;
}
public Integer getNbChanges() {
return new Integer(nbChanges);
}
public void reset() {
state = "initial state";
nbChanges = 0;
nbResets++;
}
public Integer getNbResets() {
return new Integer(nbResets);
}



/**
* 构造辅助信息,这里用了很多辅助类,具体看规范
*/
private void buildDynamicMBeanInfo() {

dAttributes[0] = new MBeanAttributeInfo("State",
"java.lang.String",
"State: state string.",
true,
true,
false);
dAttributes[1] = new MBeanAttributeInfo("NbChanges",
"java.lang.Integer",
"NbChanges: number of times the State string has been changed.",
true,
false,
false);

Constructor[] constructors = this.getClass().getConstructors();
dConstructors[0] = new MBeanConstructorInfo("SimpleDynamic(): Constructs a SimpleDynamic object",
constructors[0]);

MBeanParameterInfo[] params = null;
dOperations[0] = new MBeanOperationInfo("reset",
"reset(): reset State and NbChanges attributes to their initial values",
params ,
"void",
MBeanOperationInfo.ACTION);

dMBeanInfo = new MBeanInfo(dClassName,
dDescription,
dAttributes,
dConstructors,
dOperations,
new MBeanNotificationInfo[0]);
}

/*
* -----------------------------------------------------
* 私有变量
* -----------------------------------------------------
*/

private String state = "initial state";
private int nbChanges = 0;
private int nbResets = 0;


private String dClassName = this.getClass().getName();
private String dDescription = "Simple implementation of a dynamic MBean.";

private MBeanAttributeInfo[] dAttributes = new MBeanAttributeInfo[2];
private MBeanConstructorInfo[] dConstructors = new MBeanConstructorInfo[1];
private MBeanOperationInfo[] dOperations = new MBeanOperationInfo[1];
private MBeanInfo dMBeanInfo = null;

}

上面这个类非常简单,只是用到了一些辅助类.
为了简单起见,我们下面演示怎样在agent中使用动态MBean,而不是给出整个agent类的代码.

1:创建server,注册MBean
MBeanServer server = MBeanServerFactory.createMBeanServer();
ObjectName mbeanObjectName = null;
String domain = server.getDefaultDomain();
String mbeanName = "SimpleDynamic";
try {
mbeanObjectName = new ObjectName(domain + ":type=" + mbeanName);
server.createMBean(mbeanName,mbeanObjectName)
} catch(MalformedObjectNameException e) {
e.printStackTrace();
System.out.println("nEXITING...n");
System.exit(1);
}
2:得到属性
String State = (String) server.getAttribute(mbeanObjectName,"State");
Integer NbChanges = (Integer) server.getAttribute(mbeanObjectName,"NbChanges");
3:设置属性
Attribute nbChangesAttribute = new Attribute("NbChanges", new Integer(1));
try {
server.setAttribute(mbeanObjectName, nbChangesAttribute);
} catch (Exception e) {
e.printStackTrace();
}
4:执行操作
server.invoke(mbeanObjectName,"reset",null,null);
5:得到辅助信息
MBeanInfo info = null;
try {
info = server.getMBeanInfo(mbeanObjectName);
} catch (Exception e) {
System.out.println("t!!! Could not get MBeanInfo object for "+ mbeanName +" !!!");
e.printStackTrace();
return;
}
System.out.println("nCLASSNAME: t"+ info.getClassName());
System.out.println("nDESCRIPTION: t"+ info.getDescription());
System.out.println("nATTRIBUTES");
MBeanAttributeInfo[] attrInfo = info.getAttributes();
if (attrInfo.length>0) {
for(int i=0; i<attrInfo.length; i++) {
System.out.println(" ** NAME: t"+ attrInfo[i].getName());
System.out.println(" DESCR: t"+ attrInfo[i].getDescription());
System.out.println(" TYPE: t"+ attrInfo[i].getType() +
"tREAD: "+ attrInfo[i].isReadable() +
"tWRITE: "+ attrInfo[i].isWritable());
}
} else System.out.println(" ** No attributes **");
System.out.println("nCONSTRUCTORS");
MBeanConstructorInfo[] constrInfo = info.getConstructors();
for(int i=0; i<constrInfo.length; i++) {
System.out.println(" ** NAME: t"+ constrInfo[i].getName());
System.out.println(" DESCR: t"+ constrInfo[i].getDescription());
System.out.println(" PARAM: t"+ constrInfo[i].getSignature().length +" parameter(s)");
}
System.out.println("nOPERATIONS");
MBeanOperationInfo[] opInfo = info.getOperations();
if (opInfo.length>0) {
for(int i=0; i<opInfo.length; i++) {
System.out.println(" ** NAME: t"+ opInfo[i].getName());
System.out.println(" DESCR: t"+ opInfo[i].getDescription());
System.out.println(" PARAM: t"+ opInfo[i].getSignature().length +" parameter(s)");
}
} else System.out.println(" ** No operations ** ");
System.out.println("nNOTIFICATIONS");
MBeanNotificationInfo[] notifInfo = info.getNotifications();
if (notifInfo.length>0) {
for(int i=0; i<notifInfo.length; i++) {
System.out.println(" ** NAME: t"+ notifInfo[i].getName());
System.out.println(" DESCR: t"+ notifInfo[i].getDescription());
}

动态MBean仅仅必标准MBean复杂一点点,就是构造这些这个MBean有点麻烦.不过你怾编写自己的辅助类,将这些麻烦简化一下,在书jmx in action中就提供了这样一个辅助类.后面讲的model MBean与动态MBean相似(因为其本身就是一种特殊的动态MBean).

 

 

转载:http://zongfeng.bloghome.cn/posts/547.html