through the jmx window - lab instructions

10
HOL4820 – Th In this lab we will be looking at the a powerful tool for exposing monito stands for Java Management eXtens http://docs.oracle.com/javase/7/docs at version 4. The specification descr “The Java Management extensions ( design patterns, the APIs, and the se the Java programming language.” Essentially JMX is a way of exposin (control methods) in a standardized use them. It is a standard used by all to their runtime, and to make availab monitoring information such as mem While our goal in this lab is to use J first step is to view the kind of infor client capable of browsing JMX info VisualVM, which has been installed terminal and typing jvisualvm. This will launch VisualVM, which w hrough the JMX W JMX API, the information that it makes availab oring and management information from your ap sions and is a Java API standard described by the s/technotes/guides/jmx/JMX_1_4_specification. ribes JMX as: (also called the JMX specification) define an arc ervices for application and network managemen ng management information (metrics) and mana way, so that monitoring and management tools c l major application servers to expose metrics and ble control operations. It is also used by the JRE mory usage and garbage collection statistics. JMX to expose metrics and operations on our ow rmation that the JRE exposes using JMX, and to ormation. In this lab we will use the MBean brow d prior to this lab. To get started, launch VisualV will open with the home screen. Window le and how it can be pplication. JMX e document at .pdf. It is currently rchitecture, the nt and monitoring in agement operations can discover and d statistics relating E to expose wn application, our do this we need a wser plugin for VM by opening a

Upload: c2b2-consulting

Post on 02-Jul-2015

1.039 views

Category:

Technology


1 download

DESCRIPTION

Lab Instructions by Matt Brasier, Principal Consultant at C2B2, from the Hands on Lab delivered at the JavaOne 2014 conference. ABOUT HOL: This session will demonstrate the depth and breadth of the information available via the JMX API. We will tools that come with the JDK to peer deep into the workings of the JVM and understand how to identify and solve common performance bottlenecks or other problems. Attendees will get examples of using tools like VisualVM and Jstat to interrogate the JVM, and how to interpret the data returned, and learn how to add JMX instrumentation to their own applications and expose this to monitoring tools. This will be a session that allows attendees to understand the power available to them in some of the overlooked core JVM features The session will use a combination of slides and examples that the attendees can code-along with on their own laptops, and will focus on what JMX is, how it works, and also how you can expose your own application MBeans and use these to monitor the application. In my work as a Java performance consultant, I have found that JMX and the basic JVM tooling that uses it, is not understood by developers, so this session is about raising awareness of these tools and allowing developers to get inside the JVM and their application to understand how it works (and write better code). In my experience, once developers understand the power of JMX and VisualVM, they find it very interesting, and often the best way to demonstrate it is by allowing people to work with it. The fact that the base JDK is all that is required to demonstrate this means that there are few pre-requisites for this session, and because it is based on a low level technology, it is of interest to people working on all aspects of Java. I think this could make popular talk which will help developers understand the magic and power behind the Java Virtual Machine.

TRANSCRIPT

Page 1: Through the JMX Window - Lab Instructions

HOL4820 – Through the JMX WindowIn this lab we will be looking at the JMX API, the information that it makes available and how it can be a powerful tool for exposing monitoring and stands for Java Management eXtensions and is a Java API standard described by the document at http://docs.oracle.com/javase/7/docs/technotes/guides/jmx/JMX_1_4_specification.pdfat version 4. The specification describes JMX as:

“The Java Management extensions (also called the JMX specification) define an architecture, the

design patterns, the APIs, and the services for application and network management and monitoring in

the Java programming language.”

Essentially JMX is a way of exposing management information (metrics) and management operations (control methods) in a standardized way, so thause them. It is a standard used by all major application servers to expose metrics and statistics relating to their runtime, and to make available control operations. It is also used by the JRE to expose monitoring information such as memory usage and garbage collection statistics.

While our goal in this lab is to use JMX to expose metrics and operations on our own application, our first step is to view the kind of information that the JRE exposes using JMclient capable of browsing JMX information. In this lab we will use the MBean browser plugin for VisualVM, which has been installed prior to this lab. To get started, launch VisualVM by opening a terminal and typing jvisualvm.

This will launch VisualVM, which will open with the home screen.

Through the JMX WindowIn this lab we will be looking at the JMX API, the information that it makes available and how it can be a powerful tool for exposing monitoring and management information from your application. JMX stands for Java Management eXtensions and is a Java API standard described by the document at

.oracle.com/javase/7/docs/technotes/guides/jmx/JMX_1_4_specification.pdfat version 4. The specification describes JMX as:

“The Java Management extensions (also called the JMX specification) define an architecture, the

APIs, and the services for application and network management and monitoring in

Essentially JMX is a way of exposing management information (metrics) and management operations (control methods) in a standardized way, so that monitoring and management tools can discover and use them. It is a standard used by all major application servers to expose metrics and statistics relating to their runtime, and to make available control operations. It is also used by the JRE to expose

onitoring information such as memory usage and garbage collection statistics.

While our goal in this lab is to use JMX to expose metrics and operations on our own application, our first step is to view the kind of information that the JRE exposes using JMX, and to do this we need a client capable of browsing JMX information. In this lab we will use the MBean browser plugin for VisualVM, which has been installed prior to this lab. To get started, launch VisualVM by opening a

This will launch VisualVM, which will open with the home screen.

Through the JMX Window In this lab we will be looking at the JMX API, the information that it makes available and how it can be

management information from your application. JMX stands for Java Management eXtensions and is a Java API standard described by the document at

.oracle.com/javase/7/docs/technotes/guides/jmx/JMX_1_4_specification.pdf. It is currently

“The Java Management extensions (also called the JMX specification) define an architecture, the

APIs, and the services for application and network management and monitoring in

Essentially JMX is a way of exposing management information (metrics) and management operations t monitoring and management tools can discover and

use them. It is a standard used by all major application servers to expose metrics and statistics relating to their runtime, and to make available control operations. It is also used by the JRE to expose

While our goal in this lab is to use JMX to expose metrics and operations on our own application, our X, and to do this we need a

client capable of browsing JMX information. In this lab we will use the MBean browser plugin for VisualVM, which has been installed prior to this lab. To get started, launch VisualVM by opening a

Page 2: Through the JMX Window - Lab Instructions

On the left hand side there is a list of all the available JVMs that are running locally (and as the same user as VisualVM) and we can select the JVM that is running This will open the overview for the JVM, but we want to view the MBean browser, so select MBeans from the tabs at the top.

Here we see a tree view of the MBeans available in the JVM we are connected tothrough them to find their attributes and operations.

Now that we are connected, it seems like a good time to go over the basic concepts behind JMX. Like

On the left hand side there is a list of all the available JVMs that are running locally (and as the same user as VisualVM) and we can select the JVM that is running VisualVM itself by double clicking on it. This will open the overview for the JVM, but we want to view the MBean browser, so select MBeans

Here we see a tree view of the MBeans available in the JVM we are connected to, and we can navigate through them to find their attributes and operations.

Now that we are connected, it seems like a good time to go over the basic concepts behind JMX. Like

On the left hand side there is a list of all the available JVMs that are running locally (and as the same VisualVM itself by double clicking on it.

This will open the overview for the JVM, but we want to view the MBean browser, so select MBeans

, and we can navigate

Now that we are connected, it seems like a good time to go over the basic concepts behind JMX. Like

Page 3: Through the JMX Window - Lab Instructions

most Java services JMX is based on the concept of beans, and in JMX these are Mfor Management Beans. The attributes and operations available on an MBean relate to one component of a system. The other key part of JMX is the MBean Server. This acts as the starting point for registering, looking up or querying MBeaso with a name, and that name can be used by other services to locate it. For example the MBean for the old generation garbage collector in the VisualVM JVM is called:

java.lang:type=GarbageCollector,name=MarkSweepCompact

The MBean name consists of a number of components. All MBean names start with a domain, and it is these domains that are displayed at the top level of the tree view in VisualVM. In the example above the domain is java.lang. The domain ends with a : and after that follows a number of other components. The name component describes an individual instance of an MBean, while the type component is used to group a number of MBeans together that are of the same type. In the example above tMarkSweepCompact and the type is GarbageCollector, so we know we are looking at an MBean that exposes information and operations relating to a MarkSweepCompact garbage collector.

Go ahead and navigate to the MarkSweepCompact garbage collector MBdrilling down into the tree through java.lang

We are shown the Attributes page and we can see how many garbage collections have occurred and the time in ms that has been spent by this garbage c

most Java services JMX is based on the concept of beans, and in JMX these are MBeans, which is short for Management Beans. The attributes and operations available on an MBean relate to one component of a system. The other key part of JMX is the MBean Server. This acts as the starting point for registering, looking up or querying MBeans. When an MBean registers with the MBean server it does so with a name, and that name can be used by other services to locate it. For example the MBean for the old generation garbage collector in the VisualVM JVM is called:

or,name=MarkSweepCompact

The MBean name consists of a number of components. All MBean names start with a domain, and it is these domains that are displayed at the top level of the tree view in VisualVM. In the example above

ain ends with a : and after that follows a number of other components. The name component describes an individual instance of an MBean, while the type component is used to group a number of MBeans together that are of the same type. In the example above tMarkSweepCompact and the type is GarbageCollector, so we know we are looking at an MBean that exposes information and operations relating to a MarkSweepCompact garbage collector.

Go ahead and navigate to the MarkSweepCompact garbage collector MBean in VisualVM now by java.lang->GarbageCollector->MarkSweepCompact

page and we can see how many garbage collections have occurred and the time in ms that has been spent by this garbage collector since the JVM was started. The values in bold

Beans, which is short for Management Beans. The attributes and operations available on an MBean relate to one component of a system. The other key part of JMX is the MBean Server. This acts as the starting point for

ns. When an MBean registers with the MBean server it does so with a name, and that name can be used by other services to locate it. For example the MBean for

The MBean name consists of a number of components. All MBean names start with a domain, and it is these domains that are displayed at the top level of the tree view in VisualVM. In the example above

ain ends with a : and after that follows a number of other components. The name component describes an individual instance of an MBean, while the type component is used to group a number of MBeans together that are of the same type. In the example above the name is MarkSweepCompact and the type is GarbageCollector, so we know we are looking at an MBean that exposes information and operations relating to a MarkSweepCompact garbage collector.

ean in VisualVM now by MarkSweepCompact.

page and we can see how many garbage collections have occurred and the ollector since the JVM was started. The values in bold

Page 4: Through the JMX Window - Lab Instructions

hold composite data (multiple values) and can be double clicked to view the values within. Notice that the operations tab is grayed out as this MBean exposes no operations.

Navigate around the MBean heirarchy, and see if you can find the following information:

1. Uptime for the JVM.

2. The JVM classpath.

3. The current used memory for the Eden memory space.

4. The current thread count for the JVM.

We can also invoke operations using VisualVM. Get a list of the executing the dumpAllThreads operation on the

This returns a navigable list of the running threads and what they are doing.

Now we have seen how to view JMX attributes andapplication.

Open NetBeans and start a new project by selecting

Select a Java Application and click clicking Finish.

hold composite data (multiple values) and can be double clicked to view the values within. Notice that the operations tab is grayed out as this MBean exposes no operations.

eirarchy, and see if you can find the following information:

The current used memory for the Eden memory space.

The current thread count for the JVM.

We can also invoke operations using VisualVM. Get a list of the thread stacks for all running threads by operation on the java.lang:type=Threading MBean.

This returns a navigable list of the running threads and what they are doing.

Now we have seen how to view JMX attributes and invoke operations, lets write our own JMX enabled

Open NetBeans and start a new project by selecting File->New->Project

and click Next, then call the project JMXWindow. Accept the defaults by

hold composite data (multiple values) and can be double clicked to view the values within. Notice that

eirarchy, and see if you can find the following information:

thread stacks for all running threads by MBean.

invoke operations, lets write our own JMX enabled

. Accept the defaults by

Page 5: Through the JMX Window - Lab Instructions

This will create a simple Java project with a main class called to edit this to create our example MBean by creating that interface, and using the main method to register the MBean with the platform MBean server (which runs in all JVMs).

The first step then is to create the MBean interface. Do this by opening the hand navigation panel, right clicking

This will create a simple Java project with a main class called jmxwindow.JMXWindow

to edit this to create our example MBean by creating an MBean interface, having our class implement that interface, and using the main method to register the MBean with the platform MBean server

The first step then is to create the MBean interface. Do this by opening the source pachand navigation panel, right clicking jmxwindow and selecing New->Java Interface

jmxwindow.JMXWindow. We are going an MBean interface, having our class implement

that interface, and using the main method to register the MBean with the platform MBean server

source packages in the left Java Interface.

Page 6: Through the JMX Window - Lab Instructions

In the dialogue box that pops up, name the interface

Now let’s give our MBean interface some attributes and operations. simple counter, and the attributes we want to expose are the current count value and the current pause. We also want to create an operation to reset the current count value. Attributes are exposed in JMX by creating getter methods for them on the MBean interface (and we can make them writable via JMX by including setter methods). Any methods on the interface that do not conform to the JavaBean getter/setter pattern are treated as operations on the MBean.

Edit your MBean interface code so that it has the following methods exposed on it:

package jmxwindow;

public interface JMXWindowMBean {

public long getCount();

public long getPauseTime();

public void setPauseTime(long p);

public void resetCount();

}

In the dialogue box that pops up, name the interface JMXWindowMBean and click

s give our MBean interface some attributes and operations. Our application is going to be a simple counter, and the attributes we want to expose are the current count value and the current pause. We also want to create an operation to reset the current count value. Attributes are exposed in JMX by

methods for them on the MBean interface (and we can make them writable via JMX by including setter methods). Any methods on the interface that do not conform to the JavaBean getter/setter pattern are treated as operations on the MBean.

erface code so that it has the following methods exposed on it:

public void setPauseTime(long p);

and click Finish.

Our application is going to be a simple counter, and the attributes we want to expose are the current count value and the current pause. We also want to create an operation to reset the current count value. Attributes are exposed in JMX by

methods for them on the MBean interface (and we can make them writable via JMX by including setter methods). Any methods on the interface that do not conform to the JavaBean

Page 7: Through the JMX Window - Lab Instructions

This exposes the count as a read-only attribute, the pause time as a read/write attribute, and adds an operation to reset the counter.

Next we need to have our class implement the interface. Go back to our JMXWindow.java class and edit it so that it implements our interface.

package jmxwindow;

public class JMXWindow implements JMXWindowMBean{

private long initialValue = 0l;

private long count;

private long pause;

public static void main(String[] args) {

}

@Override

public long getCount() {

return this.count;

}

public void setCount(long c) {

this.count = c;

}

@Override

public long getPauseTime() {

return this.pause;

}

@Override

public void setPauseTime(long p) {

this.pause=p;

}

@Override

public void resetCount() {

this.count=0;

}

public long getInitialValue() {

return initialValue;

}

public void setInitialValue(long i) {

this.initialValue = i;

Page 8: Through the JMX Window - Lab Instructions

}

}

Note that we have added some getters and setters that are not in the interface. This gives us some java attributes that can be accessed from code but not via JMX. We now have a class that implements our interface, so next we need to register it with the MBean Server and have it actually do the count. To do this we need to edit the main class to have it instantiate a JMXWindow counter and register it.

public static void main(String[] args) {

try {

//Instantiate a class

JMXWindow counter = new JMXWindow();

//Set the intial pause time

counter.setPauseTime(1000);

//Get hold of the platform MBean Server

MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

//Create a name for our MBean

ObjectName name = new ObjectName("jmxwindow:type=JMXWindow");

//Register the mbean

mbs.registerMBean(counter, name);

//Start counting

while (true)

{

counter.setCount(counter.getCount()+1);

Thread.sleep(counter.getPauseTime());

}

} catch (MalformedObjectNameException ex) {

ex.printStackTrace();

} catch (InstanceAlreadyExistsException ex) {

ex.printStackTrace();

} catch (MBeanRegistrationException ex) {

ex.printStackTrace();

} catch (NotCompliantMBeanException ex) {

ex.printStackTrace();

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

We also need to add some import statements at the top of our class:

package jmxwindow;

import java.lang.management.ManagementFactory;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.management.InstanceAlreadyExistsException;

Page 9: Through the JMX Window - Lab Instructions

import javax.management.MBeanRegistrationException;

import javax.management.MBeanServer;

import javax.management.MalformedObjectNameException;

import javax.management.NotCompliantMBeanException;

import javax.management.ObjectName;

Finally we should be able to run our application by clicking the big green run arrow at the top of the NetBeans window.

While the application is running, we can use VisualVM to connect to the appas jmxwindow.JMXWindow in the list of local JVMs on the left

import javax.management.MBeanRegistrationException;

import javax.management.MBeanServer;

import javax.management.MalformedObjectNameException;

liantMBeanException;

import javax.management.ObjectName;

Finally we should be able to run our application by clicking the big green run arrow at the top of the

While the application is running, we can use VisualVM to connect to the application (it should appear as jmxwindow.JMXWindow in the list of local JVMs on the left) and view the MBean attributes.

Finally we should be able to run our application by clicking the big green run arrow at the top of the

lication (it should appear and view the MBean attributes.

Page 10: Through the JMX Window - Lab Instructions

By navigating to our MBean we can view its attributes, and you should be able to invoke the resetCounter operation and edit the pause in real time, so you can speed up or slow down the counting speed. To refresh the MBean view in VisualVM you may need to scroll down the attributes pane to the refresh button, as it usually scr

the bottom of the screen.

Congratulations, you have written your first JMX instrumented application!

If you have time remaining you may want to investigate JMX further with one of the following projects:

• Looking at the JMX instrumentation availamachine.

• Extending the JMX application we have just written.

• Exploring other JMX monitoring tools.

By navigating to our MBean we can view its attributes, and you should be able to invoke the resetCounter operation and edit the pause time. Note that editing the pause time affects the application in real time, so you can speed up or slow down the counting speed. To refresh the MBean view in VisualVM you may need to scroll down the attributes pane to the refresh button, as it usually scr

Congratulations, you have written your first JMX instrumented application!

If you have time remaining you may want to investigate JMX further with one of the following

Looking at the JMX instrumentation available in the glassfish 3 server installed in this virtual

Extending the JMX application we have just written.

Exploring other JMX monitoring tools.

By navigating to our MBean we can view its attributes, and you should be able to invoke the time. Note that editing the pause time affects the application

in real time, so you can speed up or slow down the counting speed. To refresh the MBean view in VisualVM you may need to scroll down the attributes pane to the refresh button, as it usually scrolls off

If you have time remaining you may want to investigate JMX further with one of the following

ble in the glassfish 3 server installed in this virtual