Expired SESSION Available In ColdFusion CFThread Tag

Posted December 12, 2007 at 9:01 AM by Ben Nadel

Tags: ColdFusion

Not too much to see here; I just took my previous session expiration post and applied it to ColdFusion's asynchronous threading. Basically, I wanted to make sure that a SESSION which has ended mid-page request would still be available in subsequent threads launched by the current page request (after the session has expired). But, before I even show the code, I just want to point out that it is probably not good practice to refer to the SESSION scope within the CFThread body unless explicitly passed via the CFThread tag attributes (think proper encapsulation and low coupling).

First, I had to update my Application.cfc. Since CFThreads cannot output data to the screen, I added a logging function that would write a value and time stamp to a local log file. Then, I also included the OnRequest() event method so that the log function would be available in the THIS scope (the whole page request becomes an Application.cfc mixin).

  • <cfcomponent
  • output="false"
  • hint="Handle the application configuration.">
  •  
  •  
  • <!---
  • Set the application settings. Notice that we
  • are creating sessions that only last for 2 seconds.
  • --->
  • <cfset THIS.Name = "TimeoutTest" />
  • <cfset THIS.ApplicationTimeout = CreateTimeSpan( 0, 0, 1, 0 ) />
  • <cfset THIS.SessionManagement = true />
  • <cfset THIS.SessionTimeout = CreateTimeSpan( 0, 0, 0, 2 ) />
  •  
  •  
  • <!--- Set the page request settings --->
  • <cfsetting
  • showdebugoutput="false"
  • />
  •  
  •  
  • <cffunction
  • name="OnSessionStart"
  • access="public"
  • returntype="boolean"
  • output="false"
  • hint="Fires when the user's session begins.">
  •  
  • <!--- Store the date created. --->
  • <cfset SESSION.DateCreated = Now() />
  •  
  • <!--- Return out. --->
  • <cfreturn true />
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="OnRequest"
  • access="public"
  • returntype="void"
  • output="true"
  • hint="Executes the requested template.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Template"
  • type="string"
  • required="true"
  • hint="The ColdFusion template that has been requested."
  • />
  •  
  • <!--- Include the template. --->
  • <cfinclude template="#ARGUMENTS.Template#" />
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="OnSessionEnd"
  • access="public"
  • returntype="void"
  • output="false"
  • hint="Fires when the session is terminated.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Session"
  • type="struct"
  • required="true"
  • hint="The expired session scope."
  • />
  •  
  • <!--- Store the date destroyed. --->
  • <cfset ARGUMENTS.Session.DateEnded = Now() />
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="LogValue"
  • access="public"
  • returntype="void"
  • output="false"
  • hint="Logs the given value to a local log.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Value"
  • type="string"
  • required="true"
  • hint="The value to be logged."
  • />
  •  
  • <!--- Set up the output. --->
  • <cfset var Output = (
  • ARGUMENTS.Value & " : " &
  • TimeFormat( Now(), "hh:mm:ss" )
  • ) />
  •  
  • <!--- Log value / time stamp to txt file. --->
  • <cffile
  • action="append"
  • file="#ExpandPath( './log.txt' )#"
  • output="#Output#"
  • addnewline="true"
  • />
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>
  •  
  • </cfcomponent>

Then, I created a simple Index.cfm page that sleeps for 10 seconds (so as to ensure session expiration) and then launches an asynchronous thread:

  • <!---
  • Sleep the primary page request thread to make sure
  • that the SESSION has expired by the time we launch our
  • asynchronous thread.
  • --->
  • <cfthread
  • action="sleep"
  • duration="#(10 * 1000)#"
  • />
  •  
  •  
  • <!---
  • Launch asynchronous thread. At this point, the user's
  • SESSION will have already ended.
  • --->
  • <cfthread
  • action="run"
  • name="thread1">
  •  
  • <!---
  • Check to see if the session has timed out (this should
  • always be the case, but just a precaution).
  • --->
  • <cfif StructKeyExists( SESSION, "DateEnded" )>
  •  
  • <!--- Log session timeout time. --->
  • <cfset THIS.LogValue(
  • "Session Ended " &
  • "(#TimeFormat( SESSION.DateEnded, 'hh:mm:ss' )#)"
  • ) />
  •  
  • </cfif>
  •  
  • </cfthread>

There's a couple of things to notice here. For starters, I am sleeping the primary thread (originally requested page) for 10 seconds. This will cause the user's session to expire before our CFThread tag is executed (confirmed via the logging output). Additionally, I am referring to the THIS scope within the CFThread tag body. Because we are using the OnRequest() event method, the entire page essentially becomes part of the Application.cfc and therefore has access to all of its scopes.

Running this page, we get the following log data:

Session Ended (08:48:30) : 08:48:35

As you can see, the SESSION expired at 30 seconds and the thread fired at 35 seconds, 5 seconds after the OnSessionEnd() method was called. Sweet. This just means that as with the primary page request, we never have to worry about session expiration within an asynchronous thread.




Reader Comments

Dec 12, 2007 at 9:13 AM // reply »
319 Comments

"I added a logging function that would write a value and time stamp to a local log file"

Err... why not cflog? The only reason I can see to do this would be for cases when you don't have access to normal CF logs.


Dec 12, 2007 at 9:49 AM // reply »
11,238 Comments

@Ray,

To be honest, I find it a real pain to go to the CFLogs. Our logs are like a million miles long and take forever to load (I don't manage that stuff and I don't know much about them). As such, I find it easier to make something quick and small for a local file.


Dec 12, 2007 at 9:51 AM // reply »
319 Comments

The CF logs can get big - you can use cflog to log to a custom log, like one based on the application name.


Dec 12, 2007 at 9:56 AM // reply »
11,238 Comments

@Ray,

Where is that log file located? If I can give it a custom path, then I am all for it. But, if I still need to actually go to the ColdFusion Admin to view it, still feels a little overkill... no, overkill is not the right word. I guess I mean that it requires to much "system context".


Dec 12, 2007 at 10:01 AM // reply »
319 Comments

Yeah, it is still the main CF log folder. I guess I don't mind the Log Viewer so much. Plus, Scott Stroz's Flex version makes it much sexier.


Dec 12, 2007 at 10:04 AM // reply »
11,238 Comments

@Ray,

Ha ha, I was about to say something like, Wouldn't it be cool if they made a CFLog tag where you could define the output and path the file.... then I was like, oh wait, that's what CFFile does :)

I guess the convenient thing about CFLog is that you don't have to worry about file paths.


Dec 12, 2007 at 11:09 PM // reply »
92 Comments

If you need to see a data dump logged I suggest using the format and output attributes of cfdump! Logs are great but usually whenever I debug something I prefer cfdumps since I can see any sort of complex data. I know this isn't really about debugging but since cflog was mentioned I just thought I'd bring up some of the new features cfdump has gotten in CF8.


Dec 13, 2007 at 7:17 AM // reply »
11,238 Comments

@Javier,

You raise a great point. I am a huge fan of CFDump, but have not really leveraged it yet in ColdFusion 8. Since so much of my CF8 stuff is theoretical (not on production yet), these types of things are not second nature yet. CFDump in CF8 has some great stuff including writing directly to a text file.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools