Directory
- Basic concepts
- Types of Mbean
- Standardmbean
- Dynamicbean
- Modelmbean
- How JMX is implemented
- How to use Standardmbean
- How JMX services are accessed
- Jmx--notifications
Basic concepts
JMX (Java Management Extensions) is a framework for embedding management functionality into applications.
JMX lets the program have managed functions, such as you develop a Web site, it is running 24 hours uninterrupted, then you will certainly monitor the site, such as the daily UV, PV is how much, or during the peak of the business, you want to limit the interface, you have to modify the interface concurrency configuration values.
The JMX framework is composed mainly of the following layers:
- Probe layer (Probe level)/device layer (instrumentation level)
- Proxy layer (Agent level)
- Remote management (Management level)
The relationship between them is as follows:
A program that needs to obtain managed object information through a JMX service, which interacts with a specific mbean through a remote management layer.
Describe some of the more important concepts separately:
is typically a Java class that provides an interface that enables the class to have functionality that can be managed (successfully registered on the server). It usually represents a program that we want to actually manage, or a device, and so on, which we encapsulate with the specification of Mbean.
is a Java class that manages the Mbean, you need to register an Mbean with Mbeanserver, the Mbean will really have the ability to be managed, Mbeanserver also provides the function of querying and registering listeners, Sun provides only interfaces, different JMX real The existing MBean server implementations are also different.
The Agent is a container for the Mbean Server, primarily to manage a range of Mbean, and provides a range of services, usually with MBean relationships, dynamically loading classes, simple monitor ING services, timers. Agents can make use of Protocol adapters (such as HTTP and SNMP) and connectors (RMI and Jini) to enable different clients to access the MBean.
- Protocol Adapters and connectors
Adapters and connectors make it possible for different protocols and clients to use this agent, and one agent can have multiple Protocol adapters and connectors, which makes it easier to manage the MBean (there are several types of clients and protocols that can operate MBean). Note that Protocol adapters and connectors are often also MBean.
"Jmx Learning" 1, Introduction to JMX
JMX Super-Detailed interpretation
Types of Mbean
- Standard Mbean: The Mbean interface needs to be defined, the interface contains the resources that need to be managed, and the implementation class of the Mbean interface is written. The name of the interface and implementation class must meet certain specifications, and if the implementation class name is classname, the name of the interface must be Classnamembean.
- Dynamic MBean: The Javax.management.DynamicMBean interface must be implemented, all properties, methods are defined at run time.
- Open Mbean: The specification of this mbean is not perfect and is being improved
- Model Mbean: Compared to standard and dynamic Mbean, you don't have to write an Mbean class, just use Javax.management.modelmbean.RequiredModelMBean. Requiredmodelmbean implements the Modelmbean interface, and Modelmbean expands the Dynamicmbean interface, so similar to Dynamicmbean, the management resources of Model Mbean are defined at run time. Unlike Dynamicmbean, the resources managed by Dynamicmbean are generally defined in Dynamicmbean (the runtime decides to manage those resources), and the resources managed by the model Mbean are not in the Mbean, but externally (usually a class). It is added to the model Mbean by the set method only at run time.
JMX Learning Notes (i)-mbean
Standardmbean
Start from scratch jmx (i)--Introduction and Standard MBean
JMX Study Notes (iii): MXBean
Mxbean supports class attribute management for more complex data types than Mbean
Dynamicbean
Dynamic Mbean:dynamicmbean
Modelmbean
Start from scratch JMX (iii)--model MBean
How to use JMX to implement Standardmbean method
- Defining an Mbean interface
- Writing an Mbean interface implementation class
- Create the Mbeanserver class and register the Mbean class with the server (Server.registermbean (Implementation class object, ObjectName)) "ObjectName name is canonical, in the format:" Domain name: Name=mbean names ", where the names of domain names and Mbean can be arbitrarily taken. Once this is defined, it is possible to uniquely identify the implementation class of the Mbean that we have defined. 】
- To see the implementation status, you can add a thread.sleep (long.max_value) and view it with the Jconsole tool.
How JMX services are accessed
JMX services can be connected and used in a variety of ways, as well as in different ways, but the content is the same.
1.JDK Self-jconsole tool (Jdk/bin/jconsole.exe)
Connect to the JMX service first.
The JMX service is either locally available or remotely available, and the local offer can be displayed directly on the Jconsole connection interface, with the connection selected. Remote provided connection, you need to enter the remote address and port number, and if you set the account password also need to enter the account password, and then connect.
- After a successful connection, you can see an mbean, such as a file directory, that you can click to expand, view properties, or call methods in real time (non-attribute related Get/set methods implemented in the Mbean interface implementation class can be called).
2. Access via the tools page provided by JMX
Here you need to import the external jar package JDMK
package jmx;import java.lang.management.ManagementFactory;import javax.management.JMException;import javax.management.MBeanServer;import javax.management.ObjectName;import com.sun.jdmk.comm.HtmlAdaptorServer;public class HelloAgent{ public static void main(String[] args) throws JMException, Exception { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); ObjectName helloName = new ObjectName("jmxBean:name=hello"); //create mbean and register mbean server.registerMBean(new Hello(), helloName); //这里需要对Html适配器的MBean进行注册,并启动适配器,这样就可以通过网页来使用JMX。 ObjectName adapterName = new ObjectName("HelloAgent:name=htmladapter,port=8082"); HtmlAdaptorServer adapter = new HtmlAdaptorServer(); server.registerMBean(adapter, adapterName); adapter.start(); }}
Access address: http://localhost:8082, click Name=hello:
3. Remote access via client program (RMI)
Of course, you need to start the JMX service and listen to the port before it can be accessed.
Package Jmx;import Java.io.ioexception;import Javax.management.attribute;import Javax.management.mbeanserverconnection;import Javax.management.mbeanserverinvocationhandler;import Javax.management.objectname;import Javax.management.remote.jmxconnector;import Javax.management.remote.jmxconnectorfactory;import Javax.management.remote.jmxserviceurl;public class Client{ public static void Main (string[] args) throws IOException, Exception, nullpointerexception {/** * JMX provided The URL of the service * @type {jmxserviceurl} */jmxserviceurl URL = new Jmxserviceurl ("Service:jmx:rm I:///jndi/rmi://localhost:9999/jmxrmi "); /** * JMX Service Connector for initiating connection * @type {[Type]} */Jmxconnector JMXC = jmxconnectorfactory.connect (u Rl,null); /** * Mbeanserver Connector obtained from the JMX service connector. * @type {[Type]} */Mbeanserverconnection MBSC = Jmxc.getmbeanserverconnection (); The name of the objectname is consistent with the previous registration time ObjectName mbeanname = new ObjectName ("Jmxbean:name=hello"); System.out.println ("Domains ..."); Get the domain name under all servers string[] domains = Mbsc.getdomains (); for (int i=0;i<domains.length;i++) {System.out.println ("domain[" + i + "]=" + domains[i]); }//Get all the number of Mbean registered on the server System.out.println ("Mbean count =" + Mbsc.getmbeancount ()); Sets a specific property value for the specified Mbean//The setattribute, getattribute operation can only be performed on the Bean's properties//For example, GetName or SetName, only using name, the prefix of the method needs to be removed Mbsc.setattribute (Mbeanname, New Attribute ("Name", "Hangzhou")); Mbsc.setattribute (Mbeanname, New Attribute ("Age", "1990")); String age = (string) mbsc.getattribute (Mbeanname, "age"); String name = (string) mbsc.getattribute (mbeanname, "name"); System.out.println ("age=" + Age + "; name=" + name); /** * Method of invoking an Mbean instance via JMX *//Method one//by getting proxy call MThe method of the bean instance Hellombean proxy = Mbeanserverinvocationhandler. Newproxyinstance (MBSC, Mbeanname, Hellombean.class, false); Proxy.helloworld (); Proxy.helloworld ("Migu"); Proxy.gettelephone (); Method Two//method of invoking an Mbean instance via the MSBC invoke method, only for non-attribute related methods (Get/set)//For example, invoke cannot invoke GetName method Mbsc.invoke (Mbea Nname, "Gettelephone", NULL, NULL); Mbsc.invoke (Mbeanname, "HelloWorld", New string[]{"I ll connect to JMX Server via Client2"}, new string[]{"JAV A.lang.string "}); Mbsc.invoke (Mbeanname, "HelloWorld", NULL, NULL); }}
Jmx--notifications
Starting from zero to play JMX (ii)--condition
JMX Learning Note (iv): Notifications
The JMX API defines a mechanism that enables an mbean to generate notifications, such as when a signal state changes, an event is detected, or a problem occurs.
The notification model only involves event propagation between the management artifacts in the same JMX agent. The JMX notification model relies on several parts:
- Notification, a generic event type that identifies the type of event, can be used directly, or can be extended according to the needs of the passed event.
- Notificationlistener interface, the object that accepts the notification needs to implement this interface.
- The Notificationfilter interface, as an object of the notification filter, implements this interface, providing a filter for notifying listeners of a filter notification.
- The Notificationbroadcaster interface, which notifies the sender to implement this interface, allows listeners who wish to be notified to register.
Send a generic type of notification that any listener will receive. Therefore, the listener needs to provide a filter to select the notification to be accepted. Any type of Mbean, standard or dynamic, can be used as a notification sender or as a notification listener, or both.
To generate a notification, the Mbean must implement the Notificationemitter interface or inherit the Notificationbroadcastersupport class. To send a notification you must construct an instance of the notification class, or a subclass (such as Notificationchangedattribute), after the instance is notified, Notifications are sent by Notificationbroadcastersupport's SendNotification method.
Step 1: Define the Hellombean interface
package com.jmx.demo5; public interface HelloMBean { public void sayHello(); public int add(int x, int y); public String getName(); public int getCacheSize(); public void setCacheSize(int size); }
Step 2: Write the implementation class Hello for Hellombean
Package Com.jmx.demo5; Import javax.management.AttributeChangeNotification; Import Javax.management.MBeanNotificationInfo; Import javax.management.Notification; Import Javax.management.NotificationBroadcasterSupport; MBean must extends Notificationbroadcastersupport or implements Notificationemitter public class Hello extends Noti Ficationbroadcastersupport implements Hellombean {private final String name = "Reginald"; private int cacheSize = Default_cache_size; private static final int default_cache_size = 200; Private long sequencenumber = 1; public void SayHello () {System.out.println ("Hello, World"); } public int Add (int x, int y) {return x + y; } public String GetName () {return this.name; } public int getcachesize () {return this.cachesize; } Public synchronized void setcachesize (int size) {int oldsize = this.cachesize; this.cachesize = size; System.out.println ("Cache size Now" + this.cachesize); /** * To send a notification,you need to construct an instance of class * notification or a SUBC Lass (such as attributechangednotification), and * Pass the instance to Notificationbroadcastsupport.sendnotifi cation. * */Notification n = new Attributechangenotification (This, sequencenumber + +, System.currenttimemillis (), "CacheSize changed", "CacheSize", "int", Oldsize, This.cachesize); SendNotification (n); } @Override Public mbeannotificationinfo[] Getnotificationinfo () {string[] types = n EW string[] {attributechangenotification.attribute_change}; String name = AttributeChangeNotification.class.getName (); String Description = "An attribute of this MBean have changed"; Mbeannotificationinfo info = new Mbeannotificationinfo (types, name, description); return new mbeannotificationinfo[] {info}; } }
Step 3: Create the Mbeanserver, identify the Mbean, register the Mbean, and run the Main method:
package com.jmx.demo5; import java.lang.management.ManagementFactory; import javax.management.MBeanServer; import javax.management.ObjectName; public class Main { public static void main(String[] args) throws Exception{ MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("com.jmx.demo:type=Hello"); Hello mbean = new Hello(); mbs.registerMBean(mbean, name); System.out.println("Waiting forever..."); Thread.sleep(Long.MAX_VALUE); } }
You can then open the JMX service with Jconsole to see the results of the operation: the notification option has no value in its initial state, and when you click Subscribe, the notification entry is 0
! Pic
After changing the value of CacheSize, in the observe notification option, change to 1
Summarize:
Notification can be used to monitor any information about the Mbean method invocation, as long as the Mbean implementation class inherits the Notificationbroadcastersupport class, The SendNotification (Notification N) method inherited from Notificationbroadcastersupport is then called in the method that needs to be monitored, and the parameter of the method is a Notification instance, You need to pass in the relevant notification information.
JMX (Java Management Extension) Learning