by Luis Majano - Ortus Solutions
In this session we will explore the asynchronous and parallel programming constructs built into ColdBox. Java has supported a robust and functional approach to asynchronous programming since JDK8 and now it is available to us all in the Coldfusion (CFML) ⚡ World! To the future!
- 📧 Email: lmajano@ortussolutions.com
- 🐦 Twitter: @lmajano @ortussolutions
- 🔗 Site: www.ortussolutions.com
- 🔗 Code: https://github.com/lmajano/to-the-future-with-cbFutures
-
[] The NB (non-blocking) movement
- [] Has made NodeJS shine, but other languages support this as well.
- [] Movement to more async/parallel computations
- [] Overall movement to computations
-
[] Lessons from Callbacks
- [] Also refered to as Callback hell 🔥
- Example: Callback Hell
- [] Can make developers cry 😢
- [] Never ending nesting collection of closures/functions 🕷️
- [] Callbacks could be called multiple times by the other library.
- [] Who has been there before? Don't lie! 🤥
- [] Also refered to as Callback hell 🔥
-
[] Movement to promises
- [] JavaScript has made this very popular
- [] To get some sanity back into to development from call back hellfire 🔥
- [] What is a promise?
- [] Can have 3 different states:
Resolve
: When completedReject
: Error or something elsePending
: Not executing just yet
- [] Cancel and Resolve them programmatically
- [] Two channels of communication Promises Track
Data
Error
-
[] What about ColdFusion?
- []
cfthread
, right?? right? right? 🤔- [] Great BUUUUUUT for very very very very basic threading
- [] Easy, but plagued with issues, which makes developers ALSO cry :😢
- [] No way to choose where it runs (thread pool)
- [] No concept of returning data, it's up to you to monitor/track data
- [] Hard to manage them (cancel, monitor, pause), you are on your own buddy!
- [] No functional approaches
- [] Managing multiple threads and joining can be cumbersome and horrible
- [] Example: Interceptor State Manager - Process Async All
- [] Nothing existed until ACF2018/Lucee 5.3 =>
runAsync()
- [] A step up, but not a big step
- [] Still Many Issues:
- [] Backed by a custom wrapper to
java.util.concurrent.Future
, jdk6 - [] Simplistic error handler with no way to recover or continue executing pipelines after an exception. Concept of two tracks is broken!
- [] No way to choose or reuse the executor to run the sub-sequent
then()
operations. Lucee actually creates a newsingleThreadExecutor()
for EVERYthen()
operation. - [] No way to operate on multiple futures at once
- [] No way to combine/compose futures
- [] Only works with closures, does not work on actually calling component methods
- [] Backed by a custom wrapper to
- [] 🤢 We have two approaches to threading which are extremely simplistic and not powerful at all.
- []
-
[] What about Java?
- [] JDK 8 Introduced
CompletableFutures
,CompletionStages
, Executors, Lambdas and much more. - [] 🦄 Java
CompletableFutures
are like JavaScript Promises, but you know Java devs, over complicate things, even names! - [] We have ported the majority of this functionality to CFML:
ColdBox Futures
- ColdBox, WireBox, CacheBox and LogBox
- [] JDK 8 Introduced
-
[] What is a ColdBox Future?
- [] Similar to JavaScript Promises but in CF backed by
CompletableFuture
Java API - [] Adapted for our dynamic language => many enhancements
- []
AsyncManager
class in charge of all async capabilities- [] Standalone: create an instance as a singleton
- [] ColdBox:
async()
orinject:AsyncManager@coldbox
- [] Functions:
- Create Futures
- Create/Manage Executors
- Create/Manage Schedule Tasks
- Utility Functions
- https://s3.amazonaws.com/apidocs.ortussolutions.com/coldbox/6.8.1/coldbox/system/async/AsyncManager.html
- [] Create the Future!
- [] cbFutures Stages
- [] Create a future
- [] Thread of execution
- [] Changing the pool
- [] Register many different types of executors/pool
- []
Fixed
: Control the amount of threads, cpu intensive, io intensive - []
Single
: A processing queue FIFO - []
Cached
: Ever expanding demand queue - []
Scheduled
: Scheduled Tasks
- []
- [] Register many different types of executors/pool
- [] Similar to JavaScript Promises but in CF backed by
-
[] 🎩 Magical Pipelines
- [] Java API:
thenApply()
,thenAccept()
,thenRun()
, why? - [] CF API:
then()
andthenRun()
(Easier + Dynamic API)- [] Curiosity: CompletableFutures never end! This is So Ironic! Sharknado! 🦈
- Data Transformations
- Dealing with Exceptions
- Dealing with Timeouts
- Success on timeout
- Exception on timeout
- Checking status
- [] Java API:
-
[] Working with multiple futures
-
[] Extra Credit: Schedule Tasks!