![]() ![]() ![]() ![]() ![]() ![]() |
The following sections describe how to instrument and register standard MBeans for application modules:
Figure 4-1 illustrates the MBean development process. The steps in the process, and the results of each are described in Table 4-1. Subsequent sections detail each step in the process.
One of the main advantages to the standard MBeans design pattern is that you define and implement management properties (attributes) as you would any Java property (using get
xxx
, set
xxx
, and is
xxx
methods); similarly, you define and implement management methods (operations) as you would any Java method.
When you register the MBean, the MBean server examines the MBean interface and determines how to represent the data to JMX clients. Then, JMX clients use the MBeanServerConnection.getAttribute()
and setAttribute()
methods to get and set the values of attributes in your MBean and they use MBeanServerConnection.invoke()
to invoke its operations. See
MBeanServerConnection
in the J2SE 5.0 API Specification.
To create an interface for your standard MBean:
Business-object
MBean.java
where Business-object
is the object that is being managed.
BEA’s recommended design pattern for standard MBeans enables you to follow whatever naming convention you prefer. In other standard MBean design patterns (patterns in which the MBean’s implementation file does not extend javax.management.StandardMBean
), the file name must follow this pattern: Impl-file
MBean.java
where Impl-file
is the name of the MBean’s implementation file.
get
Attribute-name
set
Attribute-name
where Attribute-name
is a case-sensitive name that you want to expose to JMX clients.
If your coding conventions prefer that you use an is
Attribute-name
as the getter method for attributes of type Boolean
, you may do so. However, JMX clients use the MBeanServerConnection.getAttribute()
method to retrieve an attribute’s value regardless of the attribute’s data type; there is no MBeanServerConnection.isAttribute()
method.
is
or a getter method.For each write-only attribute, define only a setter method.
Listing 4-1 is an MBean interface that defines a read-only attribute of type int
and an operation that JMX clients can use to set the value of the attribute to 0
.
package com.bea.medrec.controller;
public interface RecordSessionEJBMBean {
public int getTotalRx();
public void resetTotalRx();
}
BEA recommends the following pattern as a naming convention for implementation files: MBean-Interface
Impl.java
.
javax.management.StandardMBean
to enable this flexibility in the naming requirements.
See
StandardMBean
in the J2SE 5.0 API Specification.
StandardMBean(Object implementation, Class mbeanInterface)
constructor.
With BEA’s recommended design pattern in which you separate the management logic into a delegate class, you must provide a public constructor that implements the StandardMBean(Object implementation, Class mbeanInterface)
constructor.
incrementTotalRx()
method is available to business methods but it is not part of the management interface.package com.bea.medrec.controller;
import javax.management.StandardMBean;
import com.bea.medrec.controller.RecordSessionEJBMBean;
public class RecordSessionEJBMBeanImpl extends StandardMBean
implements RecordSessionEJBMBean {
public RecordSessionEJBMBeanImpl() throws
javax.management.NotCompliantMBeanException {
super(RecordSessionEJBMBean.class);
}
public int TotalRx = 0;
public int getTotalRx() {
return TotalRx;
}
public void incrementTotalRx() {
TotalRx++;
}
public void resetTotalRx() {
TotalRx = 0;
}
}
If your management attributes contain data about the number of times a business method has been invoked, or if you want management attributes to contain the same value as a business property, modify your business methods to push (update) data into the management implementation class.
Listing 4-3 shows a method in an EJB that increments the integer in the TotalRx
property each time the method is invoked.
private Collection addRxs(Collection rXs, RecordLocal recordLocal)
throws CreateException, Exception {
...
com.bea.medrec.controller.RecordSessionEJBMBeanImpl.incrementTotalRx();
...
}
If you want to instantiate your MBeans as part of application deployment, create an ApplicationLifecycleListener
that registers your MBean when the application deploys (see Use ApplicationLifecycleListener to Register Application MBeans):
weblogic.application.ApplicationLifecycleListener
.ApplicationLifecycleListener
class, implement the ApplicationLifecycleListener.postStart(ApplicationLifecycleEvent evt)
method.In your implementation of this method:
BEA recommends this naming convention:your.company
:Name=
Parent-module
,Type=
MBean-interface-classname
To get the name of the parent module, use ApplicationLifecycleEvent
to get an ApplicationContext
object. Then use ApplicationContext
to get the module’s identification.
If the classes for the JMX client are part of a Java EE module, such as an EJB or Web application, then the JNDI name for the Runtime MBeanServer is:java:comp/env/jmx/runtime
If the classes for the JMX client are not part of a Java EE module, then the JNDI name for the Runtime MBean Server is:java:comp/jmx/runtime
InitialContext ctx = new InitialContext();
MBeanServer server = (MBeanServer)
ctx.lookup("java:comp/env/jmx/runtime");
See Make Local Connections to the Runtime MBean Server in Developing Custom Management Utilities with JMX.
MBeanServer.registerMBean(Object
object
, ObjectName
name
)
where:
object
is an instance of your MBean implementation class.
name
is the JMX object name for your MBean.
When your application deploys, the WebLogic deployment service emits ApplicationLifecycleEvent
notifications to all of its registered listeners. When the listener receives a postStart
notification, it invokes its postStart
method. See
Programming Application Lifecycle Events in Developing Applications with WebLogic Server.
ApplicationLifecycleListener.preStop(ApplicationLifecycleEvent evt)
method.
In your implementation of this method, invoke thejavax.management.MBeanServer.unregister(ObjectName
MBean-name
)
method to unregister your MBean.
ApplicationLifecycleListener
by adding the following element to your application’s weblogic-application.xml
file:
<listener>
<listener-class>
fully-qualified-class-name
</listener-class>
</listener>
For an example of this technique, see the Medrec example server.
Package your MBean classes in the application’s APP-INF
directory or in a module’s JAR, WAR or other type of archive file depending on the access that you want to enable for the MBean. See Additional Design Considerations.
![]() ![]() ![]() |