我正在开发一个调用外部 Web 服务来获取数据的系统。看起来,当 Web 服务中出现故障时,调用者将被挂起,并且模板永远不会结束。
调用模板中有一个CFSETTING REQUESTTIMEOUT,但似乎没有应用于Web服务调用。调用代码使用 CFSCRIPT,我对这种语法感到不舒服。有人可以告诉我如何对以下代码应用超时吗?
ws = CreateObject("webservice", "http://#trim(arguments.RemoteSite)#",{wsversion="2"});
ret = ws.GetJData("#arguments.clubid#", #datepart('yyyy',arguments.StartDate)#, #datepart('m',arguments.StartDate)#);
文档指出,您可以在创建 WSDL 对象时在 argStruct 中设置超时。默认为 0(无超时);如果您设置了超时,您还需要异常处理,因为 Web 服务调用将抛出超时异常。
// Define webservice config
webServiceConfig = {
wsversion="2",
timeout=10
};
// Initialise wsdl webservice
ws = CreateObject(
"webservice",
"http://#trim(arguments.RemoteSite)#",
webServiceConfig
);
try
{
// Execute method
ret = ws.GetJData(
"#arguments.clubid#",
#datepart('yyyy',arguments.StartDate)#,
#datepart('m',arguments.StartDate)#
);
} catch (any e)
{
// An exception is thrown on timeout - handle it here:
ret = "";
}
如果你想自己尝试的话,我在 CF Fiddle 上测试了这个模式。