Java Reference
In-Depth Information
"modeledClass",
"", "always", "10", "." ,"ModeledClass",
null, null );
C
Build MBean
descriptor
ModelMBeanInfo info =
builder.buildModelMBeanInfo( mbeanDesc );
RmiConnectorClient client = RMIClientFactory.getClient();
ObjectName mName = new ObjectName("JMXBookAgent:name=Modeled");
client.createMBean(
"javax.management.modelmbean.RequiredModelMBean",
mName );
String[] sig = { "java.lang.Object", "java.lang.String" };
Object[] params = { obj, "ObjectReference" };
client.invoke( mName, "setManagedResource", params, sig );
sig = new String[ 1 ];
sig[ 0 ] = "javax.management.modelmbean.ModelMBeanInfo" ;
params = new Object[ 1 ];
params[ 0 ] = info;
client.invoke( mName, "setModelMBeanInfo", params, sig );
Set resource
and MBean info
D
//store the MBean
client.invoke( mName,"store",null,null );
}
}
Persist MBean
The
ModeledClass
class contains the method
getMyAttribute()
, which the cre-
ated
ModelMBean
will use as an attribute. The class also contains the method
printAttribute()
, which will be modeled as an operation. The
main()
method is
used to create a test of a
ModelMBean
managing an instance of this class.
After creating an instance of the
ModeledClass
, the
main()
method uses the
Model-
MBeanInfoBuilder
utility class to build a
ModelMBeanInfo
object. The
main()
method
first adds the attribute
MyAttribute
to the builder. To do this, it creates a
Descrip-
tor
object using the
buildAttributeDescriptor()
method, passing in the various
values to initialize its predefined fields. In this case, the
main()
method must
describe the attribute and indicate that its getter method is named
getMyAt-
tribute()
. After the descriptor has been built, it is added to the builder to create
an attribute entry for the
ModelMBeanInfo
object under construction.
Because the attribute has been added, the
main()
method must now create an
operation entry in the builder for the attribute's getter method,
getMyAt-
tribute()
. You add operations to the builder much the way you add attributes: by
building the descriptor and then calling the correct add-metadata-object method.
B