The new application event mechanism (planned for Blackstone) provides powerful new functionality to ColdFusion developers. Lots of you have asked about this feature since it was first mentioned in the MAX ColdFusion BOF (and then blogged in my BOF report), and so a basic overview is in order. Here goes.
ColdFusion currently supports two events, request start and request end. In other words, ColdFusion allows you to write code that is automatically executed before and after every request. Request start code is put into a file named Application.cfm, and request end code is put into a file named OnRequestEnd.cfm (and yes, Application.cfm is a bad name, OnRequestStart.cfm would have been more accurate, but oh well).
In Blackstone we plan to support these same two events, and some additional ones. Here is the list, and a brief example of how you could use them:
OnRequestStart: This is functionally the same as Application.cfm now, this is where you would place any code that needs to be executed before each request. An example of this might be checking to see if a user is logged in or not.
OnRequestEnd: This is functionally the same as OnRequestEnd.cfm now, and this is used for code to be executed after each request. This is sometimes used for debugging, to grab entire scopes, perhaps logging them for subsequent analysis.
OnApplicationStart: This is a new event, it is executed when an application first starts, and will not execute again unless the application times out, the server is restarted, or an explicit OnApplicationStart call is made. This is where you will typically initialize data in the APPLICATION scope. Look at your current Application.cfm, any code wrapped in a
OnApplicationEnd: Another new event, this one is executed when an application times out, or upon graceful server shutdown. This will likely be an infrequently used event, and it too can be used for debugging, logging, saving accumulated totals and stats to disk, and more.
OnSessionStart: This one is new too, and is the most requested new event. This one executes whenever a new SESSION is created, and is therefore the ideal place to store user preferences, initialize shopping carts, and more.
OnSessionEnd: Also new, and the counterpart to OnSessionStart, this one executes when a session times out. This can be used to save shopping cart contents to disk, for example, so that when users return later they can pick up where they left off. It can also be used to keep track of incomplete transactions, perhaps users who put items into a shopping cart and left without ever checking out, code in this event can track and log that.
OnRequest: The last new event, this one gets executed for every request in lieu of the actual request. If present, requests are processed by OnRequest code which can, if wanted, then execute the original request. For example, if you wanted to trap all requests from a known IP address, sending them to another URL, this would be a good place for it. If you wanted to display a “we are down between 4 and 5 am” notice, this is where you could do it. And if you wanted to process request generated output (perhaps to use compression or to strip whitespace) you could do that here too.
Of course, the old style Application.cfm files will still be supported, but with all of this new power, why would you even want to use them? 🙂
Leave a Reply