基于脚本的组件中的线程不起作用

时间:2016-01-08 03:09:48

标签: coldfusion cfml railo cfc cfthread

当我注释掉下面的线程相关代码时,对slack API的提交按预期工作。但是,当我尝试在线程命令中处理提交时,提交永远不会通过。

我做错了吗?在Railo 4中运行

// push to slack!
public function push(options = {}) {

    var http = new http();
    var data = {
        "text": "Default message",
        "channel": "activity"
    };

    structAppend(data, options);

    // thread
    //  action="run" {

        sleep(5000);

        http.setMethod("post");
        http.setUrl(variables.slackWebhookUrl);
        http.addParam(
            type = "formField",
            name = "payload",
            value = serializeJson(data)
        );

        http.send();

    // }

}

1 个答案:

答案 0 :(得分:2)

感谢Adam Cameron指出我正确的方向。事实证明,scoping is quite tricky with cfthreads

为了简洁起见,我会说在将来使用线程时我将遵循这些规则:

  1. 不引用“thread block”
  2. 之外的范围内的任何变量
  3. 明确地将您需要引用的任何“父范围”数据传递给线程
  4. 基本上将cfthread视为cfmodule(通过属性传递数据)
  5. 这是有效的代码:

        // push to slack!   
        public function push(options = {}) {
    
            var data = {
                "text": "Default message",
                "channel": "activity"
            };
    
            structAppend(data, options);
    
            thread
                action="run"
                data="#data#"
                slackUrl="#variables.slackWebhookUrl#" {
    
                var http = new http();
    
                http.setMethod("post");
                http.setUrl(attributes.slackUrl);
                http.addParam(
                    type = "formField",
                    name = "payload",
                    value = serializeJson(attributes.data)
                );
    
                http.send();
    
            }
    
        }