Java Reference
In-Depth Information
e.printStackTrace();
logLevel = 0;
}
return name;
}
B
}//class
B
The methods that appear in bold are declared in the
MBeanRegistration
interface.
For the first three, the
Logger
MBean did not provide an implementation. How-
ever, the
preRegister()
method is implemented to get its initial
logLevel
attribute
value from a
PropertyManager
MBean present in the MBean server passed in as an
argument to this method.
To find the value for the
logLevel
attribute, the
Logger
MBean must invoke the
getProperty()
method of a registered
PropertyManager
MBean. For this example,
the
Logger
MBean assumes that the object name
HelloAgent:name=props
will cor-
respond to a
PropertyManager
MBean. In the code, the appropriate method signa-
ture is created to allow the MBean server to invoke the
getProperty()
method.
The
Logger
MBean invokes the MBean server's
invoke()
method with the appro-
priate parameters.
When you're creating an MBean that depends on the existence of an-
other MBean, you need to implement some default behavior in case the
necessary MBean does not exist. For example, the
Logger
MBean must
ensure that its attributes have appropriate values if an exception occurs
when invoking methods on the
PropertyManager
MBean.
WARNING
The
MBeanRegistration
interface is useful for acquiring a reference to the con-
taining MBean server. In addition, because it declares methods that are invoked
before an MBean is removed from the MBean server, implementing MBeans can
be informed when to clean up resources before the MBean is removed from the
MBean server.
4.5 Handling MBean errors
In each example in this chapter, there is an opportunity to catch an exception.
Each MBean contains a generic
try-catch
statement: