The Standard Mbean consists of an interface and an implementation class .
Naming conventions:
Interface: Xxxmbean
Implementation class: XXX
By convention, the name of the Mbean is xxx. Self-determined.
Each method in the interface defines an attribute or operation.
A method that begins with a GET or set: if it conforms to the getter or setter pattern, it defines an attribute, a return value type of getter, or a setter parameter type.
Other method: defines a operation.
Example:
Package Test.xue.mbean;
Public interface Hellombean {public
void SayHello ();
public int Add (int x, int y);
Public String getName ();
public int getcachesize ();
public void setcachesize (int size);
public void SetText ();
public void Settitle (String title);
}
The Hellombean interface defines 3 attributes and 3 operation:
Attributes
Name:readonly
Cachesize:readable and writable
Title:writable
Operations
SayHello
Add
SetText: does not conform to setter mode.
Implementation class:
Package Test.xue.mbean;
public class Hello implements Hellombean {
private final String name = ' My Hellombean ';
private int cacheSize = 1024;
Private String title;
@Override public
void SayHello () {
System.out.println ("Hello, My Hellombean");
}
@Override public
int Add (int x, int y) {return
x+y;
}
@Override public
String GetName () {return
name;
}
@Override public
int getcachesize () {return
cacheSize;
}
@Override public
void setcachesize (int size) {
this.cachesize = size;
System.out.println ("CacheSize changed:" + size);
}
@Override public
void SetText () {
System.out.println ("SetText called");
}
@Override public
void Settitle (String title) {
this.title = title;
System.out.println ("title changed:" + title);
}
Above, Hellombean and hello define an Mbean that allows you to manage specific resources through the operation and attributes of this mbean.
You can now create a JMX agent to manage resources through an mbean.
Package test.xue.mbean.management;
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;
Import Test.xue.mbean.Hello; public class Helloagent {public static void main (string[] args) throws Malformedobjectnameexception, Nullpointerexcep tion, instancealreadyexistsexception, mbeanregistrationexception, Notcompliantmbeanexception, InterruptedException
{Mbeanserver Server = Managementfactory.getplatformmbeanserver (); ObjectName name = new ObjectName ("Test.xue.mbean:type=hello"); Any name//objectname name = new ObjectName ("Test.xue.mbean22:type=hello2");
Also works Hello Mbean = new Hello ();
Server.registermbean (Mbean, name); System.out.println ("Waiting" for remote management...");
Thread.Sleep (Long.max_value); }
}
As above, each mbean requires a objectname,objectname naming convention:
Domain: (key=value) +
The Domain=test.xue.mbean,type=hello in the example can be arbitrarily specified.
This JMX agent can be tested by jconsole
Run: Jconsole
After the connection,
You can try to modify attributes, call operations, and note the console output.
If Javax.management.NotCompliantMBeanException appears:
Exception in thread ' main ' Javax.management.NotCompliantMBeanException:MBean class Test.xue.mbean.Hello2 does not Implement Dynamicmbean, neither follows the Standard MBean conventions (javax.management.NotCompliantMBeanException: Class Test.xue.mbean.Hello2 is not a JMX compliant Standard Mbean) nor the MXBean conventions (Javax.management.NotComplia NtMBeanException:test.xue.mbean.Hello2:Class Test.xue.mbean.Hello2 isn't a JMX compliant MXBean) at
Com.sun.jmx.mbeanserver.Introspector.checkCompliance (Unknown Source) at
Com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean (Unknown Source) at
Com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean (Unknown Source) at
Test.xue.mbean.management.HelloAgent.main (helloagent.java:24)
The possible causes are:
The name of the interface and implementation class does not conform to the naming specification: Xxxmbean, XXX.