32
© C2B2 Consulting Limited 2013 All Rights Reserved Through the JMX Window Matt Brasier Head of Consulting C2B2 Consulting

Through the JMX Window

Embed Size (px)

DESCRIPTION

Slides 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.

Citation preview

Page 1: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

Through the JMX Window

Matt Brasier

Head of Consulting

C2B2 Consulting

Page 2: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

Who am I?

• Head of consulting at C2B2

• Application server consultant

– WebLogic 5.1->Present

– JBoss 3.x -> Present

– Tomcat, etc

• Performance tuning and critical situation support

• Author

Page 3: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

Agenda

• What is JMX?

• JMX tools

• What information does it expose?

• What operations can JMX expose?

• Exposing your own information and operations

Page 4: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

WHAT IS JMX?

Page 5: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

What is JMX

• Java Management eXtensions

• Currently at version 1.4

• http://docs.oracle.com/javase/7/docs/technotes/guides/jmx/JMX_1_4_specification.pdf

“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.”

Page 6: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

JMX History

• 1998 – JMX starts as JSR 3. EG composed of most J2EE vendors, based on JDMK

• 2003 – J2EE 1.4 incorporates JMX in the J2EE Spec

• 2004 – With Java 5 release, JMX becomes part of the JDK/Java SE

• JMAPI – Java Management API, failed Sun project

• JDMK – Java Dynamic Management Kit, another failed Sun project

• JMX

Page 7: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

What is management?

• Management Information

– Any information that is useful for the runtime management of the application.

• Management Operations

– Any operations that can assist in the runtime management of the application.

Page 8: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

Why JMX?

• Application Management is important!

– If often forgotten.

• Standardise the APIs and patterns for exposing management operations and tooling.

– Tools can be sure information is in an expected format.

– Applications can take advantage of existing tooling.

Page 9: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

Core Concepts

• MBean

– Management Bean

• Operations

• Attributes

• MBean Server

– Registry for MBeans

• API

– Remote and local access to MBean Server and MBeans

Page 10: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

Core Concepts

• Applications/Frameworks/Containers expose MBeans

• The JVM contains an MBeanServer which registers all MBeans

• Clients use the JMX API to connect to the MBeanServer and find relevant MBeans

• Clients read/change values of attributes and invoke operations to manage the service

Page 11: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

JMX TOOLS

Page 12: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

JMX Tools

• JConsole

• JVisualVM

• Monitoring tools

– Hyperic HQ

– Nagios

– Etc

Page 13: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

Demo

Page 14: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

WHAT INFORMATION DOES IT EXPOSE?

Page 15: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

Default JMX info

• Heap Use– java.lang:type=Memory

– java.lang:type=MemoryPool,name=Eden Space

– java.lang:type=MemoryPool,name=Tenured Gen

– java.lang:type=MemoryPool,name=Perm Gen

• Garbage collection– java.lang:type=GarbageCollector,name=MarkSweepCompact

– java.lang:type=GarbageCollector,name=Copy

• Operating system info– java.lang:type=OperatingSystem

Page 16: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

Default JMX info

• Runtime JVM parameters– java.lang:type=Runtime

• NIO Buffers– java.nio:type=BufferPool,name=direct

• JDK Logger– java.util.logging:type=Logging

• Classloading– java.lang:type=ClassLoading

• And more!

Page 17: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

In an application server

• Connection pool info

– Sizes

– Free connections

• Servlet/JSP info

– Number of requests

– Number of errors

• Thread pools

– Sizes

• JMS

– Queue sizes

– Pending messages

• EJBs

– Pool sizes

– Cache hit rates

• Connectors

– In coming request volumes

Page 18: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

WHAT OPERATIONS DOES IT EXPOSE?

Page 19: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

Default operations

• com.sun.management:type=HotSpotDiagnostic

– dumpHeap

– setVMOption

– getVMOption

• java.lang:type=Memory

– Gc

• java.lang:type=Threading– dumpAllThreads

– findDeadLockedThreads

Page 20: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

In an application server

• Datasources

– Reset all datasources

• JMS

– Delete all messages

• Server

– Restart or stop a server

• Deployment

– Deploy or redeploy applications

Page 21: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

In an application server

• Any operation you could normally perform to manage an application server!

• It is like an X-ray of your application

Page 22: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

EXPOSING YOUR OWN MANAGEMENT INFORMATION AND OPERATIONS

Page 23: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

Two step process

• Create MBeans

• Register with MBeanserver

• Profit

Page 24: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

Create MBeans

• Identify where in your application you have relevant management information

• Create an MBean interface

• Implement the MBean interface

Page 25: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

MBean Interface

public Interface LocationMonitorMBean

{

public void getNumberOfFiles();

public void setScanPeriod();

public void getScanPeriod();

public void resetFileCounter();

public void disable();

public void enable();

public void getEnabled();

}

Getters and Setters representAttributes

Other methods represent operations

Page 26: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

Implementation class

• Implement the MBean interface

– You can do this with an existing class

• Try and avoid doing intensive work in the MBean implementation

– Pre-compute values

– Periodically check and cache values

Page 27: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

Implementation class

public class LocationMonitor implements LocationMonitorMBean

{

private boolean enabled;

private long scanInterval;

private long numFiles;

public void getNumberOfFiles(

{

return numFiles;

...

Attributes are accessed via getters/setters

Page 28: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

Register with MBeanServer

• Create (or get hold of) an instance of your MBean implementation

• Get hold of an MBean Server

– PlatformMBeanServer

• Create an ObjectName for your MBean

• Register the MBean

Page 29: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

Register with MBeanServer

public static void main(String[] args) throws Exception

{

MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

ObjectName name = new ObjectName(“uk.co.c2b2.management:type=LocationMonitor");

LocationMonitor mbean = new LocationMonitor();

mbs.registerMBean(mbean, name);

}

Get the MBean Server

Create the ObjectName

Create the MBean instance

Register it

Page 30: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

What to monitor

• Resources that your application uses

– Including data (flow rates etc)

• Expose management operations

– Restarting components

– Re-loading configuration files

Page 31: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

What is through the JMX window?

• A future with more Information

• A future with more control

Page 32: Through the JMX Window

© C2B2 Consulting Limited 2013All Rights Reserved

QUESTIONS?