« Cisco Disco | Main | Photos »

GlassFish : Self Management Rules

Self Management is an initiative taken in GlassFish to reduce the complexity of the management tasks. Mundane management tasks are automated by means of self-configuring. System Administrators who are responsible for monitoring the application server for unpredictable conditions no longer have to detect them manually. This is an easy-to-use feature that comes in handy at the unpredictable run-time conditions. This feature enables to recover from failures by detecting and preventing them, thus improving availability (self-healing). Even security threats are detected and self-protective actions are taken in response. The framework has been developed using standard technologies.

Self Management comprises of a set of management rules. You could create different rules based on the various event types. As mentioned at glassfish.dev.java.net, the different type of events are as follows.
  • Lifecycle events
  • Monitor events
  • Log events
  • Trace events
  • Timer events
  • Notification events
This blog is more about how to create management rules easily. I will show examples for some of the different type of rules. The action we take in response to the occurrence of an event, could be anything from sending an email notification to displaying an alert or killing some processes depending on what is appropriate for the particular event.

Let us take an example of displaying a pop-up window as an alert. Action mBeans implement the NotificationListener interface. The handleNotification() method for our action will look like the following.

    public synchronized void handleNotification(Notification notification,Object handback) {
        try {
            JFrame. setDefaultLookAndFeelDecorated(true);
            JFrame myFrame = new JFrame("MyAction");
            myFrame. setSize(250, 180);
            myFrame. setLocation(300,200);
            myFrame. getContentPane().add( new Label("Action has been invoked",Label.CENTER), BorderLayout.CENTER);
            myFrame. setVisible(true);
            myFrame. show();
        } catch (Exception ex) { // Log the exception }

Let us name this class as CustomAction which is in the com.sun.myrule.action package. It has a corresponding CustomActionMBean. Follow these steps to deploy the action on the application server.

1. Compile the files and make a jar out of these two files.

2. After starting the application server, open the admin console by typing http://localhost:4848 on a browser.

Figure1: Admin Console

3. On the left pane, click on "Custom MBeans". On the right pane, all the deployed mbeans are listed. To deploy our action mbean, click on Deploy. Select the jar file that you just created against the "File to upload" column and click Next.

Figure2: Custom MBeans

Figure3: Creating Custom MBeans

4. Enter a name for the mbean. This will later be used while creating the rule. Enter name as "CustomAction".

5. Enter the class name along with the package name. In this case, it is com.sun.myrule.action.CustomAction.

Figure4: Creating Custom MBeans: Step 2

6. Click Finish.

The action mbean is successfully deployed on the application server.

We will use the same action mbean for all the rules. This action mbean will be called each time an event is triggered. For more information on actions, refer to actions section in glassfish.dev.java.net

Now let us proceed and create rules.

Simple rules for creating a rule:

1. Identify the event type you need. For example : lifecycle
2. Identify the mandatory and optional properties for that event type. For example : lifecycle event requires a property called "name".
3. Identify the action mbean you would like to use for your rule.
4. You could use the Application server Command line interface (CLI) or the Graphical user interface (GUI) to create rules.
5. Make sure the server is started before proceeding with rule creation.

Example 1 : Assuming the administrator wants to be notified everytime the application server shuts down or is terminated because of a failure. Continuous monitoring is necessary for this kind of a situation. Therefore, a rule could be created mentioning a notification to be sent to the administrator when this event is triggered. Management rule bundles together the event and the action.

Let us examine how this rule could be created.

After starting the application server with the command "asadmin start-domain", use either CLI or GUI to create the rule.

CLI mode :

/bin/asadmin create-management-rule
--host localhost --port 4848 --user admin
--action CustomAction
--eventtype lifecycle
--eventloglevel WARNING --eventproperties name=shutdown

GUI mode :

  • Start admin console by typing http://hostname:port on a browser, after starting the application server.
  • Go to the Management Rules tab under the Configurations tab in the left pane. This displays the list of rules deployed on the server on the right pane.

  • Figure5 : Management Rules

  • Click on New
  • Type the name of the rule as "my_lifecycle_rule".
  • Check the Status checkbox to enable the rule.
  • Enter description
  • Choose event type as lifecycle.
  • Click on Next if you are done with this page.

  • Figure6 : New Management Rule

  • In the next page, select "shutdown" against Event and enter a description if desired.
  • Select "CustomAction" listed in the action list box and click Finish.

Figure7 : New Management Rule : Step 2

The rule is successfully created and is deployed on the application server. The action will be triggered everytime the application server shuts down. Similarly, rule could be configured to take some action on ready and termination.

Example 2 : The administrator wants to be beeped once in a while regarding some status. A rule could be created to do this as follows.

CLI mode :

/bin/asadmin create-management-rule
--host localhost --port 4848 --user admin
--action CustomAction
--eventtype timer
--eventloglevel INFO
--eventproperties pattern=MM\\\\/dd\\\\/yyyy\\\\/HH\\\\:mm\\\\:ss:

Note here that "/" and ":" are escape sequences. Period, mentioned in milliseconds, is the period after which the notification is to be sent after the date mentioned in datestring.

GUI mode :
  • Start admin console by typing http://hostname:port on a browser, after starting the application server.
  • Go to the Management Rules tab under the Configurations tab in the left pane. This displays the list of rules deployed on the server on the right pane.
  • Type the name of the rule as "my_timer_rule".
  • Check the Status checkbox to enable the rule.
  • Enter description
  • Choose event type as timer.
  • Click on Next if you are done with this page.
  • Enter the values for datestring, pattern and other fields.
  • Select "CustomAction" listed in the action list box and click Finish.
Another example of a rule to monitor the runtime memory of the JVM can be found here .

« Cisco Disco | Main | Photos »

How do you capture the log events broadcasted by Glassfish ....no documentation on that

Posted by akhilesh on March 04, 2008 at 03:51 PM IST #

Can you publish the code of the "corresponding MBean". This would help to find out how to capture the log events ...

Posted by batzee on March 06, 2008 at 07:43 AM IST #

The log events could be captured by setting the type to "log". The corresponding attributes are "loggerName" and "level".

Posted by Shalini on March 13, 2008 at 09:24 AM IST #


Posted by guest on March 13, 2009 at 05:02 AM IST #

Shalini, can you pls share the code of the action jar file created for server shutdown since i am trying for the same and would be of much help to write down similar actions

Posted by kishore on April 17, 2009 at 01:56 AM IST #

Do I need to recreate a timer rule if I change amd redeploy the used MBean?

Posted by Gabriel Escamilla on September 29, 2009 at 01:30 PM IST #

Enter a Valid Date, Make sure date range is valid as per the entered month, year... This happens when I want to edit/create using GUI. How can I fix this?

Posted by Gabriel Escamilla on September 29, 2009 at 01:47 PM IST #

I'm unable to create a jar with a CustomAction and CustomActionMBean that the Glassfish admin console will accept, javax.management.NotCompliantMBeanException: Class com.example.CustomAction is not a JMX compliant Standard MBean

I'd very much like to see the source used above.

Posted by Jeff Sexton on October 20, 2009 at 06:05 PM IST #

import javax.management.Notification;
import javax.management.NotificationListener;

on your MBean class (say MyClass), it needs to implement your MBean interface (say MyClassMBean) as well as NotificationListener. Your MBean interface must have the handleNotification method:

public void handleNotification(Notification notification,Object handback);

Hope this helps

Posted by Gabriel Escamilla on October 20, 2009 at 06:24 PM IST #


I am trying to monitor my instances through jmx and management rules. I have read that a instance in start mode would give state value of 1 and in stop mode would give a state value of 3

Here is the MBean code to get a state value:

public String getInstanceStateValue() {
JMXServiceURL jmxsurl;
jmxsurl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + localhost + ":" + 8686 + "/jmxrmi");
Map env = new HashMap();
String[] credentials = new String[]{"admin", "adminadmin"};
env.put(JMXConnector.CREDENTIALS, credentials);
JMXConnector connector = JMXConnectorFactory.connect(jmxsurl, env);
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
ObjectName instanceStateValue = new ObjectName("amx:j2eeType=J2EEServer,name=instance1");
stateValue = mbsc.getAttribute(instanceStateValue, "state").toString();
System.out.println("State Value: " + stateValue);
return stateValue;

I am able to retreive the 'state' value alright.

Here the entry from domain.xml for the corresponding management rule:

<management-rule enabled="true" name="instance_monitor">
<event level="WARNING" record-event="true" type="monitor">
<property name="observedmbean" value="NAMBean"/>
<property name="observedattribute" value="StateValue"/>
<property name="monitortype" value="stringmonitor"/>
<property name="stringnotify" value="notifydiffer"/>
<property name="stringtocompare" value="2"/>
<action action-mbean-name="NAMBeanAction"/>

When i start the server i see a error message: "Observed attribute must be accessible by the MBean".

On stopping the server i see no error message.

Could you help me out here.?

Posted by Vaibhav Dhawan on December 15, 2009 at 06:05 AM IST #


Please note that my event is fired only once. After 5 seconds (Granularity period : 5000), even though free memory value is above specified high value it is not fired.

Please help me.

Posted by paresh bhavsar on January 02, 2011 at 06:18 AM IST #

Post a Comment:
  • HTML Syntax: NOT allowed



« July 2016