Tech Blog

Hacking the application scope in CFMX

Posted At : October 23, 2006 1:26 PM 3 Comments

First - a warning - Be careful! ("Here be dragons..."). Now that I've gotten that out of the way - here are some useful (and dangerous) functions for managing applications in CFMX.

Next - why I developed this. I am working on an application which uses application scope caching very heavily for performance - but this in turn causes the application to be quite slow to start up.

To prevent the application accidentally (or maliciously) being restarted I wanted to remove the application restart code from the main website.

So after a bit of digging in the CFMX classes I found the application handling functions and have put together this cfc which can list all running applications and force application restarts.

Getting all running applications

This will return an array of the names of all applications running on a sever.

<cffunction name="getRunningApplications" returntype="array" output="false">
   <cfscript>
      var oApp = createObject("java","coldfusion.runtime.ApplicationScopeTracker");
      var applications = oApp.getApplicationKeys();   
      var availApps = ArrayNew(1);
   </cfscript>
   <cfloop condition="#applications.hasNext()#">
      <cfset arrayAppend(availApps,applications.next())>
   </cfloop>
   <cfreturn availApps>
</cffunction>

Killing an application

In order force an application to restart on the next request we can kill an application - note this does not handle locking so bad things can happen.
<cffunction name="killApplication" returntype="void" output="false">
   <cfargument name="appName" type="string" required="true">
   <cfscript>
   var oApp = createObject("java","coldfusion.runtime.ApplicationScopeTracker");
   var myApp = oApp.getApplicationScope(arguments.appName);
   oApp.cleanUp(myApp);
   </cfscript>
</cffunction>

Getting an Application

And finally - pass in the name of the application scope you wish to see and it will be yours!
<cffunction name="getApplication" returntype="struct" output="false">
   <cfargument name="appName" type="string" required="true">
   <cfscript>
   var oApp = createObject("java","coldfusion.runtime.ApplicationScopeTracker");
   var myApp = oApp.getApplicationScope(arguments.appName);
   return myApp;
   </cfscript>
</cffunction>

I'll be releasing this as a cfc shortly when I get a few details sorted out - but in the meantime slap a cfcomponent tag around the lot and you are good to go.

Please be aware - these are untested and potentially dangerous (especially in shared hosting environments) - but can be very useful for developers or securely managing your applications on a dedicated box.

3 Comments

MikeR 1/26/07 9:11 AM

Excellent, thanks!

Andrew Grosset 4/18/07 12:40 PM

Very Useful, thanks!

Ciaran Archer 11/26/08 3:24 PM

Just what I needed - cheers!