Using script shell plugin with jconsole

In Mustang (Java SE), jconsole has a plugin API. Plugins can extend jconsole by adding one or more tabs to the jconsole's GUI. Let us see how to use jconsole "script shell" plugin. Script shell is an interactive JavaScript shell tab within jconsole's GUI. This plugin is under the following directory:


$JDK_HOME/demo/scripting/jconsole-plugin 

I used Mustang build 89 for the following demo.

D:\\jdk1.6.0\\bin>java -version
java version "1.6.0-rc"
Java(TM) SE Runtime Environment (build 1.6.0-rc-b89)
Java HotSpot(TM) Client VM (build 1.6.0-rc-b89, mixed mode, sharing)

I need an application which will be monitored and managed by console. So, I start "Java2D" demo using the following command:

D:\\jdk1.6.0\\demo\\jfc\\Java2D>java -jar Java2Demo.jar

Now, I start jconsole with the following commands:

D:\\>cd D:\\jdk1.6.0\\demo\\scripting\\jconsole-plugin
D:\\jdk1.6.0\\demo\\scripting\\jconsole-plugin>jconsole -pluginpath jconsole-plugin.jar

Note that I've used -pluginpath to specify the paths of jconsole plugins to be used. In the above command, I just specify script-shell plugin alone. In the jconsole's connection dialog choose the Java2D application as application to be monitored and managed. After jconsole attaches to the Java2D process, you'll see that jconsole has an additional "Script Shell" tab. When you click on that it looks like this:

As you can see, I gave few JavaScript commands and got some output from the "script shell". So far so good - usual JavaScript expressions within jconsole's GUI (yes, echo is a built-in function to display within the screen). There is nothing special about it. What is the use of this shell for monitoring and management? Let us get some help - by typing help() in the prompt...

As you can see, there are many useful "built-in" functions for accesing MBeans (like mbean, mbeanInfo, getMBeanAttribute, setMBeanAttribute, invokeMBean, newPlatformMXBeanProxy) and other utility functions (like clear, exit etc.). The built-in functions are defined in


$JDK_HOME/demo/scripting/jconsole-plugin/src/resources/jconsole.js

There is also a built-in variable by the name jcontext of type JConsoleContext. From the variable, scripts can access current jconsole context (like MBeanServerConnection)

Now let us load a few pre-written JavaScript files in the script-shell by using load built-in function.

I've loaded $JDK_HOME/demo/scripting/jconsole-plugin/src/scripts/sysprops.js file in script shell and called sysprops() function defined in that file. This function prints all Java System properties from the managed application using RuntimeMXBean. There are few more sample scripts under:


$JDK_HOME/demo/scripting/jconsole-plugin/src/scripts

jtop.js - prints threads sorted by CPU time

js>load('src/scripts/jtop.js')
js>jtop()
time - state - name 
17.75 - WAITING - AWT-EventQueue-0 
2.078125 - RUNNABLE - RMI TCP Connection(7)-127.0.0.1 
1.515625 - RUNNABLE - DestroyJavaVM 
0.46875 - RUNNABLE - AWT-Windows 
0.25 - RUNNABLE - Attach Listener 
0.234375 - TIMED_WAITING - Intro 
0.140625 - TIMED_WAITING - Image Animator 0 
0.03125 - WAITING - Reference Handler 
0.015625 - WAITING - Java2D Disposer 
0 - WAITING - Finalizer 
js>

jstack.js - prints stack traces of all Java threads


js>load('src/scripts/jstack.js')
js>jstack()
32 - RMI TCP Connection(12)-127.0.0.1 - RUNNABLE 
	java.net.SocketInputStream.socketRead0(Native Method) 
	java.net.SocketInputStream.read(SocketInputStream.java:129) 	
java.io.BufferedInputStream.fill(BufferedInputStream.java:218) 	
java.io.BufferedInputStream.read(BufferedInputStream.java:237) 
	java.io.FilterInputStream.read(FilterInputStream.java:66) 	
sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:517)
sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:678)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:703)
	java.lang.Thread.run(Thread.java:619)
30 - TimerQueue - WAITING 
	java.lang.Object.wait(Native Method)
	javax.swing.TimerQueue.run(TimerQueue.java:233) 
	java.lang.Thread.run(Thread.java:619)
29 - RMI TCP Connection(7)-127.0.0.1 - RUNNABLE 
	sun.management.ThreadImpl.getThreadInfo0(Native Method) 
	sun.management.ThreadImpl.getThreadInfo(ThreadImpl.java:147)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 	
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	java.lang.reflect.Method.invoke(Method.java:589)
com.sun.jmx.mbeanserver.ConvertingMethod.invokeWithOpenReturn(ConvertingMethod.java:167)	
com.sun.jmx.mbeanserver.MXBeanIntrospector.invokeM2(MXBeanIntrospector.java:96)
com.sun.jmx.mbeanserver.MXBeanIntrospector.invokeM2(MXBeanIntrospector.java:33)
com.sun.jmx.mbeanserver.MBeanIntrospector.invokeM(MBeanIntrospector.java:208)
[... more output...]
js>

Note that you can access Java classes from JavaScript. Using that and the built-in variable jcontext, you can write many useful monitoring and management scripts.

For advanced users: Note that the jconsole script shell plugin uses javax.script API. It is possible to use scripting language of your choice (may be, you got JSR-223 engine for your language from http://scripting.dev.java.net) with jconsole. But, that is a topic for another blog! Or may be, you can just hack by looking at script shell sources in the demo directory!

Comments:

Post a Comment:
Comments are closed for this entry.
About

sundararajan

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
Links

No bookmarks in folder

Blogroll

No bookmarks in folder

News

No bookmarks in folder