Sunday, November 24, 2019

Understanding JMX, Spring JMX




What is JMX ?

Provides an architecture to manage resources dynamically at runtime.

JMX is used mostly in enterprise applications to make system configurable or to get the state of application 
At any point of time .Because the JMX technology is dynamic ,you can use it to monitor and manage resources as they are created, installed and implemented .You can also use the JMX technology to monitor
And manage the JVM.

What can you do with  JMX ?
  • Manage resourses
  • Manage JVM


What are components of  MX ?
The JMX technology can be divided into three levels, as follows:

  • Instrumentation
  • JMX agent - Mbean Server
  • Remote management

Instrumentation

To manage resources using the JMX technology, you must first instrument the resources in the Java programming language. You use Java objects known as MBeans to implement the access to the resources' instrumentation. MBeans must follow the design patterns and interfaces defined in the JMX specification. Doing so ensures that all MBeans provide managed resource instrumentation in a standardized way.

 In addition to standard MBeans, the JMX specification also defines a special type of MBean called an MXBean.

 An MXBean is an MBean that references only a pre-defined set of data types. Other types of MBean exist, but this trail will concentrate on standard MBeans and MXBeans.


Once a resource has been instrumented by MBeans, it can be managed through a JMX agent. MBeans do not require knowledge of the JMX agent with which they will operate.
MBeans are designed to be flexible, simple, and easy to implement. Developers of applications, systems, and networks can make their products manageable in a standard way without having to understand or invest in complex management systems. Existing resources can be made manageable with minimum effort.
In addition, the instrumentation level of the JMX specification provides a notification mechanism. This mechanism enables MBeans to generate and propagate notification events to components of the other levels.

JMX Agent

  •  is a standard management agent that directly controls resources and makes them available to remote management applications. JMX agents are usually located on the same machine as the resources they control, but this arrangement is not a requirement.
  • The core component of a JMX agent is the MBean server, a managed object server in which MBeans are registered.
  •  A JMX agent also includes a set of services to manage MBeans, and 
  • at least one communications adaptor or connector to allow access by a management application.

When you implement a JMX agent, you do not need to know the semantics or functions of the resources that it will manage. In fact, a JMX agent does not even need to know which resources it will serve because any resource instrumented in compliance with the JMX specification can use any JMX agent that offers the services that the resource requires. Similarly, the JMX agent does not need to know the functions of the management applications that will access it.

Remote Management

JMX technology instrumentation can be accessed in many different ways, either through existing management protocols such as the Simple Network Management Protocol (SNMP) or through proprietary protocols. The MBean server relies on protocol adaptors and connectors to make a JMX agent accessible from management applications outside the agent's Java Virtual Machine (Java VM).

Each adaptor provides a view through a specific protocol of all MBeans that are registered in the MBean server. For example, an HTML adaptor could display an MBean in a browser.

Connectors provide a manager-side interface that handles the communication between manager and JMX agent. Each connector provides the same remote management interface through a different protocol. When a remote management application uses this interface, it can connect to a JMX agent transparently through the network, regardless of the protocol. The JMX technology provides a standard solution for exporting JMX technology instrumentation to remote applications based on Java Remote Method Invocation (Java RMI).


Monitoring an Application by Using JConsole

This procedure shows how to monitor the Notepad Java application. Under releases of the Java SE platform prior to version 6, applications that you want to monitor with JConsole need to be started with the following option.
-Dcom.sun.management.jmxremote
However, the version of JConsole provided with the Java SE 6 platform can attach to any local application that supports the Attach API. In other words, any application that is started in the Java SE 6 HotSpot VM is detected automatically by JConsole, and does not need to be started using the above command-line option.
  1. Start the Notepad Java application, by using the following command in a terminal window:
    java -jar 
        jdk_home/demo/jfc/Notepad/Notepad.jar
    
    Where jdk_home is the directory in which the Java Development Kit (JDK) is installed. If you are not running version 6 of the Java SE platform, you will need to use the following command:
    java -Dcom.sun.management.jmxremote -jar 
          jdk_home/demo/jfc/Notepad/Notepad.jar
    
  2. Once Notepad has opened, in a different terminal window, start JConsole by using the following command:
    jconsole
    
    A New Connection dialog box is displayed.
  3. In the New Connection dialog box, select Notepad.jar from the Local Process list, and click the Connect button.JConsole opens and connects itself to the Notepad.jar process. When JConsole opens, you are presented with an overview of monitoring and management information related to Notepad. For example, you can view the amount of heap memory the application is consuming, the number of threads the application is currently running, and how much central procesing unit (CPU) capacity the application is consuming.
  4. Click the different JConsole tabs.Each tab presents more detailed information about the different areas of functionality of the Java VM in which Notepad is running. All the information presented is obtained from the various JMX technology MXBeans mentioned in this trail. All the platform MXBeans can be displayed in the MBeans tab. The MBeans tab is examined in the next section of this trail.
  5. To close JConsole, select Connection -> Exit.
MBEAN

This lesson introduces the fundamental concept of the JMX API, namely managed beans, or MBeans.
An MBean is a managed Java object, similar to a JavaBeans component, that follows the design patterns set forth in the JMX specification. An MBean can represent a device, an application, or any resource that needs to be managed. MBeans expose a management interface that consists of the following:
  • A set of readable or writable attributes, or both.
  • A set of invokable operations.
  • A self-description.
The management interface does not change throughout the life of an MBean instance. MBeans can also emit notifications when certain predefined events occur.
The JMX specification defines five types of MBean:
  • Standard MBeans
  • Dynamic MBeans
  • Open MBeans
  • Model MBeans
  • MXBeans
The examples in this trail demonstrate only the simplest types of MBean, namely standard MBeans and MXBeans.