简单的MBean的实现

有两种Java MBean类型:静态MBean和动态MBean。

 

1.静态MBean

 

首先讨论静态MBean。要定义一个静态MBean,对于MBean接口类有一个约定:就是接口名必须以MBean为后缀,例如:HelloWorldMBean;然后对于这个接口的实现类名也有一个约定,就是接口名去掉MBean后缀,例如前面HelloWorldMBean对应的实现类名为HelloWorld。

 

在接口中,定义为以set或get方法开头的,被认为是一个属性值;其他方法被认为是一个操作。例如:

public interface HelloWorldMBean {

	
	public String getName();
	public void setName(String name);
	public String print();
	
	public int getAge();
	
	public void setSex(String sex);
}
 

以上定义的MBean中,有三个属性:name, age和sex;有一个操作:print。剩下的就是在实现类的实现对应的方法,这样就定义完一个MBean类型。

 

下面就是注册这个MBean。

 

		MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();

		HelloWorldMBean bean = new HelloWorld();

		ObjectName objectName = new ObjectName(
				"com.talend.liugang.jmx:type=HelloWorld,name=helloworld,number=1");

		mBeanServer.registerMBean(bean, objectName);
 

注册完以后,只要程序没有退出,你就可以通过运行JConsole,选择对应的进程,看到你刚实现的MBean了。

 

 

2.动态MBean

 

动态MBean是通过实现DynamicMBean接口来实现的。还是实现一个如上的MBean类型,现在我们需要如下实现:

 

public class HelloWorldDynamicMBean implements DynamicMBean {

	public Object getAttribute(String attribute)
			throws AttributeNotFoundException, MBeanException,
			ReflectionException {
		return null;
	}

	public AttributeList getAttributes(String[] attributes) {
		return null;
	}

	public MBeanInfo getMBeanInfo() {
		
		MBeanAttributeInfo[] attris = new MBeanAttributeInfo[]{
				new MBeanAttributeInfo("age","int","The age of mine",true,false,false,null),
				new MBeanAttributeInfo("name","java.lang.String","The name of mine",true,true,false,null),
				new MBeanAttributeInfo("sex","boolean","Male or Female",true,false,true,null)
		};
		
		MBeanConstructorInfo[] constructs = new MBeanConstructorInfo[]{
				new MBeanConstructorInfo("HelloWorld", "The helloworld constructor", new MBeanParameterInfo[0])
		};
		
		MBeanOperationInfo[] opers = new MBeanOperationInfo[]{
				new MBeanOperationInfo("print", "Print result", new MBeanParameterInfo[0], "java.lang.String", MBeanOperationInfo.ACTION)
		};
		return new MBeanInfo("com.talend.liugang.jmx.HelloWorld", "HelloWorld Mbean", attris, constructs, opers, null);
	}

	public Object invoke(String actionName, Object[] params, String[] signature)
			throws MBeanException, ReflectionException {
		return null;
	}

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

	}

	public AttributeList setAttributes(AttributeList attributes) {
		return null;
	}

}

 

这里剩下的很多方法我没有实现,其实主要的内容就在那个返回的MBeanInfo里,然后其他的方法都是对它的一个反馈,例如那些与Attribute相关的方法就是对我们MBeanInfo里定义的那些Attribute的操作;Invoke方法就是对MBeanInfo里定义的那些operation的操作。

 

剩下注册这个bean的方法和上面一样。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JMX(Java Management Extensions)是Java平台的一个管理和监控API,可以让开发人员开发Mbean(Managed Bean)来管理和监控Java应用程序。Mbean是一种Java对象,通过JMX可以公开其属性和方法,以便对其进行管理和监控。 下面是一个简单的示例,演示如何使用JMX开发一个Mbean: 1. 定义一个实现了DynamicMBean接口的类,该类表示一个Mbean: ```java public class SimpleMBean implements DynamicMBean { private String name; public SimpleMBean(String name) { this.name = name; } @Override public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException { if (attribute.equals("Name")) { return name; } else { throw new AttributeNotFoundException(attribute); } } @Override public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { throw new AttributeNotFoundException(attribute.getName()); } @Override public AttributeList getAttributes(String[] attributes) { AttributeList result = new AttributeList(); for (String attr : attributes) { try { result.add(new Attribute(attr, getAttribute(attr))); } catch (Exception ex) { // ignore } } return result; } @Override public AttributeList setAttributes(AttributeList attributes) { return new AttributeList(); } @Override public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException { throw new ReflectionException(new NoSuchMethodException(actionName)); } @Override public MBeanInfo getMBeanInfo() { MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[] { new MBeanAttributeInfo("Name", "java.lang.String", "name", true, false, false) }; MBeanConstructorInfo[] ctors = new MBeanConstructorInfo[] { new MBeanConstructorInfo("SimpleMBean()", "Default constructor", new MBeanParameterInfo[0]) }; return new MBeanInfo(getClass().getName(), "SimpleMBean", attrs, ctors, null, null); } } ``` 2. 创建一个MBeanServer对象,并将Mbean注册到MBeanServer中: ```java MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("com.example:type=SimpleMBean"); SimpleMBean mbean = new SimpleMBean("hello"); mbs.registerMBean(mbean, name); ``` 3. 使用JConsole或其他JMX客户端连接到MBeanServer,可以查看和修改Mbean的属性。 这是一个非常简单的示例,实际上,Mbean可以包含更复杂的逻辑和更多的属性。Mbean也可以使用注解来简化Mbean的开发和管理。JMX是一个强大的工具,可以帮助开发人员监控和管理Java应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值