The sample JavaScript programs in this section show how to use GlassFish Server events to generate and present statistics for system administrators who are monitoring GlassFish Server.
This example uses the glassfish:web:jsp:jspLoadedEvent event to count the number of JavaServer Pages (JSP) technology pages that GlassFish Server has loaded.
var njspLoaded=0; function jspLoaded(hostName) { njspLoaded = njspLoaded + 1; client.print( '\n js> jsp loaded event called on ' + 'host = ' + hostName + ' and count = ' + njspLoaded); } params = java.lang.reflect.Array.newInstance(java.lang.String, 1); params[0]="hostName"; scriptContainer.registerListener('glassfish:web:jsp:jspLoadedEvent', params, 'jspLoaded');
This script can be run with a command similar to the following:
asadmin run-script jsp-loaded-count.js |
Information similar to the following is displayed each time that GlassFish Server loads a JSP technology page:
js> jsp loaded event called on host = server and count = 1 |
The script runs until a user types Ctrl-C to stop the script.
This example uses the following events to measure the time to process web service requests:
glassfish:web:http-service:requestStartEvent
glassfish:web:http-service:requestEndEvent
The script also displays the information that is contained in the parameters of these events.
// http request related probes // glassfish:web:http-service:requestStartEvent requestStartEvent( // java.lang.String appName, // java.lang.String hostName, // java.lang.String serverName, // int serverPort, // java.lang.String contextPath, // java.lang.String servletPath) request_params = java.lang.reflect.Array.newInstance(java.lang.String, 6); request_params[0]="appName"; request_params[1]="hostName"; request_params[2]="serverName"; request_params[3]="serverPort"; request_params[4]="contextPath"; request_params[5]="servletPath"; var startTime; var object = new Object(); var nrequestStartEvent=0; function requestStartEvent(appName,hostName,serverName,serverPort,contextPath, servletPath){ nrequestStartEvent=nrequestStartEvent+1; startTime = (new Date()).getTime(); //insert the request time in Map key = java.lang.Thread.currentThread().getId(); object[key] = startTime; client.print( 'Count: '+ nrequestStartEvent +'\n'+ 'Event: glassfish:web:http-service:requestStartEvent' +'\n'+ 'Application: '+appName+'\n'+ 'Host: ' + hostName +'\n'+ 'Server: ' + serverName +'\n'+ 'HTTP Port: ' + serverPort +'\n'+ 'Context Path: ' + contextPath +'\n'+ 'Servlet Path: ' + servletPath + '\n' + 'Current Thread: ' + java.lang.Thread.currentThread().getId() + '\n\n'); } scriptContainer.registerListener('glassfish:web:http-service:requestStartEvent', request_params , 'requestStartEvent'); // glassfish:web:http-service:requestEndEvent requestEndEvent( // java.lang.String appName, // java.lang.String hostName, // java.lang.String serverName, // int server Port, // java.lang.String contextPath, // java.lang.String servletPath, // int statusCode) request1_params = java.lang.reflect.Array.newInstance(java.lang.String, 7); request1_params[0]="appName"; request1_params[1]="hostName"; request1_params[2]="serverName"; request1_params[3]="serverPort"; request1_params[4]="contextPath"; request1_params[5]="servletPath"; request1_params[6]="statusCode"; var nrequestEndEvent=0; function requestEndEvent(appName,hostName,serverName,serverPort,contextPath, servletPath,statusCode){ nrequestEndEvent=nrequestEndEvent+1; 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; client.print( 'Time Taken: ' + ((new Date()).getTime()-startTime) + ' ms\n' + 'Count: '+nrequestEndEvent+'\n'+ 'Event: glassfish:web:http-service:requestEndEvent' +'\n'+ 'Application: '+appName+'\n'+ 'Host: ' + hostName +'\n'+ 'Server: ' + serverName +'\n'+ 'HTTP Port: ' + serverPort +'\n'+ 'Context Path: ' + contextPath +'\n'+ 'Servlet Path: ' + servletPath +'\n'+ 'Status Code: ' + statusCode + '\n' + 'Current Thread: ' + java.lang.Thread.currentThread().getId() + '\n' + '\n\n'); } scriptContainer.registerListener('glassfish:web:http-service:requestEndEvent', request1_params, 'requestEndEvent');
This script can be run with a command similar to the following:
asadmin run-script web-service-request-timer.js |
Information similar to the following is displayed each time that a web service request is initiated:
Count: 2 Event: glassfish:web:http-service:requestStartEvent Application: __admingui Host: __asadmin Server: localhost HTTP Port: 4848 Context Path: Servlet Path: /common/commonTask.jsf Current Thread: 98 |
Information similar to the following is displayed each time that a web service request is completed:
Time Taken: 1704 ms Count: 2 Event: glassfish:web:http-service:requestEndEvent Application: __admingui Host: __asadmin Server: localhost HTTP Port: 4848 Context Path: Servlet Path: /common/commonTask.jsf Status Code: 200 Current Thread: 98 |
The script runs until a user types Ctrl-C to stop the script.