Oracle GlassFish Server 3.0.1 Monitoring Scripting Client Installation and Quick Start Guide

Writing an Event Callback Function

An event callback function is a function in a script that Monitoring Scripting Client calls in response to an event.

In your event callback functions, provide code to generate statistics from the data in events. Typically, the following types of statistics can be generated from the data in events:

ProcedureTo Generate Counter Statistics

Counter statistics typically correspond to a single event. For example, to calculate the number of received requests, only one event is required, for example, a “request received” event. Every time that a “request received” event is sent, the number of received requests is increased by 1.

  1. Declare and initialize a variable.

  2. Increase or decrease the variable each time the appropriate event is received.


Example 1–6 Generating a Counter Statistic

This example declares and initializes to zero the variable njspLoaded. Each time the callback function jspLoaded() is invoked, the value of this counter is increased by 1.

For the complete listing of the script from which this example is extracted, see Example 1–8.

var njspLoaded=0;

function jspLoaded(hostName) {
    njspLoaded = njspLoaded + 1;
    ...
}
...

ProcedureTo Generate a Timer Statistic

Timer statistics typically correspond to multiple events. For example, to calculate the time to process a request, two events are required, for example, a “request received” event and a “request completed” event.

For operations that have a measurable duration, Monitoring Scripting Client provides pairs of events to indicate the start and the end of the operations. For example, to indicate the initiation and completion of an HTTP request that has been received by the web container, Monitoring Scripting Client provides the following pair of events:

Use pairs of events that indicate the start and end of an operation to generate a timer statistic.

  1. Write an event callback function to calculate the start time.

  2. Ensure that the function to calculate the start time is called when the “operation started” event is received.

    For details, see To Register a Script as a Listener for an Event.

  3. Write an event callback function to calculate the end time.

  4. Ensure that the function to calculate the end time is called when the “operation ended” event is received.

    For details, see To Register a Script as a Listener for an Event.


Example 1–7 Generating a Timer Statistic

This example uses the following events to measure the time to process web service requests:

The events for a single request are sent in the same thread of control. Therefore, the identity of the thread can be used as a key to associate the start event and the end event for the request.

For the complete listing of the script from which this example is extracted, see Example 1–9.

...
var startTime;
var object = new Object();
...

function requestStartEvent(appName,hostName,serverName,serverPort,contextPath,
    servletPath){

	...
        startTime = (new Date()).getTime();
	//insert the request time in Map
        key = java.lang.Thread.currentThread().getId();
        object[key] = startTime;
        ...
}
scriptContainer.registerListener('glassfish:web:http-service:requestStartEvent',
    request_params , 'requestStartEvent');
...
function requestEndEvent(appName,hostName,serverName,serverPort,contextPath,
    servletPath,statusCode){

...
        key = java.lang.Thread.currentThread().getId();
        startTime = object[key];
        if (startTime == null)
            client.print("Error getting the startTime for thread = " + key);
        else
            delete[key];
        totalTime = (new Date()).getTime() - startTime;
        ...
}
scriptContainer.registerListener('glassfish:web:http-service:requestEndEvent', 
    request1_params, 'requestEndEvent');