背景:
这是计划作业的一部分,它从外部站点检索数据(外部站点提供了通过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个线程并加入它们?进程可以一次循环并创建所有3500个线程吗?ColdFusion是否会将多余的线程排队,并按时间运行它们?
发布于 2013-01-14 22:02:40
除非您需要在线程完成后将信息输出到页面,否则没有必要使用“联接”。
线程将排队;这取决于您正在运行的ColdFusion的版本。
然而,对于您正在做的事情,线程并不是您想要的。您希望使用消息队列,比如ActiveMQ或Amazon。您可以使用Adobe附带的事件网关(如ActiveMQ网关),或者在使用不同的消息队列或CF引擎时编写自己的网关。(例如,我编写了一个使用Amazon和Railo事件网关的消息传递系统,用CFML编写)
https://stackoverflow.com/questions/14326161
复制相似问题