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
	<!--- Get a list of DSNs in the current cf admin --->
	<cffunction access="public" returntype="String" name="getDSNs">
			var sf = createObject("java","coldfusion.server.ServiceFactory");
			var dsns = sf.getDataSourceService().getNames();
			return dsns;

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

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

	<!--- getJavaMemoryInfo :: gets memory usage of the underlying java runtime  --->
	<cffunction name="getJavaMemoryInfo" access="public" returntype="struct">
			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;

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

	<!--- 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"
						type="String" />
			<cfexecute name="/bin/hostname" variable="machineName" timeout="5" />
		<cfreturn LCase(trim(machineName))>

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


CF Quick Reference

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

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