首页 > 解决方案 > 限制被调用 CFC 的执行时间

问题描述

我们有一个 ColdFusion 编辑,它检查 MX 记录以软验证电子邮件地址。我们偶尔会收到来自 DNS 服务器的长响应,并且需要限制在放弃之前等待响应的时间。

调用的组件进行实际查找。下面复制的代码是 larver 验证脚本的一部分。

我想要做的是限制我将等待查找组件运行多长时间,然后在超过该时间时处理错误。这段代码对我来说似乎是正确的,但它只是超过了超时时间(我在组件中有一个“睡眠”来模拟缓慢的响应。)

try {
    requesttimeout="10";
    MyNewArray[1] = right(MyEmail, RightChars);
    mycnt = new common.functions.mxLookup(MyNewArray);
    Caller.EmailReturnCode = iif(mycnt gt 0,'"0"','"2"');
}
catch ("coldfusion.runtime.RequestTimedOutException" a) {
    Caller.EmailReturnCode = "2";
}
catch (any e) {
    Caller.EmailReturnCode = "2";
}

标签: coldfusiontimeout

解决方案


        thread action="run" name="doLookup" {
            variables.mycnt = new common.functions.mxLookup(MyNewArray);
        }
        thread action="join" name="doLookup" timeout="20000";
        if(doLookup.Status is "RUNNING"){
            thread action="terminate" name="doLookup";
            throw("MX Lookup of #MyEmail# timed out", "toException", "MX Lookup exceeded allowed time of 20 seconds at #timeFormat(now(),'HH:mm:sss')#");
        }
        Caller.EmailReturnCode = iif(mycnt gt 0,'"0"','"2"');

感谢亚历克斯

该解决方案完美运行。


推荐阅读