JMX (Java Management Extensions, or Java Management Extensions) is a framework for embedding management functions for applications, devices, systems, and so on. JMX can flexibly develop seamlessly integrated systems, network, and service management applications across a range of heterogeneous operating system platforms, system architectures, and network transport protocols.
- The interface class used to publish the service Itesthellomxbean.java
[Codesyntax lang= "java" lines= "normal"]
Package org.suren.littlebird;/** * @author Suren * Interface name must end with Mxbean */public interface itesthellomxbean{public void Hello () ;p ublic String getName ();}
[/codesyntax]
- Interface implementation class for publishing a service Testhello.java
[Codesyntax lang= "java" lines= "normal"]
Package org.suren.littlebird;/** * @author Suren */public class Testhello implements itesthellomxbean{@Overridepublic void Hello () {System.out.println ("impl hello");} @Overridepublic String GetName () {return ' test get name ';}}
[/codesyntax]
- Service-Side test class Jmxservertest.java
[Codesyntax lang= "java" lines= "normal"]
Package Org.suren.littlebird;import Java.lang.management.managementfactory;import Java.rmi.registry.locateregistry;import Java.util.hashmap;import Java.util.map;import Javax.management.mbeanserver;import Javax.management.objectname;import Javax.management.remote.jmxconnectorserver;import Javax.management.remote.jmxconnectorserverfactory;import Javax.management.remote.jmxserviceurl;import javax.naming.context;/** * @author Suren * */public Class Jmxservertest { private static final String domain = "Suren"; //With Jconsole, you'll get more than one node in the MBean tab defined by the bread name private static final String package = " Org.suren.littlebird:type= "; public static void Main (String args[]) throws exception { locateregistry.createregistry (5006);//ports for remote connections mbeanserver Server = Managementfactory.getplatformmbeanserver (); &nbSp //Registering a callable Bean object testhello Testhello = new Testhello () ; objectname testhelloobjname = new ObjectName (package + "Testhello"); server.registermbean (Testhello, testhelloobjname); //register a Bean object that is available for remote connection jmxserviceurl serverurl = new Jmxserviceurl ("Service: jmx:rmi:///jndi/rmi://127.0.0.1:5006/"+domain); map<string, String> env = new hashmap<string, string> (); env.put (context.initial_context_factory , "Com.sun.jndi.rmi.registry.RegistryContextFactory"); jmxconnectorserver Connectorserver = Jmxconnectorserverfactory.newjmxconnectorserver (serverurl,env, server); server.registermbean (Connectorserver, new ObjectName (Package + "JMXCOnnectorserver ")); //Start service, this start action can also be jconsole to invoke connectorserver.start (); }}[/codesyntax]
- The client class used to test the service connection Jmxclienttest.java
[Codesyntax lang= "java" lines= "normal"]
Package Org.suren.littlebird;import Java.util.arrays;import Java.util.hashmap;import java.util.map;import Java.util.set;import Javax.management.mbeaninfo;import Javax.management.mbeanserverconnection;import Javax.management.objectinstance;import Javax.management.objectname;import Javax.management.remote.JMXConnector; Import Javax.management.remote.jmxconnectorfactory;import javax.management.remote.jmxserviceurl;/** * @ Author Suren * */public class jmxclienttest{ /** * @param args */ public static void Main (string[] args) throws exception { & nbsp string hostName = "127.0.0.1"; Publish the address of the JMX service int portnum = 5006; Port of the Publishing Service jmxserviceurl u = new Jmxserviceurl ("service:jmx:rmi:///jndi/rmi://" + hostName + ":" +Portnum + "/suren"); map<string, object> auth = new hashmap<string, Object > (); //Authentication Required user Information auth.put ( Jmxconnector.credentials, new string[]{"Controlrole", "123"}); jmxconnector Jmxconnector = Jmxconnectorfactory.connect (U, auth); mbeanserverconnection Connection = Jmxconnector.getmbeanserverconnection (); set<objectinstance > beans = Connection.querymbeans (null, NULL); for (objectinstance objinstance: Beans) { objectname Mbeanname = Objinstance.getobjectname (); mbeaninfo mbeaninfo = Connection.getmbeaninfo (mbeanname); system.out.println (Arrays.tostring (Mbeaninfo.getoperations ())); } }}[/codesyntax] Tomcat posted to me might be jmxrmi, the URI to access is Service:jmx:rmi:///jndi/rmi://localhost:5006/jmxrmi
- Java Authentication Configuration
Locate the configuration file: $JAVA _home\jre\lib\management\jmxremote.password Add the following configuration: Suren Suren Then modify the Jmxremote.password permissions for other users without permissions, chmod Jmxremote.password Find the profile: $JAVA _home\jre\lib\management\ Jmxremote.access Suren readwrite \ Create javax.management.monitor.*,javax.management.timer.* \ Unregister
If you want to access by remote (IP address + port), you need to add the following parameters at run time (how to add parameters can be found in this site):-dcom.sun.management.jmxremote.port=5006- Dcom.sun.management.jmxremote.ssl=false-dcom.sun.management.jmxremote.authenticate=true- djava.rmi.server.hostname=10.0.32.35
Java Management Extension JMX's Helloword Chapter