Sunday, February 9, 2014

Learning JMX

http://docs.oracle.com/javase/7/docs/technotes/guides/management/agent.html
-Dcom.sun.management.jmxremote.port=55555
-Dcom.sun.management.jmxremote.authenticate=false

-Dcom.sun.management.jmxremote.ssl=false

Java MXBean custom types
http://stackoverflow.com/questions/14939335/java-mxbean-custom-types

Each resource that is to be managed must provide a management interface, which consists of the attributes and operations it exposes so that it can be monitored and controlled by a management application.

Standard MBeans
The management interface of the resource must have the same name as the resource's Java class, followed by "MBean"; it must be defined as a Java interface; and it must be implemented by the resource to be managed using the implements keyword.
The implementing class must contain at least one public constructor.
Getters and setters for attributes on the management interface must follow strict naming conventions.
The management interface is contained in its own .java file and must have the same name as its corresponding interface. Thus, every standard MBean requires at least two source code files: one for the interface and one for the class that implements the interface.

While an MBean may inherit the public (and protected) attributes and operations of its parent class, it will not necessarily inherit its management interface.
When this pattern is used, the introspection performed by the MBean server proceeds up the MBean interface inheritance graph, not the implementing inheritance graph.
Simple inheritance with MBean inheritance

Common Mistakes Not Caught by Introspection
MBean interface not given public visibility

there are three levels to the JMX architecture: instrumentation, agent, and distributed services. 
The ObjectName class

The ObjectName class is provided by the RI and is crucial to the MBean registration process. Every MBean must be represented by an ObjectName in the MBean server and no two MBeans may be represented by the same ObjectName. Each ObjectName contains a string made up of two components: the domain name and the key property list.
domain-name:key1=value1[,key2=value2,...,keyN=valueN]
DefaultDomain:name=Queue
Every compliant JMX implementation must provide a default domain name. For the JMX 1.0 RI, that name is DefaultDomain, but you can't depend on this to be the case all of the time.

There is only one restriction on domain names: you cannot use JMImplementation as the domain name for your MBeans. This domain name is reserved for the implementation (hence the name) and contains a single metadata MBean that provides information about the implementation, such as its name, version, and vendor.
String myObjName = ":Name=Worker,Role=Supplier";
ObjectName = new ObjectName(myObjName);
ObjectName = new ObjectName("UserDomain", "Name", "Controller");

The first step in using the MBean server is to obtain a reference to it. 
MBeanServer server = MBeanServerFactory.createMBeanServer(  );
MBeanServer server = MBeanServerFactory.createMBeanServer(  );
  ObjectName objName = new ObjectName("UserDomain:Name=Controller");
  Controller controller = new Controller(  );
  server.registerMBean(controller, objName);

server.createMBean("sample.standard.Controller", objName);
Dynamic MBeans
Standard MBeans are well suited for management interfaces that are relatively static. However, if a management interface must be defined for an existing resource, is likely to evolve over time, or for some other reason needs to be exposed at runtime, 

When the MBean server is asked to register a dynamic MBean, however, no introspection is performed.
Instead of using a Java interface with the name "MBean" on it, dynamic MBeans use metadata classes to expose their management interfaces. They make that metadata available through an interface called DynamicMBean, which must be implemented by all dynamic MBeans. 

Instead of using a Java interface with the name "MBean" on it, dynamic MBeans use metadata classes to expose their management interfaces. They make that metadata available through an interface called DynamicMBean, which must be implemented by all dynamic MBeans. 
  

















Open MBeans
By using open MBeans, we can instrument application resources of any type and make them available to any agent or management application that does not have access to the bytecode for either the resource, attribute, or operation parameter. The agent or management application can even be a non-Java program!

The open MBean types are at the heart of what makes open MBeans "open." The JMX RI defines a base class, OpenType, from which all open MBean types are derived. This ensures consistency among all of the subsequent types.

OpenType

This class is the base class for all open MBean types
Type name
The name that has been assigned to the data type represented by this OpenType instance. Must be unique across all other open MBean types.
Class name
The fully qualified name of the Java class that the OpenType instance represents (e.g., java.lang.Integer is the class name for integer data types).

SimpleType
This class is a subclass of OpenType (as are all valid open MBean types) that is used to represent the following open MBean types:
OpenType openType = SimpleType.LONG;

Other basic types
ArrayType
CompositeType
TabularType
public CompositeData createCompositeDataObject(  ) {
  try {
  CompositeType buildingType = new CompositeType(
    "BuildingCompositeType",
    "CompositeType that represents a Building.",
    itemNames,
    itemDescriptions,
    itemTypes
  );
  
    String[] itemNames = {
      "Name",
      "NumberOfFloors",
      "Height",
      "UndergroundParking",
      "NumberOfElevators",
      "OfficeSpace"
    };
    Object[] itemValues = {
      "Building A",
      new Short(3),
      new Integer(45),
      new Boolean(false),
      new Short(1),
      new Long(10000)
    };
    CompositeData buildingData = new CompositeDataSupport(
      buildingType, // See Example 5-1
      itemNames,
      itemValues
    );
    return buildingData;
  } catch (Exception e) {
    // . . .
  }
}

No comments:

Post a Comment