More on Troubleshooting with JMX
By daniel on Mar 15, 2006
A few days ago - I blogged about Tracing JMX - to see what's happening in the MBeanServer, and showed how to dynamically activate the JMX traces in a running Java application. I have taken this example a step further, and wrote a small command line console that uses JMX to let you:
- list known JVMs (VirtualMachine.list())
- attach to a JVM
- switch on/off console output traces in the target JVM
- list existing loggers and their levels in the target JVM
- change logger levels in the target JVM
all of this dynamically from a command-line tool.
For this all I had to do was to write a
JMXTracingCLI class - which simply reuses the
Activate, JMXTracing, and JMXTracingMBean classes I was using in my example last week. The JMXTracingCLI is a simple command line
interpreter that attach to existing VMs and then use JMX to tweak the
logging configuration in the target VM. If you create a NetBeans project (named tracing) with all these
classes, choose Java SE 6 as Java Platform, add Java SE 6 tools.jar to the
project libraries, and choose
com.sun.jmx.examples.util.tracing.JMXTracingCLI as Main class for this
project, NetBeans will create for you a dist/tracing.jar file that you
will be able to execute directly from the java command line.
[Note: you will need Java SE 6 for this, since the getAgentProperties method we use was added to Java SE 6 b65].
For instance this is an example of what it can do:
Start the command line tool
java -jar dist/tracing.jar
The following help is displayed:
available commands are: attach
list the known VMs:
Note: [inputs are in bold, output is regular] vmlist 22413 [/home/dfuchs/amos/jmx/tracing/dist/tracing.jar] 21646 [org.netbeans.Main --userdir /home/dfuchs/.netbeans/5.0 --branding nb]
I have two Java SE 6 VMs running on my machine: my tracing tool, and my NetBeans IDE [yes, I'm fearlessly using NetBeans with Java SE 6 - all you need to do is to start NetBeans with the appropriate --jdkhome switch].
I'm now going to attach to NetBeans IDE VM. But before I do so, in another terminal I'm going to display NetBeans log file:
tail -f /home/dfuchs/.netbeans/5.0/var/log/messages.log
Then I go back to my JMXTracingCLI and attach to NetBeans VM:
attach 21646 MLet registered: com.sun.jmx.examples.util.tracing:type=MLet JMXTracing registered: com.sun.jmx.examples.util.tracing:type=JMXTracing attached to: 21646 - console is off
OK, the tool tells me that console is off. So I'm going to register a new ConsoleHandler in the target VM.
console on console is on
I want to get swing loggers:
list .\*swing.\* javax.swing.BufferStrategyPaintManager javax.swing.SortingFocusTraversalPolicy
Hummm... Only two. Let's switch them to FINEST
switch .\*swing.\* FINEST javax.swing.BufferStrategyPaintManager: FINEST javax.swing.SortingFocusTraversalPolicy: FINEST console is on
If now run my mouse over NetBeans, I can see that the swing traces I just activated are dumped in the log:
[tail -f /home/dfuchs/.netbeans/5.0/var/log/messages.log] Mar 15, 2006 4:15:18 PM javax.swing.BufferStrategyPaintManager beginPaint FINEST: beginPaint Mar 15, 2006 4:15:18 PM javax.swing.BufferStrategyPaintManager endPaint FINEST: endPaint: region 604 114 628 138 Mar 15, 2006 4:15:18 PM javax.swing.BufferStrategyPaintManager beginPaint FINEST: beginPaint Mar 15, 2006 4:15:18 PM javax.swing.BufferStrategyPaintManager endPaint FINEST: endPaint: region 604 114 628 138
This example could be taken even further by e.g. improving the JMXTracingMBean
and JMXTracingCLI to make the target VM spit out its traces on a temporary
file on e.g. /tmp, instead of simply enabling Console output.
But this gives you a hint of what you can achieve with the power of JMX and Java SE 6 new APIs...