Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 25, 2014 10:47
Learning ColdFusion 9: Resetting Applications With ApplicationStop()
<cfcomponent
output="false"
hint="I define the application settings and event handlers.">
<!--- Define the application settings. --->
<cfset this.name = hash( getCurrentTemplatePath() ) />
<cfset this.applicationTimeout = createTimeSpan( 0, 0, 5, 0 ) />
<cfset this.sessionManagement = true />
<cfset this.sessionTimeout = createTimeSpan( 0, 0, 3, 0 ) />
<!--- Define the request settings. --->
<cfsetting showdebugoutput="false" />
<cffunction
name="onApplicationStart"
access="public"
returntype="boolean"
output="false"
hint="I initialize the application.">
<!--- Initialize the application settings. --->
<cfset application.dateInitialized = now() />
<!--- Return true so that the page can load. --->
<cfreturn true />
</cffunction>
<cffunction
name="onSessionStart"
access="public"
returntype="void"
output="false"
hint="I initialize the session.">
<!--- Initialize the session settings. --->
<cfset session.dateInitialized = now() />
<!--- Return out. --->
<cfreturn />
</cffunction>
</cfcomponent>
<!---
Param a variable in the application. We are doing this
outside of the OnApplicationStart() method to test whether
or not the application was truly killed of if the above
method was simply called.
--->
<cfparam name="application.hitCount" type="numeric" default="0" />
<!--- Increment the hit count. --->
<cfset application.hitCount++ />
<cfoutput>
<h1>
Application And Session Overview
</h1>
<p>
Application hit count: #application.hitCount#
</p>
<p>
Application initialized:
#dateDiff(
"s",
application.dateInitialized,
now()
)#
seconds ago.
</p>
<p>
Session initialized:
#dateDiff(
"s",
session.dateInitialized,
now()
)#
seconds ago.
</p>
<p>
<a href="reset.cfm">Reset application</a> &raquo;
</p>
</cfoutput>
<!--- Check to see if reset flag exists in URL. --->
<cfif structKeyExists( url, "reset" )>
<cfset this.onApplicationStart() />
</cfif>
<!---
Stop the application. After calling this method, the next
page request to the application should start it up again
(resetting it).
--->
<cfset applicationStop() />
<!--- Redirect back to overview. --->
<cflocation
url="index.cfm"
addtoken="false"
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment