Created
November 26, 2012 03:58
-
-
Save markmandel/4146542 to your computer and use it in GitHub Desktop.
FutureThreadedWorker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- | |
Copyright 2012 Mark Mandel | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
---> | |
<cfcomponent hint="Pass an array through to this worker, and it will go through it on a loop" output="false" accessors="true"> | |
<cfproperty name="errors" hint="The errors that get generation while processing this data"/> | |
<!------------------------------------------- PUBLIC -------------------------------------------> | |
<cffunction name="init" hint="Constructor" access="public" returntype="FutureThreadedWorker" output="false"> | |
<cfargument name="queue" hint="array queue to process through" type="array" required="true"> | |
<cfargument name="worker" hint="the worker object to call the worker function on" type="any" required="true"> | |
<cfargument name="poolSize" hint="the size of the pool to use" type="numeric" required="false" default="5"> | |
<cfargument name="workerMethodName" hint="the name of the method to call on the worker method" type="string" required="false" default="work"> | |
<cfscript> | |
variables.queue = createObject("java", "java.util.concurrent.ConcurrentLinkedQueue").init(arguments.queue); | |
variables.worker = arguments.worker; | |
variables.poolSize = arguments.poolSize; | |
variables.workerMethodName = arguments.workerMethodName; | |
variables.threadNames = createObject("java", "java.util.ArrayList").init(); | |
variables.results = []; | |
setErrors([]); | |
return this; | |
</cfscript> | |
</cffunction> | |
<cffunction name="run" hint="runs the queue in the thread pool" access="public" returntype="void" output="false"> | |
<cfloop from="1" to="#variables.poolSize#" index="local.index"> | |
<cfscript> | |
var threadName = "worker-thread-#createUUID()#"; | |
arrayAppend(variables.threadNames, threadName); | |
var logKey = getMetadata(variables.worker).name; | |
thread name="#threadName#" logKey="#logKey#" | |
{ | |
while(!variables.queue.isEmpty()) | |
{ | |
writeLog("[#attributes.logKey#] - Queue Size: #variables.queue.size()#"); | |
pollAndRun(); | |
} | |
} | |
</cfscript> | |
</cfloop> | |
</cffunction> | |
<cffunction name="get" hint="wait until done completing. (joins all the threads), and returns an array of results" access="public" returntype="array" output="false"> | |
<cfscript> | |
for(var name in variables.threadNames) | |
{ | |
threadJoin(name); | |
} | |
return variables.results; | |
</cfscript> | |
</cffunction> | |
<!------------------------------------------- PACKAGE -------------------------------------------> | |
<!------------------------------------------- PRIVATE -------------------------------------------> | |
<cffunction name="pollAndRun" hint="polls the queue and runs the item" access="private" returntype="void" output="false"> | |
<cfscript> | |
var item = variables.queue.poll(); | |
</cfscript> | |
<cfif !isNull(item)> | |
<cftry> | |
<cfinvoke component="#variables.worker#" method="#variables.workerMethodName#" argumentcollection="#item#" returnvariable="local.result" /> | |
<cfcatch> | |
<cfscript> | |
arrayAppend(getErrors(), cfcatch); | |
</cfscript> | |
</cfcatch> | |
</cftry> | |
<cfscript> | |
if(structKeyExists(local, "result")) | |
{ | |
arrayAppend(variables.results, local.result); | |
} | |
</cfscript> | |
</cfif> | |
</cffunction> | |
</cfcomponent> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment