JMX Connectors
JMX has two kinds of Connector:
1, RMI Connector
2, JMXMP Connector (JMX Message Protocol Connector)
RMI Connector Supports RMI Transports, Java Remote Method Protocol (JRMP), and Inter-Object Request Broker Protocol (IIOP)
The following example will use the RMI Connector to establish a connection between Server and Client.
Server's main features:
1. Establish MBean Server
2, establish RMI Connector Server
CLIENT's main function:
1. Create RMI Connector
2, register a MBean through the RMI Connector in MBean Server
The files contained:
Server.java
SimpleStandardmbean.java
SimpleStandard.java
Clientlistener.java
Client.java
Operating environment:
First go to SUN to download two compressed packages:
1, JMX-1_2_1-bin
2, jmxremote-1_0_1-bin
The following procedures need the lib / *. Jar in these two packages.
Source program:
Server.java
Import javax.management.mbeanserver;
Import Javax.Management.mbeanserverFactory;
Import javax.management.remote.jmxConnectorServer;
Import javax.management.remote.jmxConnectorServerFactory;
Import javax.management.remote.jmxserviceurl;
Public class server {
Public static void main (String [] args) {
Try {
// instantiate the mbean server
//
System.out.println ("/ ncreate the mbean server);
MBeanServer MBS = mbeanserverfactory.creatembeanserver ();
// Create an RMI Connector Server
//
System.out.println ("/ NCREATE AN RMI Connector Server);
JMXServiceURL URL = New JMXServiceURL
Service: JMX: RMI: /// JNDI / RMI: // localhost: 9999 / server ");
JMXConnectorServer CS =
JMXConnectorServerFactory.newjmxConnectorServer (
URL, // URL
NULL, // Environment Map
MBS); // MBeanServer
// Start the RMI Connector Server
//
System.out.println ("/ NStart The RMI Connector Server);
CS.Start ();
System.out.println ("/ NRMI Connector Server SuccessFully Started);
System.out.println ("/ nwaiting for incoming connections ...");} catch (exception e) {
E.PrintStackTrace ();
}
}
}
In order to facilitate the case of SERVER, you can add HTMLADAPTERSERSERVER in Server.java to see the browser, the specific situation is in the MBeanServer MBS = mbeanserverFactory.creatembeanserver (); below Add code: It may be so unclear, write all the code directly Although more code make people feel uncomfortable:)
/// Changed Server.java
Server.java
/ *
* CREATED ON
2004-12-25
* @Author roson
* /
Import javax.management. *;
Import Javax.Management.mbeanserverFactory;
Import javax.management.remote. *;
Import jmxinaction.logger;
Import com.sun.jdmk.comm.htmladaptorserver;
Public class server {
Public static void main (String [] args) {
Try
{
System.out.println ("Create the Mbean Server");
MBeanServer MBS = mbeanserverfactory.creatembeanserver ();
System.out.println ("/ N / TCREATE, Register and Start A New HTML Adaptor:");
HTMLADAPTORSERVER HTML = New HTMLADAPTORSERVER ();
Objectname HTML_NAME = NULL;
Try
{
HTML_NAME = New ObjectName ("Logger: Name = HTML, Port = 8082");
System.out.println ("/ TOBJECT NAME =" HTML_NAME);
Mbs.Registermbean (HTML, HTML_NAME);
}
Catch (Exception E)
{
System.out.println ("/ t !!! could not create the html adaptor !!!");
E.PrintStackTrace ();
Return;
}
Html.start ();
System.out.println ("CREATE AN RMI Connector Server);
JMxServiceURL URL = New JMXServiceURL ("Service: JMX: RMI: /// JNDI / RMI: // LocalHost: 9999 / Server");
// jmxserviceURL URL = New JMXServiceURL ("JMXMP", NULL, 5555);
// jmxmp with different places in RMI Connector
JMXConnectorServer CS = JMXConnectorServerFactory.newjmxConnectorServer (
URL, // URL
NULL, // Environment Map
MBS); //mbeanservercs.start ();
}
Catch (Exception E)
{
E.PrintStackTrace ();
}
}
}
/ ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
After the change, you can check the situation of Server through the browser. HTTP: // localhost: 8082 is the browser address bar
SimpleStandardmbean.java
Public interface simplestandardmbean {
Public string getState (); // read status State
Public void setState (string s); // Write status State
Public int getnbchanges (); // status change
Public void reset (); //
}
SimpleStandard.java
Import javax.management. *;
Public Class SimpleStandard Extends NotificationBroadcastersupport
Implements SimpleStandMbean {
Private string state = "initial state";
Private Int nbchanges = 0;
Private int nbresets = 0;
Public string getState () {
Return State;
}
Public void setState (string state) {
THIS.STATE = State;
Nbchanges ;
}
Public int getnbchanges () {
Return nbchange;
}
Public void reset () {
AttributeChangenotification ACN = New AttributeChangeNotification
this, // source
0, // sequenceenumber
0, // TimeStamp
"Nbchange indeed", // message
"Nbchange", // attribute name
"Integer", // attribute type
New integer (nbchanges), // The value before the property changes
New integer (0)); // The value after the property changes
State = "Initial State";
Nbchanges = 0;
NBRESETS ;
SendNotification (ACN);
}
}
Clientlistener.java
Import javax.management. *;
Public Class ClientListener IMPLEments NOTIFICATENER {
Public void Handlenotification (Notification Notification, Object Handback) {
System.out.println ("/ NRECeived NOTIFICATION:" NOTIFICATION);
}
}
Client.java
Import javax.management.attribute;
Import javax.management.mbeanserverConnection; import javax.management.mbeanserverinvocationhandler;
Import javax.management.Objectname;
Import javax.management.remote.jmxconnector;
Import.Remote.jmxConnectorFactory;
Import javax.management.remote.jmxserviceurl;
Public class client {
Public static void main (String [] args) {
Try {
// Create An RMI Connector Client and
// Connect It to the RMI Connector Server
//
System.out.println ("/ Ncreate An RMI Connector Client and"
"Connect It to the RMI Connector Server");
JMXServiceURL URL = New JMXServiceURL
Service: JMX: RMI: /// JNDI / RMI: // localhost: 9999 / server ");
// jmxserviceURL URL = New JMXServiceURL ("JMXMP", NULL, 5555);
// JMXMP Connector Different from RMI Connector, at this time, the port number of JMXP Connector is 5555
JMXConnector JMXC = JMXConnectorFactory.Connect (URL, NULL);
// Get An MBeanSerConnection
//
System.out.println ("/ nget an mbeanserverConnection);
MBeanServerConnection MBSC = jmxc.getmbembeanserverConnection ();
// Get Domains from MBeanServer
//
System.out.println ("/ NDOMAINS:");
String Domains [] = mbsc.getdomains ();
For (int i = 0; i System.out.println ("/ TDOMAIN [" i "] =" Domains [i]); } // Create SimpleStandard MBean registered a MBean // Objectname mbeanname = new objectName ("MBeans: Type = SimpleStandard"); System.out.println ("/ NCREATE SIMPLESTANDARD MBEAN ..."); Mbsc.creatembean ("SimpleStandard", MBeanName, NULL, NULL); // Get MBean Count // System.out.println ("/ nmbean count =" mbsc.getmbeancount ()); // Get State Attribute // System.out.println ("/ nState =" mbsc.getattribute (MBeanName, "State")); // Set State Attribute // MBSC.SetAttribute (MBeanName, New Attribute ("State", "Changed State"); // Get State Attribute // // another way of interacting with a given mbean is throughg a // dedicated proxy instead of going Directly Through the mbean // Server Connection // SimpleStandMbean Proxy = (SimpleStandardmbean) MBeanServerInvocationHandler.newProxyInstance ( MBSC, MBeanName, SimpleStandardmbean.class, FALSE); System.out.println ("/ nState =" proxy.getState ()); // Add Notification Listener on SimpleStandard MBean // ClientListener Listener = New ClientListener (); System.out.println ("/ NADD NOTIFICATION LISTENER ..."); MBSC.AddNotificationListener (MBeanName, Listener, Null, NULL); // invoke "reset" in SimpleStandard MBean // // Calling "reset" Makes The SimpleStandard MBean Emit A // Notification That Will Be received by the registered // ClientListener. // System.out.println ("/ Ninvoke Reset () in SimpleStandard Mbean ..."); MBsc.invoke (MBeanName, "RESET", NULL, NULL); // Sleep for 2 second SECONDS in Order To Have Time To Receive T // Notification Before Removing The Notification Listener. // System.out.println ("/ NWAITI for Notification ..."); Thread.sleep (2000); // Remove Notification Listener on SimpleStandard MBean // System.out.println ("/ NREMOVE NOTIFICATION LISTENER ..."); MBsc.RemovenotificationListener (MBeanName, Listener); // Unregister SimpleStandard MBean // System.out.println ("/ NunRegister SimpleStandard Mbean ..."); Mbsc.unregistermbean (mbeanname); // Close MBeanServer Connection // System.out.println ("/ NClose The Connection To The Server); JMXc.Close (); System.out.println ("/ NBYE! BYE!"); } catch (exception e) { E.PrintStackTrace (); } } } Registering in the client This is not a domain name that you are in the command line, because it is first connected to Server, check all the domain names of Server, then register again. However, you can see the changes in the browser. Try it back:) Run the program: 1. Place several JAR packages mentioned in the operating environment in ClassPath, (I am doing eclipse, I feel very convenient) 2, compile. If it is RMI Connector, use RMIREGISTER: Run prompt line input: RMIREGISTRY 9999 JMXMP is not needed. 3, run server.java, then run client.java Excerpt from JMXRemote-1_0_1-bin / Doc / Tutorial