So, I thought use ApplicationStop function which was introduced in ColdFusion-9. Before that I have never used ApplicationStop in any of my previous applications. I had read the documentation that this function basically used in ColdFusion ORM to reload the ORM objects. So, I just did some testing how exactly this function behaves in normal condition.
So, here is my Application.cfc for testing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component displayname="Application" hint="Manages Application Flow" output="false" | |
{ | |
this.name = "testApplicationStop"; | |
this.applicationTimeout = createTimeSpan(1,0,0,0); | |
this.loginStorage = "cookie"; | |
this.sessionManagement = true; | |
this.sessionTimeout = createTimeSpan(0,1,0,0); | |
this.setClientCookies = true; | |
this.ClientStorage = true; | |
this.setDomainCookies = false; | |
public boolean function onApplicationStart(){ | |
//File write during onApplicationStart | |
var logWriter = FileOpen("D:\testApplicationStop.txt", "append" ); | |
FileWriteLine(logWriter,"Application started on :- #now()#"); | |
FileClose(logWriter); | |
return true; | |
} | |
public void function onSessionStart(){ | |
session.myvar = 10; //Setting Session scope variable | |
application.myVar = 20; //Setting Application scope variable | |
//File write during onSessionStart | |
var logWriter = FileOpen("D:\testApplicationStop.txt", "append"); | |
FileWriteLine(logWriter,"Session started on :- #now()#"); | |
FileClose(logWriter); | |
return; | |
} | |
public boolean function onRequestStart(string targetPage){ | |
return true; | |
} | |
public void function onRequest(string targetPage){ | |
//Include the Target Page | |
include arguments.targetPage; | |
return; | |
} | |
public void function onRequestEnd(){ | |
//If appreset is defined in URL scope then reset the application by calling ApplicationStop | |
if( structKeyExists(url, "appReset") ) { | |
var logWriter = FileOpen("D:\testApplicationStop.txt", "append"); | |
FileWriteLine(logWriter,"Application stop is going to execute on :- #now()#"); | |
FileClose(logWriter); | |
ApplicationStop(); | |
/* | |
We can also call onApplicationStart() function here | |
*/ | |
} | |
return; | |
} | |
public void function onSessionEnd(struct SessionScope, struct ApplicationScope){ | |
//File write during onSessionEnd | |
var logWriter = FileOpen("D:\testApplicationStop.txt", "append"); | |
FileWriteLine(logWriter,"Session ended on :- #now()#"); | |
FileClose(logWriter); | |
return; | |
} | |
public void function onApplicationEnd(struct ApplicationScope){ | |
//File write during onApplicationEnd | |
var logWriter = FileOpen("D:\testApplicationStop.txt", "append"); | |
FileWriteLine(logWriter,"Application ended on :- #now()#"); | |
FileClose(logWriter); | |
return; | |
} | |
} |
Here is index.cfm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Testing Application Stop <br/> | |
<cfdump var="#session.myvar#"> <br/> <!--- Display session scope variable ---> | |
<cfdump var="#application.myVar#"> <!--- Display application scope variable ---> |
If you will notice here in this Application.cfc I just doing file append operation in onApplicationStart(), onSessionStart(), onSessionEnd(), onApplicationEnd() and also inside onRequestEnd() when we want to refresh our application.
When the very first time I run the application I got the following output in file write:
Application started on :- {ts '2013-07-02 18:28:04'}
Session started on :- {ts '2013-07-02 18:28:04'}
And for index.cfm I got the following output:
Testing Application Stop
10
20
Next time I just passed a URL parameter "appReset" so that I can reset the application. Then I got the following output in file write:
Application stop is going to execute on :- {ts '2013-07-02 18:28:49'}
Application ended on :- {ts '2013-07-02 18:28:49'}
And in index.cfm I got the following output:
Testing Application Stop
10
20
So, here we observed that ApplicationStop() executed and then onApplicationStop() executed but onSessionEnd() was not executed.
On third time I just removed the URL parameter for Application reset. Then I got the following output:
In log file:
Application started on :- {ts '2013-07-02 18:29:04'}
And in index.cfm:
Testing Application Stop
10
Error Message:
Element MYVAR is undefined in APPLICATION.
So, when we are calling ApplicationStop it doesn't affect any session scope variables. Session scope variables remain unchanged and also it doesn't creates a new session. It simply uses the old session scope but reset all application scope variables.
So, if we want to put any object instantiation code in onApplicationStart() then we can reinstantiate that using ApplicationStop().
If someone will ask that why can't we follow simply by calling onApplicationStart() in onRequest() as per our requirements.
Yes, we can do that but it depends on your logic you have implemented in your Application.
If someone doesn't wants to reset all application scope variable then directly call onApplicationStart(). But, if someone wants to reset all application scope variables or using ORM in the Application then ApplicationStop() will work.
Still exploring more on this...
Nice Info, Upendra.
ReplyDeleteI was only knowing about onApplicationStart(); till now, good to know how ApplicationStop(); works.
Its really a good article. I really let to know many good things after reading this. Thanks Upendra for such a nice and organized article.
ReplyDeleteFew days back, in one of my project i need to make changes in cfcs and then upload the updated cfcs through FTP client. After uploading the updated cfcs, it was still not reflecting my application. Then i reached at the conclusion that i have to restart the coldfusion server. But client's codes are in one of the shared server so we do not have access to the CF Server. So, i was supossed to restart the CF server from codes and then i used "ApplicationStop()" function to restart the application server and it really worked for me. You can also create a simple cfm page let's say restartApp.cfm and put inside that and you can run this cfm page whenever you want to restart your coldfusion server.
We can also call onApplicationStart() to reinitialize in your case. You have used ApplicationStop() that is also good as it again initializes all your application scope variables.
DeleteIn both the cases we don't have to restart our application server.
Thanks for your suggestion Upendra. In both the cases we do not required to restart the Application Server.
ReplyDelete