Simplify Threaded Processing with FutureThreadedWorker

Having worked with both Groovy, (J)Ruby and, Java and (to a much smaller degree) Clojure, I can pretty easily claim that ColdFusion's threading and distributed processing capabilities.. leave a little to be desired. (Okay, actually a whole lot).

There are solutions though – using a job queuing system like Amazon SQS, IronMQ, or if you are looking for a something more threaded, dig into Marc Esher's awesome cfconcurrent project.

But sometimes you just want to take an array of values and run them through some sort of processing in a threaded manner, without having to jump onto any other projects or infrastructures, and without having to write that nasty <cfthread> create and join pattern for the millionth time.

So I wrote a quick CFC I've called 'FutureThreadedWorker', which does exactly that, without half the code you would otherwise need. (Note: this was written for ColdFusion 9. It would be a bit nicer if I could use closures)


<cfscript>
   //create, passing in an array for the array of structures (which are the arguments passed to the workerMethod)
   //then pass it the worker to call the worker method on
   //tell it how many thread it's should use
   //finally, tell it the name of the method to invoke.
   var future = new services.util.FutureThreadedWorker(queue, this, 5, "doWork");

   //start the running

   future.run();

   //if I want the results, wait for them.

   var results = future.get();

   //if i want to check for errors

   var errors = resuls.getErrors();
</cfscript>

Bam. That's it.  The FutureThreadedWorker now loops around the passed in array, and passed in the arguments to the method specified on the worker without you having to worry about creating threads or joining them back up again. I think you'll find that a lot easier than using <cfthread>.

Leave a Comment

Comments

  • John Allen | November 27, 2012

    Totally cool. Saves writing a lot of boiler plate code. Your smart.