Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onApplicationEnd - Is CF actually shutting down?

I need to use onApplicationEnd() as part of Application.cfc to execute a call on a 3rd party Java object to close a connection to another device on the network.

The code I have worked perfectly if I call it as a normal request, but when I place it in the onApplicationEnd() method I'm running into some errors. These errors suggest that CF might in fact be shutting down already to the point where I cannot access these 3rd party Java classes.

Code:

    <cffunction name="onApplicationEnd" returnType="void">
    <cfargument name="appScope" required="true" />

    <cfset var logLocation = "test" />

    <cflog file="#logLocation#" text="*** [Application.cfc]  - **** START RUN ****" />
    <cflog file="#logLocation#" text="*** [Application.cfc]  - #timeformat(now(),'HH:mm:ss' )# - onApplicationEnd() called " />


    <cftry>

        <cfif structKeyExists(ARGUMENTS, "appScope")>
            <cflog file="#logLocation#" text="*** [Application.cfc]  - #timeformat(now(),'HH:mm:ss' )# - ARGUMENTS.appScope is defined" />
        <cfelse>
            <cflog file="#logLocation#" text="*** [Application.cfc]  - #timeformat(now(),'HH:mm:ss' )# - ARGUMENTS.appScope is undefined! " />
        </cfif>

        <!--- Check if we have a test crypto object in scope, and if so close it's connection --->
        <cfif structKeyExists(ARGUMENTS.appScope, "testCrypto")>

            <cflog file="#logLocation#" text="*** [Application.cfc]  - #timeformat(now(),'HH:mm:ss' )# - onApplicationEnd() - crypto object exists in app scope" />

            <cfset ARGUMENTS.appScope.testCrypto.closeConnection() />
            <<cflog file="#logLocation#" text="*** [Application.cfc]  - #timeformat(now(),'HH:mm:ss' )# - onApplicationEnd() - closed crypto server connection" />

        <cfelse>
            <cflog file="#logLocation#" text="*** [Application.cfc]  - #timeformat(now(),'HH:mm:ss' )# - onApplicationEnd() - NO crypto server connection present to close" />
        </cfif>

            <cfcatch type="any">

                <cflog file="#logLocation#" text="*** [Application.cfc]  - #timeformat(now(),'HH:mm:ss' )# - onApplicationEnd() - Error - #cfcatch.message#" />

            </cfcatch>

        </cftry>
    <cflog file="#logLocation#" text="*** [Application.cfc]  - #timeformat(now(),'HH:mm:ss' )# - onApplicationEnd() ended " />

</cffunction>

The line to close the connection on my object is failing with the message: 'java.lang.IllegalStateException: Shutdown in progress'.

Here are the full logs for one run:

"Information","Thread-8","10/23/09","09:05:54",,"*** [Application.cfc]  - **** START RUN 

"Information","Thread-8","10/23/09","09:05:54",,"*** [Application.cfc]  - 09:05:54 - onApplicationEnd() called "

"Information","Thread-8","10/23/09","09:05:54",,"*** [Application.cfc]  - 09:05:54 - ARGUMENTS.appScope is defined"

"Information","Thread-8","10/23/09","09:05:54",,"*** [Application.cfc]  - 09:05:54 - onApplicationEnd() - crypto object exists in app scope"

"Information","Thread-8","10/23/09","09:05:54",,"*** [Application.cfc]  - 09:05:54 - onApplicationEnd() - Error - Shutdown in progress"

"Information","Thread-8","10/23/09","09:05:55",,"*** [Application.cfc]  - 09:05:55 - onApplicationEnd() ended "

Is there restrictions to what I can do in onApplicationEnd() and if so is there any work around?

I am using CF 8 (8,0,1,195765) Developer Edition on a Windows XP machine.

Also, if I run CF in a console window and press CTRL-C I see this, but I also see this behaviour if I run cfstop.

Many thanks in advance!

EDIT: Some others had this issues here, but no solutions.

EDIT: Removed thread example as it might be fogging the issue. Posted code and logs.

like image 788
Ciaran Archer Avatar asked Oct 22 '09 13:10

Ciaran Archer


2 Answers

It sounds like this may be caused by the fact that the server is shutting down, rather than just the CF application. I am guessing that if the JVM is already in the process of shutting down, the resources your java class uses might not be available at that point. So onApplicationEnd might not be the correct place for that code.

You might want to look into adding a ShutdownHook. I am not 100% positive, but I think placing the cleanup code there, instead of in onApplicationEnd might allow the java object to do its cleanup before the JVM enters its death throes stage.

But having said all that, would not the "connection to another device on the network" be closed automatically once the server shuts down?

like image 191
Leigh Avatar answered Oct 01 '22 02:10

Leigh


according to the docs "You cannot use this method to display data on a user page, because it is not associated with a request."

I think the "is not associated with a request." is the key. It could be that your java objects only exist within the request thread and not in the app instance.

Perhaps instead you shound try using onRequestEnd

http://livedocs.adobe.com/coldfusion/8/AppEvents_09.html

like image 37
ryber Avatar answered Oct 01 '22 03:10

ryber