CF: Flag application to run onApplicationStart Part 2

After Mark Mandel kindly pointed out the ApplicationStop() function that’s available in CF9, I thought my previous post, on flagging an application to fire onApplicationStart, needed a follow up.

CF9’s ApplicationStop()

This is a new addition to CF9. The documentation for it isn’t very clear as it states it will:

Stops or resets the current application. The application is restarted on the next request to the application.

Is that supposed to be “Stops and resets”? I’m guessing so as it’s a little confusing otherwise.

The function appears to destroy the application scope completely, as soon as the request has finished being processed.  The next request that hits that application will start a completely new application scope, firing the “onApplicationStart()” event in the process.

If you continue reading, you’ll see it in action below.

What about CF7 and CF8?

Well they don’t have that function, it was new to CF9.  But, why don’t we make our own?  Doing my usual dig around I uncovered a function called “cleanUp” that belongs to the “ApplicationScopeTracker” which appears to manage all the application scopes that are active.

Here’s a dump of the “ApplicationScopeTracker”:

<cfdump var="#CreateObject('java', 'coldfusion.runtime.ApplicationScopeTracker')#" />

Application Scope Tracker

As you can see, we need to pass it the current application scope so it can work it’s magic.

<cfset CreateObject('java', 'coldfusion.runtime.ApplicationScopeTracker').cleanUp(application) />

It’s as simple as that.  All you need to do is wrap it up in a function and you’ve backported “applicationStop()” to CF7 and CF8.

<cffunction name="ApplicationStop" returntype="boolean" output="false">
 <cfif IsDefined('application')>
     <!--- This is just in case there's no app scope but variables.application --->
     <cfset CreateObject('java', 'coldfusion.runtime.ApplicationScopeTracker').cleanUp(application) />
     <cfreturn true />
     <cfcatch type="any"></cfcatch>
 <cfreturn false />

ApplicationRestart() for CF7 – CF9

With stopping an application taken care of, what about restarting one.  Well we covered that in part 1 of this post, we have a way of not destroying the application scope and firing onApplicationStart again.  This is very useful if you have a large set of application variables that you don’t really want to repopulate, but still have some tasks in the “onApplicationStart” that you want executed.  A good idea for this approach is, within the onApplicationStart, add an if that checks for an application variable.  If it’s present, you know the application has been restarted and all the data is currently there, if not then it’s a fresh start.

Let’s wrap up the code from part 1 of this post into a function called ApplicationRestart().

<cffunction name="ApplicationRestart" returntype="boolean" output="false">
 <cfif IsDefined('application')>
     <!--- This is just in case there's no app scope but variables.application --->
     <cfset application.setIsInited(false) />
     <cfreturn true />
     <cfcatch type="any"></cfcatch>
 <cfthrow message="No application scope found." />

Putting them together

Now we can construct a little application that has a simple onApplicationStart which counts how many times the application has been restarted (not stopped).  Then add a template with three links: Restart Application, Stop Application and a normal request.


<cfcomponent output="false">
  <cfscript> = 'TestApp';
    this.applicationTimeout = createTimeSpan(0, 1, 0, 0);

  <!--- Run when application starts up --->
  <cffunction name="onApplicationStart" returnType="boolean" output="false">
    <cfset application.stamp = Now() />
    <cfif Not StructKeyExists(application, 'inits')>
      <cfset application.inits = 0 />
    <cfset application.inits = application.inits + 1 />
    <cfreturn true>


<cfif ListFind('7,8', ListFirst(server.coldfusion.productVersion))>
 <cfinclude template="applicationStop.cfm" />
<cfinclude template="applicationRestart.cfm" />

<cfif StructKeyExists(url, 'restart')>
 <cfset applicationRestart() />
<cfelseif StructKeyExists(url, 'stop')>
 <cfset applicationStop() />

<cfoutput>Application IsInited? #application.isInited()#</cfoutput>
<cfdump var="#application#" />
 <li><a href="index.cfm?restart=true">ApplicationRestart()</a></li>
 <li><a href="index.cfm?stop=true">ApplicationStop()</a></li>
 <li><a href="index.cfm">Normal refresh.</a></li>

One quick note.  CF doesn’t like you overwriting existing functions, even if you wrap an if statement around it’s creation.  Hence the reason for the CFIncludes, otherwise if you’re on CF7/8 you can bring those functions in or store them in the same template.

Now, when you click the restart link followed by a normal request, you’ll see the application.inits value increase and the application.stamp value refresh.  Clicking the stop link, followed by the normal request link, will clear the application completely.  This will reset the init count and the date stamp.

Video example

Vodpod videos no longer available.

Railo investigation

Currently I haven’t found a way to replicate this in Railo and the ApplicationStop function has been requested already.  I can’t find any obvious way of replicating ApplicationRestart either but you could easily call onApplicationStart manually if needed.

6 thoughts on “CF: Flag application to run onApplicationStart Part 2

  1. Ben Nadel

    Very cool. I just read Part 1 and was about to ask about the cleanup() method (as you had in the session tracker post). Then I was like – Hmm, better read Part 2 first🙂 This is some cool stuff.

    1. misterdai Post author

      Cheers Ben. It’s nice to think that Adobe might be making some of these little secrets official like they have with ApplicationStop(), which must use the same underlying methods from what I can tell. Makes you wonder what else is hidden away in there😉

  2. Ben Nadel

    Yeah, it would be nice to get some of this stuff brought into the language “officially”. I am always unsure how much these underlying Java objects are acknowledged by Adobe. I know they admin API things seem to be fairly agreed upon.

  3. Pingback: onCfcRequest for CF7+ « Mister Dai

  4. Dan G. Switzer, II

    I just wanted to chime in and say this method does indeed seem to be a thread-safe way to restart the application scope. I did a ton of load testing of this method over the past 2 days just to see if I could cause any instabilities or get it to throw errors with variables that did not have read locks.

    In all my testing (which involved 100k+ page requests) I never saw any issues–even when when multiple calls to resetting the application scope would occur.

    More information on my blog:

    In my code, I actually just used the cleanUp() method as it seemed the safest–I didn’t actually test the setIsInited(false) method under load. In my case, the resets are only needed after updating source code that changes CFCs in the App scope, so I preferred as full of a reset as possible.

    1. misterdai Post author


      Thanks for taking a further look at this. Great to see it getting some proper load testing as that’s something I didn’t have time for.🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s