Updated 2012-06-06 20:12:22 by tpoindex

Java Management eXtensions (JMX) makes it easy to add monitoring capabilities to your application running under the JTcl Interpreter.

First, you'll need to be sure to start JTcl with JVM flags that enable the builtin JMX server:
JAVA_OPTS="-Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote=true \
 -Dcomagement.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=0 -Dcom.sun.management.jmxremote.local.only=false \
export JAVA_OPTS

Here is a sample script that creates a Java MBean, registers it with the JMX server, and updates the value periodically:
package require hyde

# define a Java interface and static inner class that follows Java MBean
# conventions.  This example only allows a counter to be read.  JMX allows
# bean properties be set and  methods invoked.

hyde::jclass JMX {
   public interface JTclStatsMBean {
       public int getCounter();
   public static class JTclStats implements JTclStatsMBean {
       private int counter;
       public void setCounter(int i) {counter = i;}
       public int  getCounter()      {return counter;}

# create a new bean object
set tclstats [java::new hyde.JMX.JTclStats]

# get a reference to the JVM's JMX server
set mbs [java::call java.lang.management.ManagementFactory getPlatformMBeanServer]

# create a domain, key, and name for our bean
set obj [java::new  javax.management.ObjectName JTcl JTclStats bean_1]

# register our bean and object name with the jmx server
$mbs registerMBean $tclstats $obj

# now update the bean as needed by the application
for {set i 1} {$i < 100} {incr i} {
    $tclstats setCounter $i
    after 5000


To monitor your application, use VisualVM, jvisualvm or jconsole (the latter two are shipped with your Java JDK.) When using VisualVM or jvisualvm, you will need to add the VisualVM-MBeans plugin by accessing the Tools -> Plugins menu.

Here's what the above script looks like in VisualVM:

Monitoring an application on a remote machine requires either changing the -Dcom.sun.management.jmxremote.port= property to a known port instead of an ephemeral port (as specified by 0), or to run the jstatd program on the remote machine (also included in the Java JDK.)

To run jstatd, you will first need a Java policy file, e.g., jstatd.all.policy:
grant {
    permission java.security.AllPermission;

Start jstatd with the policy file:
jstatd  -J-Djava.security.policy=jstatd.all.policy  -J-Djava.rmi.server.hostname=  -J-Djava.net.preferIPv4Stack=true &

Replace the hostname ip address with the ip address of the machine on which jstatd is running (this works around an issue where the jstatd RMI registry might not be seen from another machine.) Using VisualVM, add a remote machine to see and monitor the applications running on that machine.