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(); }
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); } }
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()); } } } }
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...................."); } }
在控制台输出中可以看到,SayHello将message传递给了Hello
整个代码的一个流程图如下: