Parallel Computations
Here are some of the methods that will allow you to do parallel computations. Please note that the asyncManager() has shortcuts to these methods, but we always recommend using them via a new future, because then you can have further constructor options like: custom executor, debugging, loading CFML context and much more.
    all( a1, a2, ... ):Future : This method accepts an infinite amount of future objects, closures or an array of closures/futures in order to execute them in parallel. It will return a future that when you call get() on it, it will retrieve an array of the results of all the operations.
    allApply( items, fn, executor ):array : This function can accept an array of items or a struct of items of any type and apply a function to each of the item's in parallel. The fn argument receives the appropriate item and must return a result. Consider this a parallel map() operation.
    anyOf( a1, a2, ... ):Future : This method accepts an infinite amount of future objects, closures or an array of closures/futures and will execute them in parallel. However, instead of returning all of the results in an array like all(), this method will return the future that executes the fastest! Race Baby!
    withTimeout( timeout, timeUnit ) : Apply a timeout to all() or allApply() operations. The timeUnit can be: days, hours, microseconds, milliseconds, minutes, nanoseconds, and seconds. The default is milliseconds.
Please note that some of the methods above will return a ColdBox Future object that is backed by Java's CompletableFuture (https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html)
Here are the method signatures for the methods above, which you can call from the asyncManager or a newly created future.
1
/**
2
* This method accepts an infinite amount of future objects, closures or an array of future objects/closures
3
* in order to execute them in parallel. It will return back to you a future that will return back an array
4
* of results from every future that was executed. This way you can further attach processing and pipelining
5
* on the constructed array of values.
6
*
7
* <pre>
8
* results = all( f1, f2, f3 ).get()
9
* all( f1, f2, f3 ).then( (values) => logResults( values ) );
10
* </pre>
11
*
12
* @result A future that will return the results in an array
13
*/
14
Future function all(){
15
16
/**
17
* This function can accept an array of items or a struct and apply a function
18
* to each of the item's in parallel. The `fn` argument receives the appropriate item
19
* and must return a result. Consider this a parallel map() operation
20
*
21
* <pre>
22
* // Array
23
* allApply( items, ( item ) => item.getMemento() )
24
* // Struct: The result object is a struct of `key` and `value`
25
* allApply( data, ( item ) => item.key & item.value.toString() )
26
* </pre>
27
*
28
* @items An array to process
29
* @fn The function that will be applied to each of the array's items
30
* @executor The custom executor to use if passed, else the forkJoin Pool
31
*
32
* @return An array with the items processed
33
*/
34
any function allApply( any items, required fn, executor ){
35
36
/**
37
* This method accepts an infinite amount of future objects or closures and will execute them in parallel.
38
* However, instead of returning all of the results in an array like all(), this method will return
39
* the future that executes the fastest!
40
*
41
* <pre>
42
* // Let's say f2 executes the fastest!
43
* f2 = anyOf( f1, f2, f3 )
44
* </pre>
45
*
46
* @return The fastest executed future
47
*/
48
Future function anyOf()
49
50
/**
51
* This method seeds a timeout into this future that can be used by the following operations:
52
*
53
* - all()
54
* - allApply()
55
*
56
* @timeout The timeout value to use, defaults to forever
57
* @timeUnit The time unit to use, available units are: days, hours, microseconds, milliseconds, minutes, nanoseconds, and seconds. The default is milliseconds
58
*
59
* @returns This future
60
*/
61
Future function withTimeout( numeric timeout = 0, string timeUnit = "milliseconds" )
Copied!
Here are some examples:
1
// Let's find the fastest dns server
2
var f = asyncManager().anyOf( ()=>dns1.resolve(), ()=>dns2.resolve() );
3
4
// Let's process some data
5
var data = [1,2, ... 100 ];
6
var results = asyncManager().all( data );
7
8
// Process multiple futures
9
var f1 = asyncManager.newFuture( function(){
10
return "hello";
11
} );
12
var f2 = asyncManager.newFuture( function(){
13
return "world!";
14
} );
15
var aResults = asyncManager.newFuture()
16
.withTimeout( 5 )
17
.all( f1, f2 );
18
19
// Process mementos for an array of objects
20
function index( event, rc, prc ){
21
return async().allApply(
22
orderService.findAll(),
23
( order ) => order.getMemento()
24
);
25
}
Copied!

Custom Executors

Please also note that you can choose your own Executor for the parallel computations by passing the executor via the newFuture() method.
1
var data = [ 1 ... 5000 ];
2
var results = newFuture( executor : asyncManager.$executors.newCachedThreadPool() )
3
.all( data );
4
5
// Process mementos for an array of objects on a custom pool
6
function index( event, rc, prc ){
7
return async().allApply(
8
orderService.findAll(),
9
( order ) => order.getMemento(),
10
async().$executors.newFixedThreadPool( 50 )
11
);
12
}
Copied!
Last modified 1yr ago
Copy link