JVM runtime

Ketan Jetty
enthusiasm for technology

JVM runtime

The following code shows to how to tap into the coldfusion's underlying jvm to get information about:

getDSNsget data of DSNs in the current cf admin
getSessionCountgets data of all current sessions
getAppSessionCountgets data of all current sessions
getJavaMemoryInfogets memory usage of the underlying java runtime
getProcessorCountgets number of processors (that the java runtime can access) in the machine
getServerNamegets the servername from the system
getCFInstanceNamereturn the instance name, useful in clusters to see which instance is handling the request
restartCFrestart jrun (coldfusion 8) programmatically

JVM Runtime usage in coldfusion
<cfcomponent>
	<!--- Get a list of DSNs in the current cf admin --->
	<cffunction access="public" returntype="String" name="getDSNs">
		<cfscript>
			var sf = createObject("java","coldfusion.server.ServiceFactory");
			var dsns = sf.getDataSourceService().getNames();
			return dsns;
		</cfscript>
	</cffunction>

	<!--- Get current sessions --->
	<cffunction name="getSessionCount" access="public" returntype="numeric">
		<cfset var oSession = createObject("java","coldfusion.runtime.SessionTracker")>
		<cfreturn oSession.getSessionCount()>
	</cffunction>

	<!--- Get current sessions per application --->
	<cffunction name="getAppSessionCount" access="public" returntype="numeric">
		<cfargument name="appName" type="string" required="true">
		<cfscript>
			var oSession = createObject("java","coldfusion.runtime.SessionTracker");
			var mySessions= oSession.getSessionCollection(arguments.appName);
			return StructCount(mySessions);
		</cfscript>
	</cffunction>

	<!--- getJavaMemoryInfo :: gets memory usage of the underlying java runtime  --->
	<cffunction name="getJavaMemoryInfo" access="public" returntype="struct">
		<cfscript>
			var runtime = createObject("java","java.lang.Runtime").getRuntime();
			var stMemInfo = structNew();

			stMemInfo.freeMemory = runtime.freeMemory();
			stMemInfo.maxMemory = runtime.maxMemory();
			stMemInfo.totalMemory = runtime.totalMemory();
			stMemInfo.heapMemory = runtime.totalMemory()-runtime.freeMemory();

			return stMemInfo;
		</cfscript>
	</cffunction>

	<!--- getProcessorCount :: gets number of processors (that the java runtime can access) in the machine. --->
	<cffunction name="getProcessorCount" access="public" returntype="numeric">
		<cfscript>
			var runtime = createObject("java","java.lang.Runtime").getRuntime();
			return runtime.availableProcessors();
		</cfscript>
	</cffunction>

	<!--- getServerName :: gets the servername from the system (should work in both windows and unix platforms) but I haven't tested it recently. --->
	<cffunction name="getServerName" access="public" returntype="string">
		<cfset var machineName = "">
		<cfset var factory = createObject("java", "coldfusion.server.ServiceFactory")>
		
		<cfif factory.runtimeservice.getServerScope().os.name CONTAINS "Windows">
			<cfregistry action="get"
						branch="HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\TCPIP\Parameters"
						entry="Hostname"
						variable="machineName"
						type="String" />
		<cfelse>
			<cfexecute name="/bin/hostname" variable="machineName" timeout="5" />
		</cfif>
		
		<cfreturn LCase(trim(machineName))>
	</cffunction>

	<!--- getCFInstanceName :: return the instance name - useful in clusters to see which instance is handling the request. --->
	<cffunction name="getCFInstanceName" access="public" returntype="string">
		<cfscript>
			var jrunObj = createObject("java", "jrunx.kernel.JRun");
			return jrunObj.getServerName();
		</cfscript>
	</cffunction>
	
	<!--- restartCF :: restart jrun --->
	<cffunction name="restartCF" access="public" returntype="void">
		<cfscript>
			var jrunObj = createObject("java", "jrunx.kernel.JRun");
			jrunObj.restart(jrunObj.getServerName());
		</cfscript>
	</cffunction>
</cfcomponent>

coldfusion


CF Quick Reference


Ginger CMS
the future of cms, a simple, easy and intutive content management system ... more


CFTurbine
cf prototyping engine, generates boilerplate code and ... more


Jrun monitor
monitor and timely auto-restart to avoid Jrun hang ... more


Inheritance Config.
uses OOPs inheritance to create configuration file ... more


Real Estate App.
complete real estate application using data from MLS ... more


Search Engine Lite
create your own search engine for your web site ... more