Dynamic mbean

zhaozj2021-02-16  85

JMX architecture understanding (part of the MBean)

Board bridge http://www.jdon.com 2003/1/5

The Hellombean, the previous chapter, is STANDARDMBEAN, which is a kind of mbean in JMX. There is also a kind of Dynamicmbean. This is the JMX to control it during operation, no matter which MBean, their difference is just how they have been developed, and Not in how they are managed, JMX's Agent provides the process of processing these two types of abstraction, that is, once these two MBeans are managed by JMX Agent, these two MBeans have no difference in the outside world.

The Java Class of a standard Standard MBean is managed by its Attributes and Operations, attributes are some of the internal attribute variables, which are managed by the Getter and Setter methods in Hellombean, they are

Public string getname (); public void setname;

Operations is the other way of being willing to be managed in this class, which is public void print in Hellmbean.

All of these methods need to be defined in the MBean interface, and then exposed to an agent via the Introspection (internal provincial mechanism). The above method is to manufacture the most direct way to manage resources.

Dynamicmbean

When developing a DynamicMbean, Attributes and Operations are industriously exposed to the agent through the method call. DynamicMbean is suitable for managing those already existing resources and code.

Need to be considered by JMX's Agent as Dynamicmbean, this Java Class or its subclass must inherit a standard interface of Dynamicmbean.

GetMbeanInfo: Dynamicmbean is not like Standardmbean, revealing his Attributes and Operations through the Introspection (within the internal provincial mechanism), but by returning this method in the outside, revealing his Attribute Names and Types and Operation Signatures. This method returns the MBeanInfo instance.

GetAttribute and GetAttribute are values ​​that get the corresponding Attribute Name property name or attribute name table. This similar Standardmbean's getter method is different from the caller provides the desired property name, such as public object gettribute (String Attribute_name) Unlike StandardMbean Getxxxx (attribute_name).

SetAttribute and SetAttributes Set the Name-Value pairs of the corresponding properties similar to the Setter method similar to the StandardMbean.

The InvokeInvoke method is to let the outside world to call the DynamicMbean operation, which is the type of operation that the operation name is Object as the parameter and the type of these parameters, for example: public object infhot, object params [], string signature [])

When Dynamicmbean is registered in the JMX Agent, the DynamicMbean is actually the same as StandardMbean. The outside world will get a management interface through GETMBEANInfo, which contains Attributes and Operations name, and then call DynamicMBean's getters, setters and invoke methods,

The MBean original class can be seen from above, and MBean has some original classes such as MBeanInfo, a total of several:

MBeanInfo - Ros listed all attributes attribute, operation Operations, constructor constructor and notification

MBeanfeatureInfo - subclass of the following class

MBeanattributeInfo - Describe an attribute attribute

MBeanconstructorInfo - Describes a constructor The constructor MBean constructor is individually defined by the signature, which is the order and type of the parameters.

MBeanOperationInfo - Describe an operation Operation

MBeanParameterInfo - Parameters describing an operation or constructor

MBeanNotificationInfo - Description a Notification

By using these original classes, your specific resources can be dynamically embedded in Dynamicmbean.

Let's take a look at how the Hellodynamic program uses these original classes to form a DynamicMbean:

The Hello in the above chapters as an example, Hello is a STANDARD MBEAN to implement the assignment and print function of Name. Then use the Dynamic MBean we also implement this feature, in this resource, there is an attribute Name and the operation method. The following example shows how this property and this operation method use the original class of Dynamicmbeans:

// import javax.management *;. Import java.lang.reflect.Constructor; import java.util.Iterator; / *** @author Sunny Peng * @version 1.0 * / public class HelloDynamicimplements DynamicMBean {// This is our attribute name private String name = ""; private MBeanInfo mBeanInfo = null; private String className = this.getClass () getName ();. private String description = "Simple implementation of a dynamic MBean";. private MBeanAttributeInfo [] attributes = new MBeanAttributeInfo [1]; private MBeanConstructorInfo [] constructors = new MBeanConstructorInfo [1]; private MBeanOperationInfo [] operations = new MBeanOperationInfo [1]; MBeanNotificationInfo [] mBeanNotificationInfoArray = newMBeanNotificationInfo [0]; public HelloDynamic () {// set a property Attributes [0] = New MBeanattributeInfo ("Name", "Java.lang.String", "Name: Name String.", true, true, false; // Setting the constructor constructor [] thisconstructors = this.getClass ( ) .Getconstructors (); constructors [0] = new mbeanconstructorinfo ("Hellodynamic (): constructs a hellodynamic object" Thisconstructors [0]); // Operate method Our method is Print MBeanparameterInfo [] params = null; Operations [0] = new mbeanoperationinfo ("print", "Print (): print the name", params, "Void ", MBeanOperationInfo.INFO); mBeanInfo = new MBeanInfo (className, description, attributes, constructors, operations, mBeanNotificationInfoArray);} public Object getAttribute (String attribute_name) {if (attribute_name.equals (" name ")) return name; return null } public attributelist getttributes (string [] attributenames) {attributelist resultlist = new attributelist ();

// if attributeNames is empty, return an empty result list if (attributeNames.length == 0) return resultList; for (int i = 0; i

} Public Object invoke (String operationName, Object params [], String signature []) throws MBeanException, ReflectionException {// Check for a recognized operation name and call the corresponding operation if (operationName.equals ( "print")) {// our method of operation embodied print System.out.println ( "Hello," name "!, this is HellDynamic"); return null;} else {// unrecognized operation name: throw new ReflectionException (new NoSuchMethodException (operationName) "Cannot Find the Operation" OperationName "IN" ClassName);}} public mbeanInfo getmbembeanInfo () {return mBeanInfo;}} to call this hellodynamic:

// java imports // Import java.util. *; import java.io. *; import java.net. *; // ri imports // Import Javax.Management.ObjectName; import javax.management.mbeanserver; import javax. management.MBeanServerFactory; import javax.management.MalformedObjectNameException; import com.sun.jdmk.comm.HtmlAdaptorServer; import javax.management.Attribute; import com.sun.jmx.trace.Trace; public class HelloAgent {public HelloAgent () {} public static void main (String [] args) {// CREATE the MBeanServer // System.out.println ( "/ n / tCREATE the MBeanServer."); MBeanServer server = MBeanServerFactory.createMBeanServer (); String domain = server.getDefaultDomain (); ObjectName object_name = null; // use HelloDynamic If Hello is our last chapter describes StandardMbean the String mbeanName = "HelloDynamic"; try {object_name = new ObjectName (domain ": type =" mbeanName); server .creatembean (MBeanName, Object_name); Attribute StateAttribute = New Attribute ("Name", "I am big one"); server.setttribute (Object_name, Stateatt Ribute); string name = (string) server.getattribute (object_name, "name"); system.out.println ("name now" name); Server.invoke (Object_name, "Print", null, null; } Catch (exception e) {system.out.println ("/ t !!! could not create the hello agent !!!"); E.PrintStackTrace (); return;}}} can be output directly on the screen output Get "Hello, I am Big One, this is from hellodynamic"

转载请注明原文地址:https://www.9cbs.com/read-13248.html

New Post(0)