Objective
JMX, the Java managemen Extensions, is a framework for embedding management functionality into applications. A standard set of agents and services, in fact, users can use these agents and services in any Java application to implement management.
Often in the process of programming some configuration information needs to be changed dynamically, I will have the following methods:
Write dead in Java code, change the Java code when the environment changes
Write in a configuration file named *.properties, using Java to read the configuration file
Today, however, we will introduce a high-level approach to managing configuration information, which is the introduction of JMX.
How to use JMX
JMX is a specification that provides a JMX interface in the JDK, so you can develop JMX-based code after you install the JDK.
JMX has a two-layer structure, and the components of each layer are as follows
Tool Layer
Mbeans
Notification Model: Notification, Notificationlistener
Mbeans and Data classes: Attribute, Operator
Agent Layer
MBean Server
Agent Service
Below to learn the log4j2 of JMX support
Jmx-log4j2
Log4j 2 started to support JMX. Statuslogger, Contextselector, and all loggercontexts, Loggerconfigs and appenders use Mbeans and can be remotely detected and controlled.
make JMX effective
By default, JMX is in effect. When log4j is initialized Statuslogger, Contextselector, and all loggercontexts, Loggerconfigs and appenders use MBeans. If you want to disable JMX when you start the JVM, you need to add log4j2.disable.jmx=true to the system configuration file.
Local monitoring
Local monitoring does not require any system configuration files to be modified. Monitor your app with the Jconsole tool that comes with Java, and when the JDK is installed and environment variables are configured, you can simply open the visual monitoring interface by executing the following command at the terminal.
Jconsole
Remote monitoring App
For the remote monitoring app to work, you need to add the configuration to the system configuration file before starting the JVM.
Com.sun.management.jmxremote.port=portnum
Where Portnum refers to the port of the remote application.
log4j2 Detection Kit
The following is a section of log4j Mbeans in Jconsole.
Visual Client
The log4j contains basic visual components that can monitor the Statuslogger output and remotely modify the log4j configuration. The client can be run as a standalone application or as a plug-in for Jconsole.
As a Jconsole plugin
Use the following command to start Jconsole
%java_home%\bin\jconsole-pluginpath \path\to\log4j-api-2.1.jar;\path\to\log4j-core-2.1.jar;\path\to\ Log4j-jmx-gui-2.1.jar
After executing the above command, you will be able to see the following interface containing the Log4j2 tab page
remote editing of log4j configuration information
This GUI tool also contains a simple editor for editing log4j configuration information. As shown
Run as a standalone application
Execute the following command
%JAVA_HOME%\BIN\JAVA-CP \path\to\log4j-api-2.1.jar;\path\to\log4j-core-2.1.jar;\path\to\log4j-jmx-gui-2.1.jar Org.apache.logging.log4j.jmx.gui.ClientGui <options>
The options above can be one of the following three items
It is important to note that the port number must be the same as the JMX port number specified when the app was launched
For example, when launching the application, specify the JVM parameter information as follows:
Com.sun.management.jmxremote.port=33445com.sun.management.jmxremote.authenticate= Falsecom.sun.management.jmxremote.ssl=false
The following command is executed immediately:
%JAVA_HOME%\BIN\JAVA-CP \path\to\log4j-api-2.1.jar;\path\to\log4j-core-2.1.jar;\path\to\log4j-jmx-gui-2.1.jar Org.apache.logging.log4j.jmx.gui.ClientGui localhost:33445
Here are the two examples of the pictures that are relevant:
This article is just a brief introduction, the use of the problems encountered in the process will be described later.
JMX learning and its application in log4j