package test;
public interface HelloWorldMBean {
public void setGreeting(String greeting);
public String getGreeting();
public void printGreeting();
}
package test;
public class HelloWorld implements HelloWorldMBean {
private String greeting=null;
public HelloWorld(){
this.greeting="hello,wrold i am a standard MBean";
}
public HelloWorld(String greeting){
this.greeting=greeting;
}
public void setGreeting(String greeting) {
// TODO Auto-generated method stub
this.greeting=greeting;
}
public String getGreeting() {
// TODO Auto-generated method stub
return greeting;
}
public void printGreeting() {
// TODO Auto-generated method stub
System.out.println(greeting);
}
}
package test;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import com.sun.jdmk.comm.HtmlAdaptorServer;
public class HelloAgent {
private MBeanServer mbs=null;
public HelloAgent(){
mbs=MBeanServerFactory.createMBeanServer("HelloAgent");
HtmlAdaptorServer adapter = new HtmlAdaptorServer();
HelloWorld hw= new HelloWorld();
ObjectName adapterName=null;
ObjectName helloWorldName=null;
try {
helloWorldName= new ObjectName("HelloAgent:name=helloWorld");
mbs.registerMBean(hw, helloWorldName);
adapterName=new ObjectName("HelloAgent:name=htmladapter,port=9092");
adapter.setPort(9092);
mbs.registerMBean(adapter, adapterName);
adapter.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String args[]){
System.out.println("Hello is running");
HelloAgent agent= new HelloAgent();
}
}
1.Creating the MBean server and HTML adapter
The first step performed by the agent constructor is the creation of the MBeanServer object.
by using the javax.management.MBeanServerFactory class
Notice that the parameter HelloAgent was passed to the factory’s create-
MBeanServer() method. This parameter is a String value indicating the name for
this agent’s domain.
2。Registering and managing the MBean
Once the adapter has been created, you need to register it on the MBeanServer
3。Uniquely identifying MBeans
create an instance of the javax.management.ObjectName class. The ObjectName class is a JMX class that provides a naming system for MBeans, allowing unique identification of MBeans registered in the MBean server.
Each ObjectName consists of two parts
A domain name— The domain name usually coincides with the domain
name of the MBeanServer in which the MBean wants to register
A key=value property list—The object name may be the first representation a user will see of your MBean. You can supply information such as names, port values, locations, and purposes with a few property values.
4。Registering and starting the HTML adapter
call its start()
method
the domain name
does not even have to be specified. If it’s left blank,
the MBean server provides a default domain name.
The same is true for the MBeanServerFactory class.
If you use createMBeanServer() without a domain
name parameter, the factory will provide you with
an MBeanServer with a default domain.
what if you want to add additional MBeans to the agent
without writing more code? The Admin View is an HTML page presented by the
HTML adapter that gives you access to the agent’s MBean server in order to
remove or add MBeans. From this page, you can specify an ObjectName value and
associate it with a Java class that is an MBean. The MBean server will construct
and register an MBean corresponding to your input. The Admin View presents
four text fields
Domain—The HTML adapter defaults the Domain field to the domain of
the agent. Currently, it shows HelloAgent, which is your domain. This is
the first part of the object name.
Keys—The Keys field requires input in the form [name=value],* . This field
represents the key/value portion of an object name.
Java Class—This field requires a full Java class name of the class of the
MBean you want to create.
Class Loader—The Class Loader field is the only field that is optional. You
can specify a class loader for the MBean server to use when attempting to
load the Java class specified in the previous field.
Keys: name=helloWorld2
Java Class: test.HelloWorld