jmx 入门

本文介绍了如何使用Java管理Bean (MBean) 创建和管理服务。通过创建MBean Server实例及HTML适配器,并注册自定义的MBean实现,实现远程管理和监控应用的功能。此外,还介绍了如何利用Admin View页面动态添加和移除MBean。

 

package test;

public interface HelloWorldMBean {

	public void setGreeting(String greeting);
	public String getGreeting();
	public void printGreeting();
}

 

 

 

package test;

public class HelloWorld implements HelloWorldMBean {

	private String greeting=null;
	
	public HelloWorld(){
		this.greeting="hello,wrold i am a standard MBean";
	}
	public HelloWorld(String greeting){
		this.greeting=greeting;
	}
	
	
	public void setGreeting(String greeting) {
		// TODO Auto-generated method stub
		this.greeting=greeting;

	}

	public String getGreeting() {
		// TODO Auto-generated method stub
		return greeting;
	}

	public void printGreeting() {
		// TODO Auto-generated method stub
		System.out.println(greeting);
	}

}

 

 

 

package test;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

import com.sun.jdmk.comm.HtmlAdaptorServer;

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=helloWorld");
			mbs.registerMBean(hw, helloWorldName);
			adapterName=new ObjectName("HelloAgent:name=htmladapter,port=9092");
			adapter.setPort(9092);
			mbs.registerMBean(adapter, adapterName);
			adapter.start();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
			public static void main(String args[]){
				System.out.println("Hello is running");
				HelloAgent agent= new HelloAgent();
				
			}
}

 

 

 

1.Creating the MBean server and HTML adapter

 

 

The first step performed by the agent constructor is the  creation of the MBeanServer object.

 

by using the javax.management.MBeanServerFactory class

 

 

Notice that the parameter HelloAgent was passed to the factory’s create-

MBeanServer() method. This parameter is a String value indicating the name for

this agent’s domain.

 

2。Registering and managing the MBean

 

Once the adapter has been created, you need to register it on the MBeanServer

 

3。Uniquely identifying MBeans

 

create an instance of the javax.management.ObjectName class. The ObjectName class is a JMX class that provides a naming system for MBeans, allowing unique identification of MBeans registered in the MBean server.

 

Each ObjectName consists of two parts     

 

A domain name— The domain name usually coincides with the domain

name of the MBeanServer in which the MBean wants to register

 

A key=value property list—The object name may be the first representation a user will see of your MBean. You can supply information such as names, port values, locations, and purposes with a few property values.

 

4。Registering and starting the HTML adapter

 

call its start()

method

 

 

 

 

 

 

 

 

 

the domain name

does not even have to be specified. If it’s left blank,

the MBean server provides a default domain name.

The same is true for the MBeanServerFactory class.

If you use createMBeanServer() without a domain

name parameter, the factory will provide you with

an MBeanServer with a default domain.

 

 

 

 

 

 

 

 

 

what if you want to add additional MBeans to the agent

without writing more code? The Admin View is an HTML page presented by the

HTML adapter that gives you access to the agent’s MBean server in order to

remove or add MBeans. From this page, you can specify an ObjectName value and

associate it with a Java class that is an MBean. The MBean server will construct

and register an MBean corresponding to your input. The Admin View presents

four text fields

 

 

Domain—The HTML adapter defaults the Domain field to the domain of

the agent. Currently, it shows HelloAgent, which is your domain. This is

the first part of the object name.

 

 Keys—The Keys field requires input in the form [name=value],* . This field

represents the key/value portion of an object name.

 

 Java Class—This field requires a full Java class name of the class of the

MBean you want to create.

 

Class Loader—The Class Loader field is the only field that is optional. You

can specify a class loader for the MBean server to use when attempting to

load the Java class specified in the previous field.

 

 

 

Keys:                   name=helloWorld2

Java Class:  test.HelloWorld

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值