JMX
provide APIs to enable MBeans to generate notifications. By using this, you can
receive notifications whenever some operation is performed by MBean.
How to generate
notifications?
MBean
class must implement interface NotificationEmitter or extend
NotificationBroadcasterSupport. By calling the sendNotification method, you can
broad cast the notification to correspondent listeners.
How to receive
notifications?
By
adding notification listeners to MBean object, you can receive notifications.
Example
SystemStatistics mbean = new SystemStatistics();
mbean.addNotificationListener(new StatsListener(), null, null);
mbean.addNotificationListener(new StatsListener(), null, null);
Find
the below working application.
SystemStatisticsMBean.java
package com.sample.mbeans; public interface SystemStatisticsMBean { public int readCount(); public int incrementReadCount(); public int decrementReadCount(); public int writeCount(); public int incrementWriteCount(); public int decrementWriteCount(); }
SystemStatistics.java
package com.sample.mbeans; import javax.management.Notification; import javax.management.NotificationBroadcasterSupport; public class SystemStatistics extends NotificationBroadcasterSupport implements SystemStatisticsMBean { private int readCount = 0; private int writeCount = 0; private static int sequenctNumber = 1; public int getReadCount() { return readCount; } public void setReadCount(int readCount) { this.readCount = readCount; } public int getWriteCount() { return writeCount; } public void setWriteCount(int writeCount) { this.writeCount = writeCount; } @Override public int readCount() { return readCount; } @Override public int incrementReadCount() { readCount++; Notification notification = new Notification("com.sample.mbeans.SystemStatistics", this, sequenctNumber++, System.currentTimeMillis(), "Read Count is incremented to " + readCount); sendNotification(notification); return readCount; } @Override public int decrementReadCount() { return --readCount; } @Override public int writeCount() { return writeCount; } @Override public int incrementWriteCount() { return ++writeCount; } @Override public int decrementWriteCount() { return --writeCount; } }
StatsListener.java
StatsAgent.java
Application.java
package com.sample.listeners; import javax.management.Notification; import javax.management.NotificationListener; public class StatsListener implements NotificationListener{ @Override public void handleNotification(Notification notification, Object handback) { System.out.println("Receiving notification..."); System.out.println(notification.getType()); System.out.println(notification.getMessage()); } }
StatsAgent.java
package com.sample.agents; 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.sample.listeners.StatsListener; import com.sample.mbeans.SystemStatistics; public class StatsAgent { private MBeanServer mbs; private SystemStatistics mbean; public StatsAgent() throws MalformedObjectNameException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException { ObjectName name = new ObjectName("com.sample.mbean.interfaces:type=SystemStatisticsMBean"); mbean = new SystemStatistics(); mbs = ManagementFactory.getPlatformMBeanServer(); mbs.registerMBean(mbean, name); mbean.addNotificationListener(new StatsListener(), null, null); } }
Application.java
package com.sample.app; import com.sample.agents.StatsAgent; public class Application { public static void main(String[] args) throws Exception { new StatsAgent(); System.out.println("Waiting forever..."); Thread.sleep(Long.MAX_VALUE); } }
Run
the application.
Open
command prompt (or) terminal, execute jconsole command.
Select
‘com.sample.app.Application’ process, click on Connect button.
If
you see above kind of screen, click on ‘Insecure Connection’ button.
Go
to MBeans tab.
Select
‘incrementReadCount’ under Operations.
Click
on ‘incrementReadCount’ button.
You
can see return value of ‘incrementReadCount’ function.
In
the application console, you will get below notifications.
Waiting
forever...
Receiving
notification...
com.sample.mbeans.SystemStatistics
Read
Count is incremented to 1
Click
again on incrementReadCount button.
You
can see below messages in the console.
Receiving
notification...
com.sample.mbeans.SystemStatistics
Read
Count is incremented to 2
I
hope you understand the JMX notification mechanism
No comments:
Post a Comment