Thursday December 22, 2005
A JMX MBean to visualize AWT events
Some MBean seem generic enough to be deployed in any Java application. Why not share them?
I recently developed an MBean that can be dropped in any Java
application. It will export AWT events in the JMX world. Using JMX
monitoring tools, you can visualize what is happening when the mouse is
moved, a button is pressed and much more. The today MBean code is
verbose and could be optimized but I think that it makes a start.
Your application doesn't need any particular modifications. Just
download
the MBean sources (Standard MBean class and interface), compile them and deploy the MBean in your
application.
1 Deploying the MBean
This MBean follows the singleton pattern. A single MBean instance is
deployed per application. Call
AWTEvents.getDefault in order to access the
singleton mbean. Then register it in an MBeanServer. Using JDK 5, you
can use the platform MBeanServer.
AWTEvents mbean = AWTEvents.getDefault();
ManagementFactory.getPlatformMBeanServer().registerMBean(mbean,new ObjectName(":type=AWTEvents");
Recompile and relaunch your application.
2 Monitoring thanks to JConsole
Connect
JConsole
to the running process. NetBeans users, have a look to JMX module
documentation to see how to automatically connect JConsole to your
running project.
Once JConsole is connected to your application, select
MBeans Tab then
AWTEvents mbean.
You should see something like:
For each AWT event (Invocation, Action,
Mouse, ...) an MBean attribute is exposed. It displays the number of time an
event has occurred.
An interesting thing to visualize is the attribute value plotting.
Double clicking on numerical values will make a plotter to be displayed.
If you double click on
NumMouseMotionEvents
attribute value and move mouse pointer on top of your application, you
should visualize something like :
You can do the same for any numerical attributes.
What about receiving a JMX notification when an AWT event occurs?
CurrentNotificationFilter,
writable attribute, allows you to express for which AWT event a JMX
notification is to be sent. To see the list of possible filters, double
click on
PossibleNotificationFilterValues
attribute.
You can provide multiple filters by using '|' as separator. Setting to
all filter will
make all AWT events to be translated into JMX Notifications.
Select
Notifications
Tab to visualize received notifications. First subscribe then play with
your app. You should received a lot of JMX Notifications.
Want to stop the translation ? Select
Operations Tab.
From here you can start the translation, stop it or reset attribute counters.
Have fun and be prepared to share your
MBeans!
Jean-Francois
Posted by jeanfrancoisdenise
( Dec 22 2005, 05:12:36 PM GMT+02:00 )
Permalink