coldfusioncoldfusion-9cfthread

How to run multiple threads in ColdFusion


I am trying to run 3 threads of an http request in ColdFusion. This is an email system which will select 3 different campaigns and send there respective recipients at the same time.

But my following code only runs one thread and then drops off.

   <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>

Any idea guys?


Solution

  • Well, I decided to not use the cfhttp and used the threading like this and it works.

    <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>