Establish a JMX management system for web applications (1)
Very good explanation [reproduced]
The web application system has been developed. How to make the customer (the administrator of the web application system) easily manage my a bunch of configuration files, or how to implement the dynamic modification system running properties, but also make customers do not need too much understanding Can the content of the file be implemented? This is a problem that many system analysts that have just been developed in the previous Web application system. Or, I want to have an effective resource management that has already completed the web application system. While I want to add management functions, I don't need to do too much modifications to the original code. In other words, the management system and the managed application system Do very good isolation. JMX's management framework (Figure 1) is a good solution to these issues.
JMX (Java Management Extensions) is to manage networks, devices, applications and other resources, which describe an extensible management architecture and provides JMX API and some predefined Java management services. When writing this article, the latest version of JMX specification is V1.2 (http://jcp.org/aboutjava/communityprocess/final/jsr003/index3.html), the latest version of JMX reference implementation is V1.2.1 (http: // Java.sun.com/products/javaManagement/). After JMX is launched, some large items immediately use JMX-based implementation frameworks, such as Jakarta Tomcat and JBoss, which fully illustrates the feasibility and good characteristics of JMX. The management of web applications is often more troublesome, such as customers manually modify profiles, enable database monitoring, etc. If you want to dynamically modify the database access scheme or monitor the number of users, the dynamic modification log level will be more troublesome, and the system may The structure has made messy, causing harmful consequences, let alone scalability. JMX hierarchical structure and height componentization, by encapsulating various resources into MBean, so that we can have a high cost management solution for existing web applications. This article uses Tomcat as a web server as an example. Detailed explain how to use JMX to establish management of Web applications. There is a lot of related documents for JMX conceptual, architectural and use specifications, in order to better understand this paper, please refer to these documents when reading this article, the written ink will focus on applications and implementations. The following figure (Fig. 2) is a basic framework of JMX (see JMX specification), which is to understand this article.
1. Creating a web application to build a JMX-based management system for web applications, what do we need to do? 1. Create an instance of a MBean for each resource that needs to be managed, this is required by the JMX framework, there are two types of instances to choose from, one is MBean for direct management resources, one by calling resource instances MBean. 2. Write a MBean description file and describe each MBean, selecting an XML-based MBean description file is a nice decision. 3. Generate MBeanInfo by reading the MBean description file to generate one MBean. 4. Register the MBean that needs to be managed to MBean Server. 5. Write the client code, select the web, and compare the style of the web application, it is more easy to implement. Then a JMX-based web application has been shaped, Figure 3 is a basic structural diagram, a dashed portion is a JMX-based management system. Next, we implemented the entire management system in accordance with the steps.
II. Act of MBeanServer has two scenarios to obtain MBeanserver instance: 1. By obtaining the instance of the web server's MBeanserver, the benefits are parties yourself through the MBeanServer, and can even implement some of the yourself of the web server. Tomcat's management framework is also based on JMX. The JMX it uses is MX4J. This is a very good JMX open source project. In Tomcat4.1.27, the instance of MBeanServer is stored and the property name "org.apache.catalina Application Variables of .MBeanserver (Several ranges of variables in web applications: Page, Request, Session, Application), WESTERVERMANY WUNSERVER VEREXTERMICAL MBEANSERVER instances: server = (MBeanserver) GetServletContext (). GetaTtribute ("Org.apache .catalina.mbeanserver "); If you get this way, you get Server is Null, which means that you must do the following work, so that you can have permission to get the system's MBeanserver, Tomcat will only store the MBeanServer instance The system variable of the property name "org.apache.catalina.mbeanserver" under the web application. Find Tomcat under the confed directory to modify the server.xml file. Modify the Context element of the web application, add the privileged = "true" this property, for example: DocBase = "C: / Web /" Debug = "9" privileged = "true" reloadable = "true" crossContext = "true" /> 2. Create an instance of the cretembeanserver () of the MBeanServerFactory class in the JMX API, so that the benefit of the JMX's implementation is independent of the web server, so that the transplantability of the code is stronger. After creating MBeanServer, in order to get a reference to the MBeanServer in the management system, you can place it into the Application variable (recommended), or create and get it using the Singleton design mode. The code for creating MBean Server using JMX API is as follows: MBeanServer Server = mbeanserverfactory.creatembeanserver (); It is not very important to take MBeanServer, which can be considered for the convenience of implementation. II. Creating MBean in order to manage the resources of the web application, first allow resources to be managed, according to the JMX specification requirements, we put resource into MBEAN, actually adding manageability for web applications. After getting the instance of MBeanServer, you can write your own MBean. According to JMX specification, MBean has two main types of standard and dynamic, here is not described here, specifically see the JMX specification (http: //). There are two dynamics for special purposes MBeans: model MBean and open MBean. Model MBean (MODLE MBEAN) provides an "ready-to-see" MBean implementation, you can use it to quickly take any JMX manageable resources. It is a prefabricated, universal and dynamic MBean class, and provides reference implementation, which already contains all the necessary default behaviors - allows you to add or override those required to be customized at runtime. This allows Java-based, non-tool-based resources to provide a compatible MBean virtual package at runtime, enabling them to manage through the JMX architecture. In web applications, resources are diversified, running instances, static files, or even device or hardware status, then we can divide these resources into two categories, some resources need to perform dynamic management monitoring (such as login users, Database Connection Monitoring, Instant Log, etc.), some are static resources, just need static management (system configuration file, log level, etc.) to manage these different types of resources, which requires the MBEAN to directly call the running instance The interface is provided, and the other hand hope that MBean itself can provide static resource management, and the model MBean meets this requirement, and provides good flexibility and scalability for new resources that need to be managed. Therefore, the recommended model MBean is preferred as a web application. Of course, what kind of MBEAN is used in conjunction with different applications, or in combination of multiple types of MBean. By the way, use different types of MBean, which is transparent to the management client, that is, the client uses a consistent access method to access the MBean's properties and methods, which is an advantage of JMX. In order to better observe how to achieve management of static resources and dynamic resources, this article has written two MBEANs, respectively implement static and dynamic management of database connection properties. The purpose of static managing MBean is to implement the database configuration file modification, so when the web application is restarted, use the new database configuration, we use MBean's direct operation of the resource. Dynamic managing MBEAN's purpose is to modify the properties of the instantiated database connection object so that the database connection is changed without restarting the web application. 1. Implement the MBean for the management profile. JDBCCONFIG is a Model MBean, which is to encapsulate the config.properties file into MBean, including four properties and related Getter and SETTER and a SAVE method, where the role of the Save method is to store the configuration file, thus Implement the configuration file directly via MBean. The MBean in this example inherits the Basemodelmbean class, which implements the javax.management.modelmbean interface, we can find the Basemodelmbean class in Commons-Modeler.jar, Commons-Modeler.jar is an open source for use in Tomcat4.x. package. import javax.management.MBeanException; import javax.management.RuntimeOperationsException; import org.apache.commons.modeler.BaseModelMBean; import java.io *;. import java.util.Properties; public class JDBCConfigMBean extends BaseModelMBean {private String driver; private String username; private String password; private String dburl; private static final String CONFIG_FILEPATH = "c: //myweb//conf//config.properties"; /// // constructor /// public JDBCConfig () throws MBeanException, RuntimeOperationsException {super (); init ();} private void init () {InputStream in = null; try {Properties prop = new Properties (); in = new BufferedInputStream (new FileInputStream (CONFIG_FILEPATH)); prop.load (in); Driver = prop.getProperty; DBURL = Prop.GetProperty ("dburl"); username = prop.getProperty ("username"); password = prop.getProperty ("password"); in.close (); } catCH (Exception E) {try {if (in! = null) in .close ();} catch (ooException E1) {E1.PrintStackTrace ();} E.PrintStackTrace ();}} /// // getter and set //// ... /// // public way () {OutputStream out = null; tryoutputstream (new fileoutputstream (new fileoutputstream (config_filepath) ); Properties Prop = New Properties (); Prop.SetProperty ("DRIVER", DRIVER); Prop.SetProperty ("DBURL", DBURL); Prop.SetProperty ("UserName", UserName); Prop.SetProperty ("password" , PASSWORD; Prop.Store (out, "--- jdbc config ---"); out.close (); return "save surcess!";} catch (exception e) {try {ix (out! = null Out.close ();} catch (ioException e1) {e1.printStackTrace ();} E.PrintStackTrace (); return "save error!";}}} 2. Implement the properties of the modified run instance. The above example is a modification of static resource. If you need to dynamically modify the running class instance, Model Mbean also provides a good method, there is a setManageDResourced method in javax.management.modelmbean's API, this method's role It is to place the MODEL MBEAN that is running, so much in actually do. When you need to call the instance attribute, you only need to call the MBean's setAttribute, MBean will call the attribute setting method corresponding to the instance by reflection, is as simple as it is. But many people like to learn from the code, so I will write out how the properties code that implements dynamic modification instance is also written. Also in JDBC, this assumes that the system already has a database connection using JDBC, and now you need to change the attribute of JDBC, and create a new connection, which is not completed without restarting the Web application. The first code is a database connection class used by web applications, which provides two methods that get database connections and test connection properties, and related properties. When the web application is started, this class will instantiate, we temporarily call this instance of the resource instance. Package com.myapp.db; import java.sql.Connection; import java.sql.SQLException; public class DBAccess {private String driver; private String username; private String password; private String dburl; public Connection getConnection () {......} public static boolean TestConnection (String Driver, String Username, String Password, String DBURL) {} /// // getter and setter /// ... The second code is a Model MBean class, like the first Model MBean mentioned earlier, inherits BASEMODELMBEAN, which will have a reference to the web application runtime resource instance, because all attributes and methods are in the In the resource instance of the reference, the parent class BaseModelmbean completes the property setting method corresponding to the resource instance by reflection, so this class is quite simple, we can do some other related methods in it. Package com.myapp.db; import org.apache.commons.modeler.BaseModelMBean; import javax.management.MBeanException; import javax.management.RuntimeOperationsException; public class ResInstanceMBean extends BaseModelMBean {public ResInstanceMBean () throws MBeanException, RuntimeOperationsException {super ();} / ** other operations ** / .....} The third code is how to implement the running instance of the management database connection class ......