What is JMX? 10 mins Quick Start JMX Tutorial (MBean, JConsole)





What is JMX? 10 mins Quick Start JMX Tutorial (MBean, JConsole)

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.

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.

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.

What is JMX? 10 mins Quick Start JMX Tutorial (MBean, JConsole)_第1张图片

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

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.

SystemConfigMBean.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
packagecom.journaldev.jmx;
 
publicinterface SystemConfigMBean {
 
    publicvoid setThreadCount(intnoOfThreads);
    publicint getThreadCount();
     
    publicvoid setSchemaName(String schemaName);
    publicString getSchemaName();
     
    // any method starting with get and set are considered
    // as attributes getter and setter methods, so I am
    // using do* for operation.
    publicString 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 beSystemConfig.

SystemConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
packagecom.journaldev.jmx;
 
publicclass SystemConfig implementsSystemConfigMBean {
 
    privateint threadCount;
    privateString schemaName;
     
    publicSystemConfig(intnumThreads, String schema){
        this.threadCount=numThreads;
        this.schemaName=schema;
    }
     
    @Override
    publicvoid setThreadCount(intnoOfThreads) {
        this.threadCount=noOfThreads;
    }
 
 
    @Override
    publicint getThreadCount() {
        returnthis.threadCount;
    }
 
 
    @Override
    publicvoid setSchemaName(String schemaName) {
        this.schemaName=schemaName;
    }
 
 
    @Override
    publicString getSchemaName() {
        returnthis.schemaName;
    }
     
    @Override
    publicString 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.

SystemConfigManagement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
packagecom.journaldev.jmx;
 
importjava.lang.management.ManagementFactory;
 
importjavax.management.InstanceAlreadyExistsException;
importjavax.management.MBeanRegistrationException;
importjavax.management.MBeanServer;
importjavax.management.MalformedObjectNameException;
importjavax.management.NotCompliantMBeanException;
importjavax.management.ObjectName;
 
publicclass SystemConfigManagement {
    privatestatic final int DEFAULT_NO_THREADS=10;
    privatestatic final String DEFAULT_SCHEMA="default";
 
    publicstatic void main(String[] args) throwsMalformedObjectNameException, InterruptedException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
        //Get the MBean server
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        //register the MBean
        SystemConfig mBean = newSystemConfig(DEFAULT_NO_THREADS, DEFAULT_SCHEMA);
        ObjectName name = newObjectName("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.

1
2
3
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 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 schemaNamedynamically 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.

What is JMX? 10 mins Quick Start JMX Tutorial (MBean, JConsole)_第2张图片

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

Some of the benefits of using JMX are:

  • JMX allows us to manage our application at runtime without any heavy investment and provides out of the box solution.
  • Using JMX is easy and reduces effort and investment for creating any management tool for our applications.
http://www.journaldev.com/1352/what-is-jmx-mbean-jconsole-tutorial

你可能感兴趣的:(What is JMX? 10 mins Quick Start JMX Tutorial (MBean, JConsole))