【问题标题】:How to run multiple threads in ColdFusion如何在 ColdFusion 中运行多个线程
【发布时间】:2013-07-09 22:17:41
【问题描述】:

我正在尝试在 ColdFusion 中运行一个 http 请求的 3 个线程。这是一个电子邮件系统,它将选择 3 个不同的活动并同时发送给相应的收件人。

但是我下面的代码只运行一个线程然后就掉线了。

   <cfscript>
        newsLetterCampaignGateway = createObject("component", "legacy.ssl.admin.news.model.newsLetterCampaignGateway");
        newsLetterList = newsLetterCampaignGateway.getNewsLettersDueForSend();
        //writedump(newsLetterList);abort;

    </cfscript> 

  <cfloop query="newsLetterList" >
    <cfset newsLetterId =  newsLetterList.newsletterid>
    <cfset campId =  newsLetterList.id>
    <cfset fromEmail =  newsLetterList.fromEmail>

    <!--- <cfdump var="#campId#"> --->
    <cfthread action="run" name="runCampaign#campId#" >
        <cflock
            name="runCampaign_#campId#_Lock"
            type="exclusive"
            timeout="60">
            <!--- <cfdump var="#campId#"> --->
            <cfscript>      
                httpService = new http(); 
                httpService.setMethod("get"); 
                httpService.setCharset("utf-8"); 
                httpService.setUrl("http://mysamplesite.com/legacy/ssl/admin/news/model/newsLettercampaign.cfc"); 
                httpService.addParam(type="url",name="method",value="sendCampaignNewsLetters"); 
                httpService.addParam(type="url",name="live",value="true"); 
                httpService.addParam(type="url",name="campaignId",value="#campId#"); 
                httpService.addParam(type="url",name="newsLetterId",value="#newsLetterId#"); 
                httpService.addParam(type="url",name="fromEmail",value="#fromemail#"); 
                httpService.send();
            </cfscript> 
        </cflock>
    </cfthread>

  </cfloop>


    <cfloop query="newsLetterList" >

            <cfthread
            action="join"
            name="runCampaign#campId#"
            />
    </cfloop>

有什么想法吗?

【问题讨论】:

  • 你如何确定它只运行一个线程?
  • 可能是错误的,但是您的join 代码看起来不对。要等待所有线程完成,我相信您需要传入一个线程列表,而不是在每个线程上调用join
  • @Leigh,你没有错。另外,我只看到一个 cfthread action=run 标签,而且它不在循环内。
  • @DanBracuk,线程正在这个循环中运行:&lt;cfloop query="newsLetterList" &gt;

标签: coldfusion coldfusion-9 cfthread


【解决方案1】:

好吧,我决定不使用 cfhttp 并使用这样的线程并且它可以工作。

<cfsetting requesttimeout="300000">
<cfscript>
    newsLetterCampaignGateway = createObject("component", "path.to.cfc");
    newsLetterList = newsLetterCampaignGateway.getNewsLettersDueForSend();
</cfscript>


  <cfloop query="newsLetterList" >

    <cftry>
        <cfthread action="run" name="runCampaign#url.campaignId#" >
            <cfset sendRequest(url)>
        </cfthread>

    <cfcatch>
        <cfdump var="#cfcatch#"><cfabort>
    </cfcatch>
    </cftry>

  </cfloop>

<cffunction name="sendRequest">
    <cfargument name="urlStu" required="true">

    <cfset newsLettercampaign = createObject("component", "path.to.cfc")>
    <cfset newsLettercampaign.sendCampaignNewsLetters(arguments.urlStu)>

</cffunction>

【讨论】:

    猜你喜欢
    • 2021-10-24
    • 2021-01-02
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 2022-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多