JMX Notification Listener
August 9, 2014 . Comments
Tags: java, JMX, Monitoring, JVM
The Java notification framework is mainly used in the cache eviction space. It is a reliable way to determine wether a particular server is active and healthy as it would be constantly sending notifications and performance metrics using the JMX API.
The notification framework works in the publisher subscriber model. In this example we can see how the NotificationListener can be used to subscribe to messages from a JMX client.
The below code
The above is a simple example to print all the object names that are available in the application that runs and sends its JMX notifications on port 9999.
Create an RMI connector client and connect it to the RMI connector server
MBeanServer default domain = DefaultDomain
MBean count = 27
Query MBeanServer MBeans:
ObjectName = JMImplementation:type=MBeanServerDelegate
ObjectName = com.banyan.SimpleThreadPool:type=SimpleThreadPoolCounter
ObjectName = com.banyan.deserialize:type=TimeLapseCounter
ObjectName = com.banyan.get:type=TimeLapseCounter
ObjectName = com.banyan.put:type=TimeLapseCounter
ObjectName = com.banyan.serialize:type=TimeLapseCounter
ObjectName = com.sun.management:type=DiagnosticCommand
ObjectName = com.sun.management:type=HotSpotDiagnostic
ObjectName = java.lang:type=ClassLoading
ObjectName = java.lang:type=Compilation
ObjectName = java.lang:type=GarbageCollector,name=PS MarkSweep
ObjectName = java.lang:type=GarbageCollector,name=PS Scavenge
ObjectName = java.lang:type=Memory
ObjectName = java.lang:type=MemoryManager,name=CodeCacheManager
ObjectName = java.lang:type=MemoryManager,name=Metaspace Manager
ObjectName = java.lang:type=MemoryPool,name=Code Cache
ObjectName = java.lang:type=MemoryPool,name=Compressed Class Space
ObjectName = java.lang:type=MemoryPool,name=Metaspace
ObjectName = java.lang:type=MemoryPool,name=PS Eden Space
ObjectName = java.lang:type=MemoryPool,name=PS Old Gen
ObjectName = java.lang:type=MemoryPool,name=PS Survivor Space
ObjectName = java.lang:type=OperatingSystem
ObjectName = java.lang:type=Runtime
ObjectName = java.lang:type=Threading
ObjectName = java.nio:type=BufferPool,name=direct
ObjectName = java.nio:type=BufferPool,name=mapped
ObjectName = java.util.logging:type=Logging
This one spits out all the objects available and since I am not writing a general purpose JMX client. I want to listen to specific counters for notification. Now the enum that we defined for TimeLapseCounters will help us here as it holds an object for the deserialize method.
We will be adding our class for listening to Notification. This class simply spits out the error message it receives from a notification.
In order to listen to notifications we need to add the below two lines to our MiscNotificationListener class
The final output that includes the notification error messages is listed below
The javax.management package can be used in your application to listen to messages and act accordingly to resolve conflicts. Look for a blog on that soon.
Feel free to comment on the post but keep it clean and on topic.