A MBean
ia a managed java object, similar to a JavaBeans component,
that follows the design patterns mentioned in JMX specification.
An MBean can represent a device (printer etc.), an application, system objects,
service-oriented networks, or any resource, that needs to be managed
The JVM specification defines 5 types of MBean:
- Standard MBean
- Dynamic MBean
- Open MBean
- Model MBean
- MXBean
Demonstration of a Standard Bean
–
package com.thecodecache.mbeans;
/**
* MBean
*
* @author manoranjan.kumar
*/
public interface GameMBean {
public void playFootball(String clubName);
public String getPlayerName();
public void setPlayerName(String playerName);
}
package com.thecodecache.mbeans;
public class Game implements GameMBean {
private String playerName;
@Override
public void playFootball(String clubName) {
System.out.println(this.playerName + " playing football for " + clubName);
}
@Override
public String getPlayerName() {
System.out.println("Return playerName " + this.playerName);
return playerName;
}
@Override
public void setPlayerName(String playerName) {
System.out.println("Set playerName to value " + playerName);
this.playerName = playerName;
}
}
package com.thecodecache.mbeans;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
/**
* JMX Agent
*
* @author manoranjan.kumar
*/
public class Main {
public static void main(String[] args) {
System.out.println("main entry point");
try {
ObjectName objectName = new ObjectName("com.thecodecache.mbeans:type=basic,name=game");
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
server.registerMBean(new Game(), objectName);
} catch (MalformedObjectNameException | InstanceAlreadyExistsException | MBeanRegistrationException
| NotCompliantMBeanException e1) {
System.out.println("Exception Caught !!");
e1.printStackTrace();
}
int code;
try {
code = System.in.read(); // to stop the JVM from exit so that we could troubleshoot
System.out.println("user entered exit code: " + code);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Exit!");
}
}
Open up the JConsole
from the JDK /bin installation directory –
Eclipse Standard Console –
Eclipse Standard Console –
Reference: