lucee.Componentwirebox.system.async.tasks.Future
This is the ColdBox Future object modeled and backed by Java's CompletableFuture but with Dynamic Goodness!
Property Summary | ||||
---|---|---|---|---|
type | property | default | serializable | required |
boolean
|
debug
Add debugging output to the thread management and operations.
|
false
|
true
|
false
|
any
|
executor
The custom executor to use with the future execution, or it can be null.
|
true
|
false
|
|
struct
|
futureTimeout
The timeout you can set on this future via the withTimeout() method.
|
true
|
false
|
|
boolean
|
loadAppContext
Load the CFML App context and page context's into the spawned threads.
|
true
|
true
|
false
|
any
|
native
The native Completable future we model: java.
|
true
|
false
|
Constructor Summary | |
---|---|
init([any value], [any executor], [boolean debug='false'], [boolean loadAppContext='true'])
Construct a new ColdBox Future backed by a Java Completable Future. |
Method Summary | |
---|---|
Future
|
all()
This method accepts an infinite amount of future objects, closures or an array of future objects/closures. |
any
|
allApply([any items], [any fn], [any executor], [any timeout], [any timeUnit])
This function can accept an array of items or a struct of items and apply a function. |
Future
|
anyOf()
This method accepts an infinite amount of future objects or closures and will execute them in parallel. |
boolean
|
cancel([boolean mayInterruptIfRunning='true'])
If not already completed, completes this Future with a CancellationException. |
boolean
|
complete(any value)
If not already completed, sets the value returned by get() and related methods to the given value. |
Future
|
completeExceptionally([any message='Future operation completed with manual exception'])
If not already completed, causes invocations of get() and related methods to throw the given exception. |
Future
|
completeOnTimeout(any value, any timeout, [any timeUnit='milliseconds'])
Completes this CompletableFuture with the given value if not otherwise completed before the given timeout. |
Future
|
completeWithException()
Alias to completeExceptionally. |
Future
|
completedFuture(any value)
Returns a new ColdBox Future that is already completed with the given value. |
Future
|
exceptionally(any target)
Register an event handler for any exceptions that happen before it is registered. |
private any
|
futuresWrap()
This utility wraps in the coming futures or closures and makes sure the return. |
any
|
get([numeric timeout='0'], [string timeUnit='milliseconds'], [any defaultValue])
Waits if necessary for at most the given time for this future to complete, and then returns its result, if available. |
string
|
getDebug()
|
string
|
getExecutor()
|
string
|
getFutureTimeout()
|
string
|
getLoadAppContext()
|
string
|
getNative()
|
any
|
getNow(any defaultValue)
Returns the result value (or throws any encountered exception) if completed, else returns the given defaultValue. |
Future
|
handle(any action)
Returns a new CompletionStage that, when this stage completes either normally or exceptionally, is executed with this stage's result. |
Future
|
handleAsync(any action, [any executor])
Returns a new CompletionStage that, when this stage completes either normally or exceptionally,. |
boolean
|
isCancelled()
Returns true if this Future was cancelled before it completed normally. |
boolean
|
isCompletedExceptionally()
Returns true if this Future completed exceptionally, in any way. |
boolean
|
isDone()
Returns true if completed in any fashion: normally, exceptionally, or via cancellation. |
any
|
join([any defaultValue])
Returns the result value when complete, or throws an (unchecked) exception if completed exceptionally. |
any
|
onException(any target)
Alias to exceptionally(). |
Future
|
orTimeout(any timeout, [any timeUnit='milliseconds'])
Exceptionally completes this CompletableFuture with a TimeoutException if not otherwise completed before the given timeout. |
Future
|
run(any supplier, [any method='run'], [any executor='[runtime expression]'])
Executes a runnable closure or component method via Java's CompletableFuture and gives you back a ColdBox Future:. |
Future
|
runAsync(any runnable, [any executor])
Alias to the `run()` method but left here to help Java developers. |
any
|
setDebug(boolean debug)
|
any
|
setExecutor(any executor)
|
any
|
setFutureTimeout(struct futureTimeout)
|
any
|
setLoadAppContext(boolean loadAppContext)
|
any
|
setNative(any native)
|
Future
|
supplyAsync(any supplier, [any executor])
Alias to the `run()` method but left here to help Java developers. |
Future
|
then(any target)
Executed once the computation has finalized and a result is passed in to the target:. |
Future
|
thenAccept()
Alias to thenRun(). |
Future
|
thenAcceptAsync()
Alias to thenRunAsync(). |
Future
|
thenApply()
Alias to `then()` left to help Java devs feel at Home. |
Future
|
thenApplyAsync()
Alias to `thenAsync()` left to help Java devs feel at Home. |
Future
|
thenAsync(any target, [any executor])
Executed once the computation has finalized and a result is passed in to the target but. |
Future
|
thenCombine(any future, [any fn])
This used when you want two Futures to run independently and do something after. |
Future
|
thenCompose(any fn)
Returns a new CompletionStage that, when this stage completes normally,. |
Future
|
thenRun(any target)
Returns a new CompletionStage that, when this stage completes normally, is executed with this. |
Future
|
thenRunAsync(any target, [any executor])
Returns a new CompletionStage that, when this stage completes normally,. |
Future
|
whenComplete(any action)
Returns a new CompletionStage with the same result or exception as this stage, that executes the given action when this stage completes. |
Future
|
whenCompleteAsync(any action, [any executor])
Returns a new CompletionStage with the same result or exception as this stage, that executes the given action using this stage's. |
Future
|
withTimeout([numeric timeout='0'], [string timeUnit='milliseconds'])
This method seeds a timeout into this future that can be used by the following operations:. |
Methods inherited from class lucee.Component |
---|
None |
Constructor Detail |
---|
Construct a new ColdBox Future backed by a Java Completable Future
value
- The actual closure/lambda/udf to run with or a completed value to seed the future withexecutor
- A custom executor to use with the future, else use the defaultdebug
- Add output debuggingloadAppContext
- Load the CFML App contexts or not, disable if not usedProperty Detail |
---|
Add debugging output to the thread management and operations. Defaults to false
access
- publicrequired
- falsereturntype
- anyserializable
- trueThe custom executor to use with the future execution, or it can be null
access
- publicrequired
- falsereturntype
- anyserializable
- trueThe timeout you can set on this future via the withTimeout() method which is used in operations like allOf() and anyOf()
access
- publicrequired
- falsereturntype
- anyserializable
- trueLoad the CFML App context and page context's into the spawned threads. Defaults to true
access
- publicrequired
- falsereturntype
- anyserializable
- trueThe native Completable future we model: java.util.concurrent.CompletableFuture
access
- publicrequired
- falsereturntype
- anyserializable
- trueMethod Detail |
---|
This method accepts an infinite amount of future objects, closures or an array of future objects/closures in order to execute them in parallel. It will return back to you a future that will return back an array of results from every future that was executed. This way you can further attach processing and pipelining on the constructed array of values.
results = all( f1, f2, f3 ).get() all( f1, f2, f3 ).then( (values) => logResults( values ) );
This function can accept an array of items or a struct of items 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
// Array allApply( items, ( item ) => item.getMemento() ) // Struct: The result object is a struct of `key` and `value` allApply( data, ( item ) => item.key & item.value.toString() )
items
- An array or struct to process in parallelfn
- The function that will be applied to each of the collection's itemsexecutor
- The custom executor to use if passed, else the forkJoin Pooltimeout
- The timeout to use when waiting for each item to be processedtimeUnit
- The time unit to use, available units are: days, hours, microseconds, milliseconds, minutes, nanoseconds, and seconds. The default is millisecondsThis method accepts an infinite amount of future objects or closures and will execute them in parallel. However, instead of returning all of the results in an array like allOf(), this method will return the future that executes the fastest!
// Let's say f2 executes the fastest! f2 = anyOf( f1, f2, f3 )
If not already completed, completes this Future with a CancellationException. Dependent Futures that have not already completed will also complete exceptionally, with a CompletionException caused by this CancellationException.
mayInterruptIfRunning
If not already completed, sets the value returned by get() and related methods to the given value.
value
- The value to setIf not already completed, causes invocations of get() and related methods to throw the given exception. The exception type is of `java.lang.RuntimeException` and you can choose the message to throw with it.
message
- An optional message to add to the exception to be thrown.Completes this CompletableFuture with the given value if not otherwise completed before the given timeout.
value
- The value to use upon timeouttimeout
- how long to wait before completing normally with the given value, in units of unittimeUnit
- The time unit to use, available units are: days, hours, microseconds, milliseconds, minutes, nanoseconds, and seconds. The default is millisecondsAlias to completeExceptionally
Returns a new ColdBox Future that is already completed with the given value.
value
- The value to setRegister an event handler for any exceptions that happen before it is registered in the future pipeline. Whatever this function returns, will be used for the next registered functions in the pipeline. The function takes in the exception that occurred and can return a new value as well:
( exception ) => newValue; function( exception ) => { return newValue; }Note that, the error will not be propagated further in the callback chain if you handle it once.
target
- The function that will be called when the exception is triggeredThis utility wraps in the coming futures or closures and makes sure the return is an array of futures.
Waits if necessary for at most the given time for this future to complete, and then returns its result, if available. If the result is null, then you can pass the defaultValue argument to return it.
timeout
- The timeout value to use, defaults to forevertimeUnit
- The time unit to use, available units are: days, hours, microseconds, milliseconds, minutes, nanoseconds, and seconds. The default is millisecondsdefaultValue
- If the Future did not produce a value, then it will return this default value.Returns the result value (or throws any encountered exception) if completed, else returns the given defaultValue.
defaultValue
- The value to return if not completedReturns a new CompletionStage that, when this stage completes either normally or exceptionally, is executed with this stage's result and exception as arguments to the supplied function. When this stage is complete, the given function is invoked with the result (or null if none) and the exception (or null if none) of this stage as arguments, and the function's result is used to complete the returned stage. The action is a closure/udf with the incoming input (if any) or an exception (if any) and returns a new result if you want
handle( (input, exception) => {} ) handle( function( input, exception ){} )
action
- the function to use to compute the value of the returned CompletionStageReturns a new CompletionStage that, when this stage completes either normally or exceptionally, is executed using this stage's default asynchronous execution facility, with this stage's result and exception as arguments to the supplied function. When this stage is complete, the given function is invoked with the result (or null if none) and the exception (or null if none) of this stage as arguments, and the function's result is used to complete the returned stage. The action is a closure/udf with the incoming input (if any) or an exception (if any) and returns a new result if you want
handleAsync( (input, exception) => {} ) handleAsync( function( input, exception ){} ) handleAsync( (input, exception) => {}, asyncManager.$executors.newFixedThreadPool() )
action
- the function to use to compute the value of the returned CompletionStageexecutor
- the java executor to use for asynchronous execution, can be emptyReturns true if this Future was cancelled before it completed normally.
Returns true if this Future completed exceptionally, in any way. Possible causes include cancellation, explicit invocation of completeWithException, and abrupt termination of a CompletionStage action.
Returns true if completed in any fashion: normally, exceptionally, or via cancellation.
Returns the result value when complete, or throws an (unchecked) exception if completed exceptionally. To better conform with the use of common functional forms, if a computation involved in the completion of this CompletableFuture threw an exception, this method throws an (unchecked) CompletionException with the underlying exception as its cause.
defaultValue
- If the returned value is null, then we can pass a default value to returnAlias to exceptionally()
target
Exceptionally completes this CompletableFuture with a TimeoutException if not otherwise completed before the given timeout.
timeout
- how long to wait before completing normally with the given value, in units of unittimeUnit
- The time unit to use, available units are: days, hours, microseconds, milliseconds, minutes, nanoseconds, and seconds. The default is millisecondsExecutes a runnable closure or component method via Java's CompletableFuture and gives you back a ColdBox Future: - This method calls `supplyAsync()` in the Java API - This future is asynchronously completed by a task running in the ForkJoinPool.commonPool() with the value obtained by calling the given Supplier.
supplier
- A CFC instance or closure or lambda or udf to execute and return the value to be used in the futuremethod
- If the supplier is a CFC, then it executes a method on the CFC for you. Defaults to the `run()` methodexecutor
- An optional executor to use for asynchronous execution of the taskAlias to the `run()` method but left here to help Java developers feel at home. Since in our futures, everything becomes a supplier of some sort.
runnable
- A CFC instance or closure or lambda or udf to execute and return the value to be used in the futureexecutor
- An optional executor to use for asynchronous execution of the taskdebug
executor
futureTimeout
loadAppContext
native
Alias to the `run()` method but left here to help Java developers feel at home. Since in our futures, everything becomes a supplier of some sort.
supplier
- A CFC instance or closure or lambda or udf to execute and return the value to be used in the futureexecutor
- An optional executor to use for asynchronous execution of the taskExecuted once the computation has finalized and a result is passed in to the target: - The target can use the result, manipulate it and return a new result from the this completion stage - The target can use the result and return void - This stage executes in the calling thread
// Just use the result and not return anything then( (result) => systemOutput( result ) ) // Get the result and manipulate it, much like a map() function then( (result) => ucase( result ) );
target
- The closure/lambda or udf that will receive the resultAlias to thenRun()
Alias to thenRunAsync()
Alias to `then()` left to help Java devs feel at Home Remember, the closure accepts the data and MUST return the data if not the next stage could be receiving a null result.
Alias to `thenAsync()` left to help Java devs feel at Home
Executed once the computation has finalized and a result is passed in to the target but this will execute in a separate thread. By default it uses the ForkJoin.commonPool() but you can pass your own executor service. - The target can use the result, manipulate it and return a new result from the this completion stage - The target can use the result and return void
// Just use the result and not return anything then( (result) => systemOutput( result ) ) // Get the result and manipulate it, much like a map() function then( (result) => ucase( result ) );
target
- The closure/lambda or udf that will receive the resultexecutor
This used when you want two Futures to run independently and do something after both are complete.
future
- The ColdBox Future to combinefn
- The closure that will combine them: ( r1, r2 ) =>Returns a new CompletionStage that, when this stage completes normally, is executed with this stage as the argument to the supplied function. Basically, this used to combine two Futures where one future is dependent on the other If not, you return a future of a future
fn
- the function returning a new CompletionStageReturns a new CompletionStage that, when this stage completes normally, is executed with this stage's result as the argument to the supplied action. See the CompletionStage documentation for rules covering exceptional completion. - The target can use the result and return void - This stage executes in the calling thread
// Just use the result and not return anything thenRun( (result) => systemOutput( result ) )
target
- The action to perform before completing the returned CompletionStageReturns a new CompletionStage that, when this stage completes normally, is executed using this stage's default asynchronous execution facility, with this stage's result as the argument to the supplied action. See the CompletionStage documentation for rules covering exceptional completion. - The target can use the result and return void - This stage executes in the passed executor or the stage's executor facility
// Just use the result and not return anything thenRunAsync( (result) => systemOutput( result ) ) thenRunAsync( (result) => systemOutput( result ), myExecutor )
target
- The action to perform before completing the returned CompletionStageexecutor
- If passed, the executor to use to run the targetReturns a new CompletionStage with the same result or exception as this stage, that executes the given action when this stage completes. When this stage is complete, the given action is invoked with the result (or null if none) and the exception (or null if none) of this stage as arguments. The returned stage is completed when the action returns. If the supplied action itself encounters an exception, then the returned stage exceptionally completes with this exception unless this stage also completed exceptionally. The action is a closure/udf with the incoming input (if any) or an exception (if any) and returns void.
whenComplete( (input, exception) => {} ) whenComplete( function( input, exception ){} )
action
- the action to performReturns a new CompletionStage with the same result or exception as this stage, that executes the given action using this stage's default asynchronous execution facility when this stage completes. When this stage is complete, the given action is invoked with the result (or null if none) and the exception (or null if none) of this stage as arguments. The returned stage is completed when the action returns. If the supplied action itself encounters an exception, then the returned stage exceptionally completes with this exception unless this stage also completed exceptionally. The action is a closure/udf with the incoming input (if any) or an exception (if any) and returns void.
whenCompleteAsync( (input, exception) => {} ) whenCompleteAsync( function( input, exception ){} ) whenCompleteAsync( (input, exception) => {}, asyncManager.$executors.newFixedThreadPool() )
action
- the action to performexecutor
- the java executor to use for asynchronous execution, can be emptyThis method seeds a timeout into this future that can be used by the following operations: - allApply()
timeout
- The timeout value to use, defaults to forevertimeUnit
- The time unit to use, available units are: days, hours, microseconds, milliseconds, minutes, nanoseconds, and seconds. The default is milliseconds