JMX and ssh tunneling

Posted: November 24, 2010

How do you use JMX on a Java process over an ssh tunnel? Standard ssh port forwarding doesn't work because JMX uses RMI which negotiates a connection on an arbitrary port. So what to do?

I had this problem yesterday and found jmxterm, a command line JMX client. It worked great.

Installing jmxterm

First download jmxterm using wget:

wget http://downloads.sourceforge.net/cyclops-group/jmxterm-1.0-alpha-4-uber.jar

Connect to your JXM process

# first run the program.  it comes as a single JAR with no dependencies
$ java -jar jmxterm-1.0-alpha-4-uber.jar 

# 'open' starts a connection to a process.  use the standard host:port format
$>open somehost:8999

# 'help' will display available commands
$>help
#following commands are available to use:
about    - Display about page
bean     - Display or set current selected MBean.
...

List JMX beans

$>beans

#domain = Catalina:
Catalina:J2EEApplication=none,J2EEServer=none,WebModule=//localhost/,j2eeType=Servlet,name=default
Catalina:J2EEApplication=none,J2EEServer=none,WebModule=//localhost/,j2eeType=Servlet,name=jsp
Catalina:J2EEApplication=none,J2EEServer=none,WebModule=//localhost/,name=jsp,type=JspMonitor
Catalina:J2EEApplication=none,J2EEServer=none,WebModule=//localhost/StingrayTest,j2eeType=Servlet,name=default
...

List operations for a bean

$>info -b com.imprev:context=StingrayTest,group=queues,name=handleCreatePrintMailJobTrigger,scheduler=OmcUiInstallDeploy,type=schedulers
#mbean = com.imprev:context=StingrayTest,group=queues,name=handleCreatePrintMailJobTrigger,scheduler=OmcUiInstallDeploy,type=schedulers
#class name = com.imprev.mbeans.quartz.TriggerManagement
# attributes
  %0   - Description (java.lang.String, r)
  %1   - FullName (java.lang.String, r)
  %2   - JobClass (java.lang.String, r)
  %3   - JobGroup (java.lang.String, r)
  %4   - JobName (java.lang.String, r)
  %5   - JobState (java.lang.String, r)
  %6   - LastRun (java.util.Date, r)
  %7   - NextRun (java.util.Date, r)
  %8   - Priority (int, r)
  %9   - TriggerGroup (java.lang.String, r)
  %10  - TriggerName (java.lang.String, r)
  %11  - Volatile (boolean, r)
# operations
  %0   - void executeNow()
  %1   - void pause()
  %2   - void resume()

Execute an operation

$>run -b com.imprev:context=StingrayTest,group=queues,name=handleCreatePrintMailJobTrigger,scheduler=OmcUiInstallDeploy,type=schedulers executeNow  
#calling operation executeNow of mbean com.imprev:context=StingrayTest,group=queues,name=handleCreatePrintMailJobTrigger,scheduler=OmcUiInstallDeploy,type=schedulers
#operation returns: 
null

More Information

That's a quick overview of how to get in and execute an operation from the command line. Thanks to CyclopsGroup for writing this excellent tool.

More information available at: http://wiki.cyclopsgroup.org/jmxterm