GlassFish Project - Self Management - Example rule and related code

 New to GlassFish | Community Guidelines  | Downloads | FAQ How-Tos

Let us take an example of monitoring the free runtime memory of the JVM. If the value falls below a certain threshold, a popup window is displayed. A monitor type event is created and the monitor type chosen is gaugemonitor. Other monitor types are countermonitor and stringmonitor. An mBean is created which gets the free memory of the JVM at a particular point of time. To register the mbean with the application server, either the CLI command create-mbean or the GUI could be used.

A typical mbean will look like the following. 

    package com.sun.example.mbeans;
    public class Memory implements MemoryMBean {
        public Memory() {}
        public long getFreeMemory() {
            freeMemory = Runtime.getRuntime().freeMemory();
            return freeMemory;
        private long freeMemory = 0;

    package com.sun.example.mbeans;
    public interface MemoryMBean {
        public long getFreeMemory();

CLI command to create an mbean:

  asadmin create-mbean --user admin_user --port [4848 | 4849] --name MemoryBean com.sun.example.mbeans.Memory

Creating an mbean using GUI:

An action mbean is written which will popup a window whenever an event is triggered. This mbean implements the NotificationListener interface.

    package com.sun.example.action;
    public class MemoryAction implements NotificationListener, MemoryActionMBean {
        public Memory() {}
        public synchronized void handleNotification(Notification notification,Object handback) {
            try {
                JFrame myFrame = new JFrame("Glassfish");
                myFrame.setSize(250, 180);
                if(notification.getType().equals(THRESHOLD_LOW_VALUE_EXCEEDED)) {
                    myFrame.getContentPane().add(new Label("Running out of Free Memory",Label.CENTER), BorderLayout.CENTER);
                } else {
                    myFrame.getContentPane().add(new Label("More Free Memory than usual",Label.CENTER), BorderLayout.CENTER);
            } catch (Exception ex) { // Log the exception }

    public interface MemoryActionMBean {

CLI command to create action mbean:

    asadmin create-mbean --user admin_user --port 4848 --name MemoryActionBean com.sun.example.action.MemoryAction

Creating Action Mbean using GUI:

Same procedure as mentioned above is followed for creating the action mbean and deploying it on the application server.

A management rule is created with monitor type as gaugemonitor. Low and high threshold values ( values that are checked against the derived gauge ) are set in the configuration. Observed object is the mbean which gets the free memory of the JVM at any time. (that is, com.sun.example.mbeans.Memory) and the attribute we will observe is FreeMemory. The action triggered would be that of com.sun.example.action.Memory.

CLI command to create this rule:

    asadmin create-management-rule --port 4848 --user admin_user --action MemoryActionBean
    --eventtype monitor --eventloglevel WARNING    --eventproperties

Note here that the "eventproperties" has an attribute called observedobject. The objectName of the mbean that is observed is mentioned here. The object name will be created when create-mbean command is executed. observedobject could be replaced by observedmbean if an mbean name is to be specified.

Creating a Management Rule using GUI
The rule is now successfully created. The configuration file domain.xml has these details as follows.

        <management-rule enabled="true" name="memory_gauge_rule">
            <event level="WARNING" record-event="true" type="monitor">
                <property name="monitortype" value="gaugemonitor"/>
                <property name="lowthreshold" value="9000000"/>
                <property name="highthreshold" value="9700000"/>
                <property name="numbertype" value="long"/>
                <property name="observedobject" value="user:impl-class-name=com.sun.example.mbeans.Memory,name=MemoryMBean,server=server"/>
                <property name="observedattribute" value="FreeMemory"/>
            <action action-mbean-name="MemoryActionBean"/>

Once the server is restarted, whenever the free memory of the JVM goes below the low threshold, a popup window is displayed saying "Running out of free memory". When it goes above the threshold, a popup window saying "More free memory than usual" is displayed. The Low and High threshold values can be changed accordingly to check the rule.