Coldfusion: cfinvoke timeout

Written on 19 March 2012, 03:57pm

Tagged with: , ,

Consider the following scenario: you make a cfinvoke to a remote web service, that takes a long time to execute. In the same, you want to be in control of your script, and present a timeout error message. The natural thing to do in this case is to use cfsetting requestTimeout.
Or, you can use the cfinvoke’s timeout attribute, as in the example below.

I created a ColdFusion component, to be used a web service: sleeping.cfc. This component has only one method – sleepit – that simulates a long process. In our case, it sleeps for 5 seconds.
The client – call_sleeping.cfm – is using the cfinvoke tag to call the sleepit() method, but it will only wait for 2 seconds (timeout=2). The error received after 2 seconds will be:

Could not perform web service invocation “sleepit”.

The code below.

sleeping.cfc

<cfcomponent displayname="Sleeping beauty"  
	output="false"
	hint="Just sleeps for some seconds">
    
	<cffunction access="remote" 
    	name="sleepit" 
	    output="false" 
        returntype="string">
        
    	<!---<cfset Sleep(5000)>--->
		<cfset thread = CreateObject("java", 
						"java.lang.Thread")>
        <cfset thread.sleep(5000)>

        <cfreturn 1 />
	</cffunction>

</cfcomponent>

call_sleeping.cfm

<cftry>
	<cfinvoke webservice="url://sleeping.cfc?wsdl" method="sleepit" 
		returnvariable="result" refreshWSDL="yes" timeout="2" />
	<cfoutput>Success invoking method <strong>sleepit</strong></cfoutput>

    <cfdump var="#result#" label="Output" />

	<cfcatch>
    	<cfoutput>Error</cfoutput>
        <cfdump var="#cfcatch#" label="Error" />
    </cfcatch>
</cftry>

Leave a response