javatmanagementextensionsjmxt-111224090411-phpapp02

download javatmanagementextensionsjmxt-111224090411-phpapp02

of 11

Transcript of javatmanagementextensionsjmxt-111224090411-phpapp02

  • 8/12/2019 javatmanagementextensionsjmxt-111224090411-phpapp02

    1/11

    public

    Java Management Extensions (JMX)

    Introduction

    Tarun Telang,SAP NetWeaver Life Cycle Management,

    May 2008

  • 8/12/2019 javatmanagementextensionsjmxt-111224090411-phpapp02

    2/11

    public SAP 2008 / Page 2

    1. Overview1.1. Introduction?

    1.2. Architecture

    1.2. Why JMX?

    2. MBeans

    2.1. What are MBeans?

    2.2. Writing MBeans

    3. Agents

    3.1. What are Agents?3.2. Registering MBeans

    4. Code Snippets

    4.1. Connecting to MBean Server

    4.2. Accessing MBean

    Agenda

  • 8/12/2019 javatmanagementextensionsjmxt-111224090411-phpapp02

    3/11

    public SAP 2008 / Page 3

    Introduction

    Java Management Extensions (JMX) is an open technology specification that defines the

    management architecture, which enables management and monitoring of applications andservices.

    JMX Architecture provides interfacesfor defining runtime monitoring, managementandconfiguration supportfor applications.

    The JMX architecturehas three layers:

    Distributed layer - adaptors or connectors

    Agent layer - JMX Services and MBean Server

    Instrumentation layer - Managed Beans encapsulating the resources

    JMX is part of both Java SE and Java EE specifications.

  • 8/12/2019 javatmanagementextensionsjmxt-111224090411-phpapp02

    4/11

    public

    JMX Architecture

    SAP 2008 / Page 4

    JVM( host2)

    JVM(host1)

    Application

    MBeans

    Instrumentation Layer

    Mbean Server

    AgentServices

    Agent Level

    Protocol Adaptersand Connectors

    Web Browser

    JMX CompliantManagementApplication

    JMX Manager

    ProprietaryManagementApplication

    Distributed Services Level

  • 8/12/2019 javatmanagementextensionsjmxt-111224090411-phpapp02

    5/11

    public

    Why JMX?

    JMX is suitable for:

    adapting legacy systems

    implementing new solution for management, and monitoring

    plugging into future applications.

    JMX is usedto provide application management for:

    Web applications.

    Stand-alone applications.

    Application servers.

    Services.

    Java-enabled devices.

    The roleof JMX in J2EE applications is to:

    Expose portions of JMS and EJB components to a management console.

    Monitor the application server.

    Configure the runtime environment for your application.

    SAP 2008 / Page 5

  • 8/12/2019 javatmanagementextensionsjmxt-111224090411-phpapp02

    6/11

    public

    MBeans

    MBeans are managed Java objects, similar to Java Bean

    MBeans represents a resourcethat needs to be managed.

    resources are instrumented by MBeans

    MBeans exposes management interface.

    MBeans must have an object name Getters/Setters for attributes

    Management operations

    Self description

    Object Name Represents an MBean, or a patternthat can match the names of several MBeans.

    Syntax: "domain:=

    Example: com.example.mbean:key1=value1, key2=value2, ... keyN=valueN

    SAP 2008 / Page 6

  • 8/12/2019 javatmanagementextensionsjmxt-111224090411-phpapp02

    7/11public

    Writing MBean

    SAP 2008 / Page 7

    // The Memory MBean Interface

    public interfaceMemoryMBean {

    // read

    public int getUsedMemory();

    // write

    public voidsetUsedMemory(int size);

    // read only

    public int getTotalMemory();

    // operation

    public intcalculateFreeMemory();

    }

    // Memory MBean Implementation

    public classMemory implementsMemoryMBean {

    private static final int MEMORY_SIZE = 512;

    private int totalMemory = MEMORY_SIZE;

    private int usedMemory = 0;

    ....

    public int getUsedMemory() {

    return this.usedMemory;

    }

    public synchronized void setUsedMemory(int size)

    {

    this.usedMemory = size;

    }

    public float calculateFreeMemory() {

    returntotalMemory - usedMemory;

    }

    }

  • 8/12/2019 javatmanagementextensionsjmxt-111224090411-phpapp02

    8/11public

    Registering MBean with MBean Server

    SAP 2008 / Page 8

    import java.lang.management.*;

    import javax.management.*;

    public class MemoryAgent {

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

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

    ObjectName name = new ObjectName("com.developersummit.mbeans:type=Memory");

    MemoryMBean mbean = newMemory();

    mbs.registerMBean(mbean, name);

    System.out.println("Agent is running...");

    Thread.sleep(Long.MAX_VALUE);

    }

    }

  • 8/12/2019 javatmanagementextensionsjmxt-111224090411-phpapp02

    9/11public

    Agent

    Management of resources are performed by JMX agent

    Agent includes set of services to manage MBeans

    Agent contains a managed object server (MBean Server)

    MBeans are registeredin MBean Server

    JConsole

    $ java -Dcom.sun.management.jmxremote MemoryAgent

    allows monitoringon the host

    SAP 2008 / Page 9

  • 8/12/2019 javatmanagementextensionsjmxt-111224090411-phpapp02

    10/11public

    Code Snippets

    SAP 2008 / Page 10 SAP 2008 / Page 10

    // Lookup MBeanServer from the JNDIInitialContext initCtx = newInitialContext();

    MBeanServer mbs = (MBeanServer) initCtx.lookup("jmx");

    ObjectName name;

    ...

    mbs.setAttribute(name, newAttribute("Test", "test value"));

    Connecting to MBean Server

    Access to an MBean

    SAP 2008 / Page 10

    mbs.invoke(ObjectNamename, StringoperationName, Object[] params, String[] signature)

    throws InstanceNotFoundException, MBeanException, ReflectionException

    Invoking an MBean Operations

    // the query returns a set of matching ObjectNames

    Set names = mbs.queryName(pattern, null);

    Query an MBean

    http://java.sun.com/j2se/1.5.0/docs/api/javax/management/ObjectName.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/javax/management/InstanceNotFoundException.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/javax/management/MBeanException.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/javax/management/ReflectionException.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/javax/management/ReflectionException.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/javax/management/MBeanException.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/javax/management/InstanceNotFoundException.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/javax/management/ObjectName.html
  • 8/12/2019 javatmanagementextensionsjmxt-111224090411-phpapp02

    11/11public SAP 2008 / Page 11

    Thank you!