Here is some sample Java source code for a multithreaded JMX test that I created recently. After creating two other JMX example projects — a simple JMX Hello World tutorial project, and then a JMX standalone (Swing) project — I decided to create this multi-threaded JMX test application to see how JMX would work with an application that had multiple threads running simultaneously.
Our JMX MBean interface
Let's start looking at some code. First, here's the source code for a Java interface (essentially a JMX MBean interface) named HelloMBean
:
public interface HelloMBean { public void setMessage(String message); public String getMessage(); public void sayHello(); }
Implement the JMX MBean interface
Next, here's the source code for a concrete Java class named Hello
that implements the HelloMBean
interface (our JMX MBean interface):
public class Hello implements HelloMBean { private String message = null; public Hello() { message = "HI there"; } public Hello(String message) { this.message = message; } public void setMessage(String message) { this.message = message; } public String getMessage() { return message; } public void sayHello() { System.out.println(message); } }
A Java class that extends Thread
Next up, here's the source code for a Java class named PingPong
that extends the Java Thread
class:
public class PingPong extends Thread { String word; // what word to print int delay; // how long to pause int count; // number of iterations PingPong(String what, int time, int number) { word = what; delay = time; count = number; setName(what); } public void run() { try { for(int i=0;i < count;i++) { System.out.println(i+": "+word+":"+activeCount()); sleep(delay); // wait until next time } } catch (InterruptedException e) { return; // end this thread } } }
Our JMX agent (JMX service) class
Next, here's the source code for a Java class named SimpleAgent
that includes a main
method that kicks off this JMX agent (JMX service) application, and starts several PingPong
threads:
import javax.management.*; import java.lang.management.*; import javax.swing.*; public class SimpleAgent { private MBeanServer mbs = null; public SimpleAgent() { // Get the platform MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); // Unique identification of MBeans Hello helloBean = new Hello(); helloBean.setMessage("HELLO - HELLO - HELLO"); ObjectName helloName = null; try { // Uniquely identify the MBeans and register them with the platform MBeanServer helloName = new ObjectName("SimpleAgent:name=SwingHelloThere"); mbs.registerMBean(helloBean, helloName); } catch(Exception e) { e.printStackTrace(); } } public static void main(String argv[]) { SimpleAgent agent = new SimpleAgent(); System.out.println("SimpleAgent is running..."); new PingPong("ping", 2000, 1000).start(); new PingPong(" pong", 5000, 500).start(); new PingPong(" ding", 4000, 500).start(); new PingPong(" dong", 3000, 500).start(); new PingPong("PONG", 2800, 500).start(); } }
A shell script to start our JMX service
And finally here's a Unix/Linux shell script named run.sh
that I wrote to start this sample multithreaded JMX service application:
#!/bin/sh java -Dcom.sun.management.jmxremote \ -Dcom.sun.management.jmxremote.port=1616 \ -Dcom.sun.management.jmxremote.authenticate=false \ -Dcom.sun.management.jmxremote.ssl=false \ SimpleAgent
To run all of this code, save the source code shown here to the appropriate files (HelloMBean.java
, Hello.java
, PingPong.java
, and SimpleAgent.java
, and run.sh
), compile them, and then run them with the run.sh
script.
Once the application is running you can then connect to it with the jconsole application (Sun's "JMX console" application) and view its JMX resources.