Tuesday, July 02, 2013

Restart your Application without restarting your ColdFusion Server

I was working in a old application where the object instantiate was done in onApplicationStart method of Application.cfc. To solve few hot fixes I modified the cfc and pushed into live but the code which I had modified was not working. As I have to reset that application variables which holds the Old component object.

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.

Here is index.cfm

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...

4 comments:

  1. Nice Info, Upendra.
    I was only knowing about onApplicationStart(); till now, good to know how ApplicationStop(); works.

    ReplyDelete
  2. 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.

    Few 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.

    ReplyDelete
    Replies
    1. 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.

      In both the cases we don't have to restart our application server.

      Delete
  3. Thanks for your suggestion Upendra. In both the cases we do not required to restart the Application Server.

    ReplyDelete

Followers