JMX之Notification

Notification起到了Mbean之间沟通桥梁的作用。JMX notification model 和 java event model 类似,将一些重要的信息,状态的转变,数据的变更传递给 notification listener。以使资源更容易管理。我想象中的Notification使用的场景是:一个服务器的MBean配置做更改了之后,可以通过Notification通知其他所有分布式的服务器也一同做更改。

JMX notification 由四部分组成:
1、  Notification
2、  Notification broadcaster
3、  Notification listerner
4、  Notification filter

下面的例子展示了两个MBean之间的通信:当执行SayHelloMBean的方法后,将一些状态信息传递到HelloMBean中,并且执行HelloMBean的相应的方法。

先创建HelloMBean、SayHelloMBean及其实现

package com.jmx.notification.demo;

public interface HelloMBean {
	public String getName();
	
	public void setName(String name);
	
	public void helloWorld();
	
	public void helloWorld(String name);
	
	public String getTelephone();
	
}

package com.jmx.notification.demo;

public class Hello implements HelloMBean {
	
	private String name;
	private String telephone ="1351201201";
	@Override
	public String getName() {
		return name;
	}

	@Override
	public void helloWorld() {
		System.out.println("print......"+name);

	}

	@Override
	public void helloWorld(String name) {
		System.out.println("print......."+name);

	}

	@Override
	public void setName(String name) {
		this.name = name;
		System.out.println("My value is set to "+name);

	}

	@Override
	public String getTelephone() {
		return telephone;
	}

}

package com.jmx.notification.demo;

public interface SayHelloMBean {
	
	public void hello();
}


SayHello在执行完自己的方法实现后,调用了Notification,传递了一个通知
package com.jmx.notification.demo;

import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;

public class SayHello extends NotificationBroadcasterSupport implements
		SayHelloMBean {

	private int seq = 0;
	
	@Override
	public void hello() {
		
		System.out.println("SayHello===========hello============");
		
		//create a notification(packet)
		Notification notification = new Notification("sayhello",// notification name
											this,//who send
											++seq,//sequence number
											System.currentTimeMillis(),//send time
											"Hello everybody.");//message content
		//send
		sendNotification(notification);
		
	}
}

创建NotificationListener,决定了其他MBean拿到SayHello传递的Notification后需要做什么

package com.jmx.notification.demo;

import javax.management.Notification;
import javax.management.NotificationListener;

public class SayHelloListener implements NotificationListener {

	@Override
	public void handleNotification(Notification notification, Object mbean) {
		//execute this method when listener receive notification
		System.out.println("Type=" + notification.getType()
							+ ";\nSource=" + notification.getSource()
							+ ";\nSequenceNumber=" + notification.getSequenceNumber()
							+ "\nSend time=" + notification.getTimeStamp()
							+ "\nMessage=" + notification.getMessage());
		
		if (mbean != null) {
			if (mbean instanceof Hello) {
				Hello hello = (Hello) mbean;
				hello.helloWorld(notification.getMessage());
			}
		}
	}

}

注册各个MBean,并且为SayHello添加Notificaiton监听器和消息要传递到的目的MBean

package com.jmx.notification.demo;

import java.lang.management.ManagementFactory;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;

import com.sun.jdmk.comm.HtmlAdaptorServer;

public class HelloAgent {
	public static void main(String[] args) throws MalformedObjectNameException, NullPointerException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
		
		//create mbean server
		MBeanServer server = ManagementFactory.getPlatformMBeanServer();
		
		//create object name
		ObjectName helloName = new ObjectName("jmx:name=hello");
		//create mbean
		Hello hello = new Hello();
		//register mbean and hello name
		server.registerMBean(hello, helloName);
		
		//create adaptor
		HtmlAdaptorServer adaptor  = new HtmlAdaptorServer();
		//create adaptor name
		ObjectName adaptorName = new ObjectName("jmxAD:name=adaptor,port=5050");
		//create adaptor, adaptor is just a form as show mbean. It has no relation to specific mbean.
		server.registerMBean(adaptor, adaptorName);
		
		//create a notification broadcaster
		SayHello sayHello = new SayHello();
		server.registerMBean(sayHello, new ObjectName("jmx:name=sayhello"));
		sayHello.addNotificationListener(new SayHelloListener(), null, hello);
		
		//SayHello notice Hello do sth. through Notification
		
		adaptor.setPort(9999);
		adaptor.start();
		
		System.out.println("....................jmx server start....................");
	}
}

和上一篇Standard MBean相同,访问http://localhost:9999/ ,这次点击作为broadcaster的MBean:SayHello,然后点击hello方法



在控制台输出中可以看到,SayHello将message传递给了Hello



整个代码的一个流程图如下:




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值