Skip to content

JMX

November 13, 2009
tags:

ModelMBean can be created by using any simple POJO.

ModelMBeanInfo = Interface, MBeanInfo     = Class

I was wondering – why does the MBeanInfo class not implement the ModelMBeanInfo interface?. MBeanInfo class does implement some methods defined in ModelMBeanInfo interface but not all the methods – like getDescriptors etc is not implemented, and in addition to that there are other methods like fastGetAttributes.

Java resources wishing to be manageable instantiate the ModelMBean using the MBeanServer’s createMBean method. The resource then sets the ModelMBeanInfo and Descriptors for the ModelMBean instance

A Java object cannot be registered in the MBeanServer unless it is a JMX compliant MBean. By instantiating a ModelMBean, resources are guaranteed that the MBean is valid. MBeanException and RuntimeOperationsException must be thrown on every public method. This allows for wrapping exceptions from distributed communications (RMI, EJB, etc.)

The contents of the MBeanInfo for a Dynamic MBean are determined by its getMBeanInfo() method. This includes Open MBeans and Model MBeans, which are kinds of Dynamic MBeans.

We need the following class instances

<div>ModelMBeanOperationInfo[] ops;
ModelMBeanAttributeInfo[] atts ;
ModelMBeanConstructorInfo[] cons;
ModelMBeanNotificationInfo[] notifs;

mmbinfo (intfc), mbinfo (class), mmbinfosupport (implementation of mmbinfo)


  return new ModelMBeanInfoSupport( "javax.management.modelmbean.ModelMBeanInfo", "description", atts, cons, ops, notifs, desc );
  ObjectName mName = new ObjectName("JMXBookAgent:name=Modeled");
  client.createMBean( "javax.management.modelmbean.RequiredModelMBean", mName );
 

Now once you have created an object of MMBISupport then you use it in the call of it’s setter method setModelMBeanInfo and only the is your bean ready to be used by other clients.

So the sequence is:

1. Create  an object of mmbiSupport and populate it (myInfo).

2. Call the createBean method and pass it your ObjectName. (Registration)

3. Call the setModelMBeanInfo method to set myInfo (Op. Exposer)

4. Call the setManagedResource method to set POJO instance.


String[] sig = { "java.lang.Object", "java.lang.String" };
Object[] params = { obj, "ObjectReference" };
client.invoke( mName, "setManagedResource", params, sig );

Java 5.0 JMX Api Doc

Useful class for building  – ModelMBeanInfoBuilder.java

Code for  creating ModelMbean

  //Step 1: Create your object
			DBModel mypojo = new DBModel();

			//Step 2: Expose it through ModelMBeanInfo
		    ModelMBeanInfoBuilder builder = new ModelMBeanInfoBuilder();
		    //Descriptor attDesc = builder.buildAttributeDescriptor( "MyAttribute", null, "always", "10", null, "getMyAttribute", null, "10" );
		    Descriptor attDesc = builder.buildAttributeDescriptor( "MaxConn","Max Connection","never","0","0","getTotalConn",null,"-1");
		    builder.addModelMBeanAttribute( "MaxConn", "java.lang.Integer", true, false, false, "Get the max connection", attDesc );
Descriptor opGetDesc = builder.buildOperationDescriptor( "getTotalConn", null, "getter", null, null, "mypackage.jmx.DBModel", "10" );
		    builder.addModelMBeanMethod( "getTotalConn", null, null, null, "", "java.lang.String" , MBeanOperationInfo.INFO, opGetDesc );
		    Descriptor mbeanDesc = builder.buildMBeanDescriptor( "DB", "SampleDBModel", "never", "-1", null ,null, null, null );
		    ModelMBeanInfo myinfo = builder.buildModelMBeanInfo( mbeanDesc );

		    //Step 3: Register it
		    ObjectName oname = new ObjectName(dbDomainName);
		    MBeanServer mbs;
		    List mbsArl = MBeanServerFactory.findMBeanServer(null);
			if(mbsArl.size() > 0)
			{
				mbs = (MBeanServer)mbsArl.get(0);
				System.out.println("Got existing mbean server . . . .");
			}
			else
				mbs = ManagementFactory.getPlatformMBeanServer();

                     /mbs.createMBean("javax.management.modelmbean.RequiredModelMBean", oname);
		     RequiredModelMBean modelmbean =	new RequiredModelMBean( myinfo );
		    // Associate it with the resource (a TestBean instance)
		    modelmbean.setManagedResource(  mypojo, "objectReference");
		    //modelmbean.setModelMBeanInfo(myinfo);

         	    mbs.registerMBean(modelmbean, oname);
                            /* The below code does not work ! - so have commented it */
                              mbs.createMBean("javax.management.modelmbean.RequiredModelMBean", oname);
				//Step 4: Call the setters for our POJO and INFO objects.

				//Set our POJO
			    String[] sig = { "java.lang.Object", "java.lang.String" };
			    Object[] params = { mypojo, "ObjectReference" };
			    mbs.invoke( oname, "setManagedResource", params, sig );

				//Set our INFO
			    sig = new String[ 1 ];
			    sig[ 0 ] = "javax.management.modelmbean.ModelMBeanInfo" ;
			    params = new Object[ 1 ];
			    params[ 0 ] = myinfo;
			    mbs.invoke( oname, "setModelMBeanInfo", params, sig ); */

PersistPeriod: Specifies the number of seconds that the OnTimer or NoMoreOftenThan persistence policies use. If you do not specify this attribute in the MBeanType or

MBeanAttribute elements, the assumed value is 0.If PersistPolicy is set to OnTimer, then the attribute is persisted when the number of seconds expires. If PersistPolicy is set to

NoMoreOftenThan, then persistence is constrained to happen not more often than the specified number seconds.
Note: When specified in the MBeanType element, this value overrides any setting within an individual MBeanAttribute subelement.

PersistPolicy: Specifies how persistence will occur

Never. The attribute is never stored. This is useful for highly volatile data or data that only has meaning within the context of a session or execution period.
OnTimer. The attribute is stored whenever the MBean type’s persistence timer, as defined in the PersistPeriod attribute, expires.
OnUpdate. The attribute is stored every time the attribute is updated.
NoMoreOftenThan. The attribute is stored every time it is updated unless the updates are closer together than the PersistPeriod. This mechanism helps prevent temporarily highly

volatile data from affecting performance.
OnUnregister, that means that the value of the attribute is persisted when the MBean containing it is unregistered from the MBean Server

currencyTimeLimit: How long an attribute value is valid before needing to be refreshed.
0 = a seconds value

If you do not specify this attribute in the MBeanType or MBeanAttribute elements, the assumed value is Never. Note: When specified in the MBeanType element, this value overrides any setting within an individual MBeanAttribute subelement.

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: