First, here's the source code for a Java MBean interface named HelloMBean:
public interface HelloMBean {
public String helloService(String name);
}
A class to implement the JMX MBean interface
Next, here's the source code for a Java class named Hello that implements the HelloMBean interface we just defined:
public class Hello implements HelloMBean {
@Override
public String helloService(String name) {
System.out.println(" Called ...");
return " Hello Mbean :"+name;
}
}
A JMX example class (a JMX service)
Next up, here's the source code for a Java class named SimpleAgent that includes a main method that starts up our JMX service application:
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.ObjectName;
public class SimpleAgent {
private MBeanServer mbs = null;
public SimpleAgent() {
// Get the platform MBeanServer
mbs = ManagementFactory.getPlatformMBeanServer();
// Unique identification of MBeans
Hello helloBean = new Hello();
ObjectName helloName = null;
try {
// Uniquely identify the MBeans and register them with the platform MBeanServer
helloName = new ObjectName("FOO:name=HelloBean");
mbs.registerMBean(helloBean, helloName);
} catch(Exception e) {
e.printStackTrace();
}
}
// Utility method: so that the application continues to run
private static void waitForEnterPressed() {
try {
System.out.println("Press to continue...");
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String argv[]) {
SimpleAgent agent = new SimpleAgent();
System.out.println("SimpleAgent is running...");
SimpleAgent.waitForEnterPressed();
}
}
A script to start our JMX service
And finally here's a Unix/Linux shell script named run.bat that I wrote to run this sample JMX application ("JMX service")
Run.bat
java -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=5555 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false SimpleAgent
As you can see from that code, my application will listen on port 5555 of whatever computer system it is run on.

Access our JMX service with JConsole
Once the application is running you can then connect to it with the jconsole application and view its JMX resources.
test your mbean by passing args.

