开发者

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.


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?


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


Well my first thought is that the server doesn't actually stop when the application stops... The application will stop, but the server is liable to keep chugging along. If you have a directory with an older Application.cfm in it and just some flat .cfm files in that directory, people can request those cf pages and they can be not associated with any kind of application context. So you can actually have no applications running and still be serving lots of CF pages. Granted, that's not how people generally set up their CF servers, but they do work that way. There's been some talk of creating a Server.cfc like Application.cfc in a future version, although I don't know if they've implemented it in CF9.

My second thought is that the only way for the onApplicatinEnd method to cause the specific error "java.lang.IllegalStateException: Shutdown in progress" would be if the java objects you're referencing are java objects that are somehow intrinsically linked to the application context, such as objects that you might access through the undocumented ColdFusion.server.ServiceFactory. But the code you've posted doesn't look like that's happening.

It's a long shot, but you could try placing the testCrypto object in a local / temp variable within the onApplicationEnd method and executing it from there.

Sorry I couldn't be more helfpul.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜