course # 3250 best practices and design patterns for jmx development satadip dutta justin murray...

63
Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Upload: gilbert-bruce

Post on 26-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Course # 3250Best Practices and Design

Patterns for JMX Development

Satadip DuttaJustin Murray

Hewlett-Packard

Page 2: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Objectives

Why are we talking about Design Patterns and JMX?

Java Management Extension API is now available in the J2SE core as well as J2EE– Usage of the new J2SE APIs– Design Patterns

Page 3: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Who are the speakers

• Satadip Dutta– Software Architect working on Manageability

enablement tools– Works on creating tools that help

management enable custom applications• Justin Murray

– Technical consultant with HP’s software business unit

– Works with customers on implementing manageability for Java/J2EE/web services based applications

Page 4: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Presentation goals

At the end of the presentation, you will be able to– Use the Java Management Extensions

(JMX)– Apply design patterns while

instrumenting your application for management

– Build better managed applications

Page 5: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Agenda

Introduction to Manageability

Introduction to JMX

Using JMX

Design Patterns

A Manageability Enablement Tool

Page 6: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

AgendaIntroduction to Manageability

Introduction to JMX

Using JMX

Design Patterns

A Manageability Enablement Tool

Page 7: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Problem statement

erroroccurs

Symptoms

Causes

Application Development IT/Operations

Where?

Page 8: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Why is application manageability important?

We focus most of our application development effort on functionality and performance

- but –

Most of the lifetime of an application is in deployment

Page 9: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Manageability definedManageability:

Monitoring: Tracking: Control:

exercise administrative and supervisory actions

capture runtime and historical events of a

particular component,

example: the monitoring of the performance of a network router

observe aspects of a single thread across multiple components.

example: the tracking of a message from sender to receiver

through a messaging backbone.

alter the behavior of the managed

component without interrupting its

operation

example: changing the logging level of an

application component.

Page 10: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Accomplishment

Manageability enables– Reactive Problem Resolution– Proactive Problem Detection

Results in – Stability– Resiliency

Page 11: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

AgendaIntroduction to Manageability

Introduction to JMX

Using JMX

Design Patterns

Application Architecture

Managebaility Enablement Tools

Page 12: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

JMX defined

The JMX specification provides an API and architecture that provides a standard mechanism to management enable Java applications

Page 13: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

JMX Architecture

Instrumentation

MBeanServer

Inst

rum

en

tatio

n

Bu

sin

ess

Lo

gic

Ap

plic

atio

n M

Be

an

s

Platform MBeans

Java Virtual Machine

ManagementApplication

Page 14: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

JMX Architecture

Instrumentation

MBeanServer

Inst

rum

en

tatio

n

Bu

sin

ess

Lo

gic

Ap

plic

atio

n M

Be

an

s

Platform MBeans

Java Virtual Machine

ManagementApplication

Page 15: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

JMX Architecture

• Instrumentation Layer– MBeans - MemoryMXBean

• Agent Layer– MBeanServer -PlatformMBeanServer

• Distributed Layer– Connectors- RMI– Adapters- SNMP, WSDM

Page 16: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

MBeans

• An MBean is a named managed objectrepresenting a resource

– Application configuration – JVM/ Environment Attributes– User identity– Business entities

• An MBean can have:– Attributes that can be read and/or written– Operations that can be invoked– Notifications that the MBean can send

Page 17: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

AgendaIntroduction to Manageability

Introduction to JMX

Using JMX

Design Patterns

Application Architecture

Managebaility Enablement Tools

Page 18: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Using JMX

• Using the monitoring and management API from J2SE 5.0

• Creating custom MBeans

Page 19: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Solving the problem

• Monitor JVM Health– Monitor Memory and CPU

• Track usage– Specify Memory Thresholds

• Control logging levels– Get information when problems occur

Page 20: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Monitoring and Management API

• MXBeans = Predefined Platform MBeans • JVM related MXBeans focus on

– Memory consumption• MemoryMXBean• MemoryPoolMXBean

– CPU consumption• ThreadMXBean• RuntimeMXBean

Page 21: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Getting started

• Enable the JMX Agent on the JVM

$JAVA_HOME/bin/java -Dcom.sun.management.jmxremote ApplicationName

enables local JVM monitoring

Page 22: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Monitoring the health

• Connect to the MBeanServer• Get the MemoryMXBean• Get the ThreadMXBean

Page 23: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Get the ThreadMXBean

directly using ManagementFactory

ThreadMXBean threadMXBean =

ManagementFactory.getThreadMXBean();

Page 24: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Connecting to the MBeanSever

using ProxymBeanServerConnection= JMXConnectorFactory.connect(new JMXServiceURL(jmxServiceURL),null) .getMBeanServerConnection();

String jmxServiceURL =

service:jmx:rmi:///jndi/rmi://localhost:<port>/<name>

Page 25: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Get the MemoryMXBean

MemoryMXBean memoryMXBean =

ManagementFactory. newPlatformMXBeanProxy

(

mBeanServerConnection,

ManagementFactory.MEMORY_MXBEAN_NAME,

MemoryMXBean.class

);

Page 26: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Tracking usage

// memory usage

memoryPool.setUsageThreshold(MEMORY_LIMIT);

MEMORY_LIMIT is the value in bytes

Page 27: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Control logging level

// get the loggerLoggingMXBean loggerMbean =ManagementFactory.newPlatformMXBeanProxy(mBeanSe

rverConnection,LogManager.LOGGING_MXBEAN_NAME,

LoggingMXBean.class); // change the log levelif(! loggerMbean.getLoggerLevel(loggerName).equals(Level.INFO.getName())) loggerMbean.setLoggerLevel(loggerName,

Level.INFO.getName());

Page 28: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Creating new MBeans

• Create an MBean• Register the MBean

Page 29: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Create MBean

ObjectPoolMBean

totalCapacity: Integer

used Capacity: Integer

resetPool(String Name)

com.hp.util.pools.exceeded

Attributes

Operations

Notification

Page 30: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Registering MBean

//Get MBeanServerMBeanServer platformMBeanserver=

ManagementFactory.getPlatformMBeanServer();

//Register the ObjectPool MBeanObjectName poolName = new ObjectName(“com.hp.util.pools:id=ObjectPool”);

platformMBeanserver.registerMBean (new ObjectPool(),poolName);

Page 31: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Recommendations

• Expose Relevant Data • Expose Coarse grained Data• Use consistent naming• Use standard Java Logging

Page 32: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

AgendaIntroduction to Manageability

Introduction to JMX

Using JMX

Design Patterns

Application Architecture

Managebaility Enablement Tools

Page 33: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Design Patterns

• MBeanForAppConfig General• MBeanWritesToLog• MBeanLivesForever Creational

• MBeanHelper• ManageabilityFacade Structural• MBeanAggregator

Page 34: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

One or more MBeans designed to contain the runtime configuration parameters for your application

Constructed at initialization by reading from a properties file or other persistent data

Allows those configuration parameters to be changed if necessary (e.g. the amount of logging)

Mbean browsers can view its content

MBeanForAppConfig Pattern

Page 35: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

MBeanWritesToLog Pattern

MBeans are good places for counting quantities that are rapidly changing

Requirement sometimes is to draw a time series of such data (e.g. # of users logged in, # of conncurent connections)

Sending that data to a log file is a good approach

The data can be processed in the log file offline from the MBean

Page 36: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Should a business object implement the MBean interface?

-or-

Should the business object talk to a separate MBean object?

Using the JMX model – decisions for developers

applicationobject

MBean Need a reference

Page 37: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Developer registers the new MBean with the MBean server – means every instance needs to register if we use the inheritance

method– Many entries to lookup and store– However, the inheritance method may be simpler than holding a

reference to an MBean

The management interface should be separate from the business objects interfaces (people skills/development process)

Much like MVC

Using an interface or separate object?

Page 38: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Pull Model

– MBean polls the application components and obtains relevant state

– The MBean must obtain a reference to the object

applicationobject

MBean

Page 39: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Push Model

– Application updates MBean on relevant state– The application must be able to locate the

MBean in order to invoke it when necessary

applicationobject

MBean

Page 40: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Which object creates the other object?

(The answer depends on the lifetime we need to give the MBean – should it be alive past the lifetime of the application object?)

• MBean could create the application object• Application object could create the MBean• Application runtime server could create the MBean• MBean could be in a separate process to the

application object

JMX MBean Creational Pattern

Page 41: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

MBeanLivesForEver

Page 42: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

MBeanLivesForEver Pattern

Application servers/containers can create one or more MBeans for you (in a startup class)

Important if the MBean needs to see the instantiation of other objects – like business objects

These business objects are created here separately from the MBeans

MBean is alive for the lifetime of the process

Page 43: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

MBeanAsCreator

Page 44: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

The MBean is the instantiation point for business objects – MBean controls their lifespan

MBean is alive for the lifetime of the process

Easy capturing of business objects coming and going

MBeanAsCreator Pattern

Page 45: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Motivation for an MBeanHelper Pattern

An MBean Helper will take care of the mechanics of registration and lookup of the MBean (which are standard for every MBean)

Details of strings for the MBean name and methods to be invoked are hidden from the business object

Page 46: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

MBeanHelper

Page 47: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Business object does not talk to the MBean directly

Instead they use a Proxy – the MBeanHelper, through which all communication flows to the MBean

MBeanHelper takes care of the mechanics of registration and lookup of the MBean

Details of strings for MBean name and any invoked method are hidden from the business object

Better strong typing on method signature in MBeanHelper

MBeanHelper Pattern

Page 48: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

MBeanHelper

Use the dynamic proxy in Java, java.lang.reflect.Proxy

Benefits• No more Strings in object and method names• No need for one-to-one relationship between Helper and MBean• MBeanRegistrator takes care of registering the bean with the MBeanServer• MBeanProxy creates a proxy with the same interface as the MBean, ready for invocations• Naming of MBean based on name of MBean interface

Page 49: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

MBeanHelper Example

Registering MBean at startup of App Server

public class MyAppListener extends ApplicationLifecycleListener { public void postStart(ApplicationLifecycleEvent

appEvent) { MBeanRegistrator.registerMBean(new

MyManagerMBean());}

}

Page 50: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

ManageabilityFacade

Page 51: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

ManageabilityFacade Pattern•Façade is a collection of all the MBeans in one centralized place, perhaps with just one method ManageabilityFacade.update(origin, msgtype, data)

•Individual MBean names and methods are not important to the business objects that use the ManageabilityFacade

•The ManageabilityFacade decides which MBean gets updated

•Separation of business object concerns from manageability concerns

Page 52: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

ManageabilityFacade Pattern

Use AbstractFactory to create MBean

Makes it possible to extend configuration of MBeans

Use MBeanLivesForever Pattern when creating the MBean

Page 53: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

MBeanAggregator Pattern

Page 54: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

MBeanAggregator PatternThere can be hundreds or even thousands of MBeans to deal with

Management tools should not be polling large number of these MBeans or MBeanHelpers to get the important data they need.

Collection points are needed in an Aggregator object – which may be an MBean itself.

Fewer numbers of these will help performance and scalability

They could cross application server boundaries

Page 55: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Make your MBean as coarsely grained (large) as possible

But

Do not mix completely different manageability issues into one MBean

There should be very few MBeans needed for your application, ideally

Recommendations

Page 56: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Design Patterns• MBeanForAppConfig• MBeanWritesToLog• MBeanLivesForever• MBeanHelper• ManageabilityFacade• MBeanAggregator

• Others you can see as useful?

Page 57: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

AgendaIntroduction to Manageability

Introduction to JMX

Using JMX

Design Patterns

Guideline for manageability

Managebaility Enablement Tools

Page 58: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

• A Policy is that set of rules/measures which express the action to be taken on the occurrence of a management event

• Manageability policies can change over time

• These policies should therefore be removed from the business application code (and from the MBean code)

• Policies belong in a policy engine with a scripting language

Guiding Principle for Application Manageability

Page 59: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Java Virtual Machine

Web Application Server

MBean

MBean Server

MBean

DirectoryService

Monitor

Target tool

Java Virtual Machine

JMX Collector / Analyzer

get config

collect metric

calculate

send to tool

start

Target tool

JNDI

RMI

Metric Definition File(XML)

MBeans Free of Management Policy

Page 60: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Summary

• JMX provides the infrastructure to instrument applications to enable– Monitoring– Tracking– Control

• Design Patterns are available to help with design decisions

Page 61: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Call to Action

• Visit Developer Resources Web Sitehttp://devresource.hp.com

• Search for “JMX”

• Meet the experts at the HP booth

• See the demos

Page 62: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Questions?

Page 63: Course # 3250 Best Practices and Design Patterns for JMX Development Satadip Dutta Justin Murray Hewlett-Packard

Thank You

Course # 3250Best Practices and Design Patterns for JMX

Development

Please fill out the speaker evaluation

You can contact us further at …[email protected], [email protected]