JVM Runtime

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

getDSNs   get data of DSNs in the current cf admin
getSessionCount   gets data of all current sessions
getAppSessionCount   gets data of all current sessions
getJavaMemoryInfo   gets memory usage of the underlying java runtime
getProcessorCount   gets number of processors (that the java runtime can access) in the machine
getServerName   gets the servername from the system
getCFInstanceName   return the instance name, useful in clusters to see which instance is handling the request
restartCF   restart jrun 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>



Ginger CMS
the future of cms, a simple and intuitive content management system ...

ASP.NET MVC Application
best practices like Repository, LINQ, Dapper, Domain objects ...

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

Search Engine LITE
create your own custom search engine for your web site ...

JRun monitor
monitors the memory footprint of JRun engine and auto-restarts a hung engine ...

Validation Library
complete validation library for your web forms ...