背景
这是从外部站点检索数据的预定作业的一部分(外部站点提供用于通过Web服务检索数据的API)并使用新信息更新数据库。它正在检索大约3,500个数据项。我当前的预定作业创建了CFThread
个任务块,每次运行10个线程并在开始下一个10块之前加入它们。
代码:
<cfset local.nMaxThreadCount = 10>
<!---retrieve a query that contains the items that need to be updated, approximately 3,500 items--->
<cfset local.qryItemsNeedingUpdate = getItemsNeedingUpdate(dtMostRecentItemPriceDate = local.qryMostRecentItemPriceDate.dtMostRecentItemPrice[1])>
<cfset local.nThreadBlocks = Ceiling(local.qryItemsNeedingUpdate.RecordCount / local.nMaxThreadCount)>
<cftry>
<cfloop index="local.nThreadBlock" from="1" to="#local.nThreadBlocks#">
<cfif local.nThreadBlock EQ local.nThreadBlocks>
<cfset local.nThreadCount = local.qryItemsNeedingUpdate.RecordCount MOD local.nMaxThreadCount>
<cfelse>
<cfset local.nThreadCount = local.nMaxThreadCount>
</cfif>
<cfset local.lstThreads = "">
<cfloop index="local.nThread" from="1" to="#local.nThreadCount#">
<cfset local.nQryIdx = ((local.nThreadBlock - 1) * local.nMaxThreadCount) + local.nThread>
<cfset local.vcThreadName = "updateThread#local.qryItemsNeedingUpdate.nItemID[local.nQryIdx]#">
<cfset local.lstThreads = ListAppend(local.lstThreads, local.vcThreadName)>
<!---create the attributes struct to pass to a thread--->
<cfset local.stThread = StructNew()>
<cfset local.stThread.action = "run">
<cfset local.stThread.name = local.vcThreadName>
<cfset local.stThread.nItemID = local.qryItemsNeedingUpdate.nItemID[local.nQryIdx]>
<!---spawn thread--->
<cfthread attributecollection="#local.stThread#">
<cfset updateItemPrices(nItemID = attributes.nItemID)>
</cfthread>
</cfloop>
<!---join threads--->
<cfthread action="join" name="#local.lstThreads#" />
</cfloop>
<cfcatch type="any">
<cflog text="detailed error message logged here..." type="Error" file="myDailyJob" application="yes">
</cfcatch>
</cftry>
问题:
后台进程需要这种逻辑吗?也就是说,需要CFThread action="join"
吗?线程中没有显示任何内容,并且线程是独立的(不依赖于其他线程或生成它们的进程)。线程更新数据库中的价格并死亡。是否有必要限制线程,即一次运行10并加入它们?流程是否可以循环并一次创建所有3,500个线程? ColdFusion是否会对额外的线程进行排队并按时运行它们?
答案 0 :(得分:3)
线程会排队;这取决于您正在运行的ColdFusion的版本。
然而,对于你正在做的事情,线程不是你想要的。您想使用消息队列,如ActiveMQ或Amazon SQS。您可以使用Adobe CF附带的ActiveMQ网关之类的事件网关,如果您正在使用其他消息队列或CF引擎,则可以编写自己的事件网关。 (例如,我编写了一个使用Amazon SQS和Railo事件网关的消息传递系统,用CFML编写)