jconsole jmx_什么是JMX? 10分钟快速入门JMX教程(MBean,JConsole)

卢文博
2023-12-01

jconsole jmx

Java Management Extensions (JMX) was introduced in J2SE 5.0 release. It provides an architecture to manage resources dynamically at runtime. JMX is used mostly in enterprise applications to make the system configurable or to get the state of application at any point of time.

J2SE 5.0版本中引入了Java管理扩展( JMX )。 它提供了一种架构,可在运行时动态管理资源。 JMX主要用于企业应用程序中,以使系统可配置或在任何时间获取应用程序的状态。

To manage any resource through JMX, we need to create Managed Beans (MBeans) and then register it to the MBean Server. MBean server works as a management agent for all the MBeans registered.

要通过JMX管理任何资源,我们需要创建托管Bean (MBean),然后将其注册到MBean Server 。 MBean服务器充当所有注册的MBean的管理代理。

We use JMX Connectors to connect to MBean server and to manage the registered resources. For example, JDK comes with JConsole through which you can connect to any local or remote MBean server. If you have worked on JBoss server or any enterprise servers, they all comes with JMX Console to monitor and manage MBeans.

我们使用JMX连接器连接到MBean服务器并管理注册的资源。 例如,JDK自带的JConsole,通过它可以连接到任何本地或远程MBean服务器。 如果您使用过JBoss服务器或任何企业服务器,那么它们都随附JMX控制台来监视和管理MBean。

Here we will learn basics of JMX and how to use JConsole to connect and manage MBeans. Lets start now…

在这里,我们将学习JMX的基础知识以及如何使用JConsole连接和管理MBean。 让我们现在开始…

First of all we need to create MBean and for that we need to first create the interface that defines the attributes and operations that we want to expose. The interface name must end with MBean. If you just want to allow read-only, you can leave setter methods.

首先,我们需要创建MBean,为此,我们首先需要创建定义我们要公开的属性和操作的接口。 接口名称必须以MBean结尾 。 如果只想允许只读,则可以保留setter方法。

SystemConfigMBean.java

SystemConfigMBean.java

package com.journaldev.jmx;

public interface SystemConfigMBean {

    public void setThreadCount(int noOfThreads);
    public int getThreadCount();
    
    public void setSchemaName(String schemaName);
    public String getSchemaName();
    
    // any method starting with get and set are considered
    // as attributes getter and setter methods, so I am 
    // using do* for operation.
    public String doConfig();
}

Next step is to provide the actual implementation of the MBean interface. The JMX Naming convention is to keep the implementation class name as interface name – MBean. So my implementation class will be SystemConfig.

下一步是提供MBean接口的实际实现。 JMX命名约定是将实现类名称保留为接口名称– MBean。 所以我的实现类将是SystemConfig

SystemConfig.java

SystemConfig.java

package com.journaldev.jmx;

public class SystemConfig implements SystemConfigMBean {

    private int threadCount;
    private String schemaName;
    
    public SystemConfig(int numThreads, String schema){
        this.threadCount=numThreads;
        this.schemaName=schema;
    }
    
    @Override
    public void setThreadCount(int noOfThreads) {
        this.threadCount=noOfThreads;
    }


    @Override
    public int getThreadCount() {
        return this.threadCount;
    }


    @Override
    public void setSchemaName(String schemaName) {
        this.schemaName=schemaName;
    }


    @Override
    public String getSchemaName() {
        return this.schemaName;
    }
    
    @Override
    public String doConfig(){
        return "No of Threads="+this.threadCount+" and DB Schema Name="+this.schemaName;
    }

}

Now we need to register our MBean implementation to the MBean server. We can keep any name of our class. After registering MBean, I will keep the thread running until we modify the thread count to 0 through jconsole, that will be the logic to end our application.

现在,我们需要将MBean实现注册到MBean服务器。 我们可以保留班级的任何名称。 注册MBean之后,我将保持线程运行,直到通过jconsole将线程计数修改为0,这将是结束应用程序的逻辑。

SystemConfigManagement.java

SystemConfigManagement.java

package com.journaldev.jmx;

import java.lang.management.ManagementFactory;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;

public class SystemConfigManagement {
    private static final int DEFAULT_NO_THREADS=10;
    private static final String DEFAULT_SCHEMA="default";

    public static void main(String[] args) throws MalformedObjectNameException, InterruptedException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
        //Get the MBean server
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        //register the MBean
        SystemConfig mBean = new SystemConfig(DEFAULT_NO_THREADS, DEFAULT_SCHEMA);
        ObjectName name = new ObjectName("com.journaldev.jmx:type=SystemConfig");
        mbs.registerMBean(mBean, name);
        do{
            Thread.sleep(3000);
            System.out.println("Thread Count="+mBean.getThreadCount()+":::Schema Name="+mBean.getSchemaName());
        }while(mBean.getThreadCount() !=0);
        
    }

}

When I run the above program, it registers our MBean implementation to the platform MBean server and keeps on printing following lines on console.

当我运行上述程序时,它会将我们的MBean实现注册到平台MBean服务器,并继续在控制台上打印以下行。

Thread Count=10:::Schema Name=default
Thread Count=10:::Schema Name=default
Thread Count=10:::Schema Name=default

Let’s start JConsole now and check all the options our MBean provides. To start JConsole, all you need to do is open a terminal window and run jconsole command.

现在让我们启动JConsole并检查MBean提供的所有选项。 要启动JConsole,您需要做的就是打开一个终端窗口并运行jconsole命令。

JConsole allows to create local connection as well as connection to remote processes, you will be able to see our program name in the list of local processes. Just chose the application and click on Connect button, once connected go to MBean tab and you will see that we can change the value of threadCount and schemaName dynamically and any value changed there will be reflected in console where our application is running. We are also presented with the methods that we can invoke.

JConsole允许创建本地连接以及与远程进程的连接,您将能够在本地进程列表中看到我们的程序名称。 只需选择应用程序并单击“连接”按钮,连接后转到“ MBean”选项卡,您将看到我们可以动态更改threadCountschemaName的值,并且其中的任何更改都将反映在运行我们的应用程序的控制台中。 我们还介绍了可以调用的方法。

As soon as you will change the threadCount to 0, the application will exit the do-while loop and terminate.

一旦将threadCount更改为0,应用程序将退出do-while循环并终止。

Some of the benefits of using JMX are:

使用JMX的一些好处是:

  • JMX allows us to manage our application at runtime without any heavy investment and provides out of the box solution.

    JMX允许我们在运行时管理我们的应用程序,而无需花费大量资金,并提供了即用的解决方案。
  • Using JMX is easy and reduces effort and investment for creating any management tool for our applications.

    使用JMX很容易,并减少了为我们的应用程序创建任何管理工具的工作量和投资。

翻译自: https://www.journaldev.com/1352/what-is-jmx-mbean-jconsole-tutorial

jconsole jmx

 类似资料: