WorkshopAsync Programming & SchedulingIn this session, we will explore the concepts of asynchronous and parallel programming with the ColdBox Async Manager and Java's Completable Futures APIs. We will explore the concept of async computations, async pipelines, exception handling, scheduling and so much more. Pain medication is not included.PrerequisitesThe following are the workshop requirements for this workshop:CommandBox latest installedOpen JDK 11+ or CompatibleVSCode Editor (Preferred) with the Kamasamak CFML Extension installedcfscript KnowledgeBeen bitten or (want to avoid being bitten) by <cfthread>!Repohttps://gitlab.ortushq.com/books/coldbox-async-programming-and-schedulingResourcesJava 8 Fundamentals: Asynchronous Programming Using CompletionStageJava CompletableFutureA Guide to the Java ExecutorServiceOverview of the java.util.concurrentGuide to Java's Future and CompletableFutureSchedule / Outline
1. Introduction
The NB (non-blocking) movementHas made NodeJS shine, but other languages support this as well.Movement to more async/parallel computationsOverall movement to computationsLessons from Callbacks
Also referred to as Callback hellTODO: EXAMPLE01-callback-hell.mdCan make developers cryNever ending nesting collection of closures/functionsWho has been there before? Don't lie!Movement to promisesJavaScript has made this very popularTo get some sanity back into to development from call back hellfireCan have 3 different states:
Resolve : When completedReject : Error or something elsePending : Not finished executing yetCancel and Resolve them programmaticallyTwo channels of communication Promises TrackDataErrorWhat about ColdFusion?
cfthread, right? right?
Great for very basic threadingEasy, 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 dataHard to manage them (cancel, monitor, pause)No functional approachesManaging multiple threads and joining can be is cumbersome and horribleExample:
01-coldbox-interceptor-state (Show them the InterceptorState.processAsyncAll() method)Both Challenge, convert to async packageNothing existed until ACF2018/Lucee 5.3 => runAsync()A step up, but not a big stepStill Many Issues:
Backed by a custom wrapper to java.util.concurrent.FutureSimplistic 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 new singleThreadExecutor() for EVERY then() operation.No way to operate on multiple futures at once
TODO: Eric ExampleNo way to combine/compose futuresOnly works with closures, does not work on actually calling component methodsxmap(), xeach() parallel processing
Adobe 2021 + LuceeLimited to 50 threads in Adobe, 10 standardEasy to useNot easy to do exception handlingShort circuit exception handling
NO control over itIt will break out whenever an exception is detected and leave unprocessed items in a collectionNo expected consistencyAsynchronous programming is NOT EASY!Concurrency issues / Race conditions
Know the limitations on shared resourcesSome data structures allow for multiple threads, some don't, some you need to wrap to make them concurrentjava.util.concurrent packageRead before writeLocking becomes part of your life, unfortunatelyRead before WriteAtomic operationsIdempotent operationsTimestampingDealing with multiple threads
cfthread you have to be the managerrunAsync you can't be the managerSharing data between threadsNo more writedump/abort
Logs become your best friendColdBox Debugger Panel : tracers, all incoming events are trackedEasy to spot an asynchronous developer, he is in the corner, crying.We have two approaches to threading which are extremely simplistic and not powerful at all
2. Intro to ColdBox Futures
Similar to JavaScript Promises but in CF backed by CompletableFuture Java APIWhat 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 FuturesColdBox, WireBox, CacheBox and LogBoxAdapted for our dynamic language => many enhancementsAsyncManager class in charge of all async capabilities
Standalone: Part of WireBox — create an instance as a singletonColdBox: async() or inject:AsyncManager@coldboxFunctions:
Create FuturesCreate/Manage ExecutorsCreate/Manage Schedule TasksIntro to Executors
What is an executorDefault executorWe can customize it later on.cbFutures Stages - 02-cbfutures-stages.mdRunningCompletedCompletedExceptionallyCancelledCreate a future - 03-creating-future.cfc
EXAMPLE
Make a future that prints to the consoleMake a future that returns a message to print to the console after 2 secondsSomething about timeout and default values….
CAVEAT, this does not stop the threadMake a future that throws an exception and see what happens with .get()RUNNING EXAMPLE OPTION
Read 3 files asynchronouslyPrint the output in orderPrint the output as soon as you can
3. Magical Pipelines
Java API: thenApply(), thenAccept(), thenRun(), why?
CF API: then() (Easier + Dynamic API)
Curiosity: CompletableFutures never end! This is So Ironic! Sharknado! :shark:Data TransformationsDealing with TimeoutsSuccess on timeoutException on timeoutPipelines are independent of dataEXAMPLE:
Simple examplePipelines are important especially in looping operations
4. Dealing With Exceptions
Dealing with ExceptionsBuilt-in Logginghandle Methods, which basically deal with both the result and an exception objectCustom LoggingRecovering with dataBONUS: Recovering with a FutureEXAMPLE
I would love an example that captures an exception, recovers, and continues the pipelineShow how exceptions skip .then methods until recovered.future // throws
.then() // skip
.then() // skip
.onException( ) // called and recovered
.then(); // called
.onException( ) // called for the `onException` and last `then`
Global Exception Handlers - Global ColdBox Exception Handler
5. Executors - Be the Manager!
Thread of executionChanging the poolRegister many different types of executors/poolFixed : Control the amount of threads, cpu intensive, io intensiveSingle : A processing queue FIFOCached : Ever expanding demand queueScheduled : Scheduled TasksChecking statusShutdowns and Awaiting TerminationsKilling Tasks, really?EXAMPLE:
Make a FIFO thread poolChoose the correct thread pool for the task
6. Parallel Tasks
https://www.callicoder.com/java-8-completablefuture-tutorial/Combining FuturesVery much like a reduce() operationRemember this: 1-1 operationhttps://www.callicoder.com/java-8-completablefuture-tutorial/Composing FuturesMonadic design pattern (https://medium.com/thg-tech-blog/monad-design-**pattern**-in-java-3391d4095b3f)Hmm: 2-1 Operation, future of futures!Racing FuturesAll FuturesAll futures ModifiedEXAMPLES:
Promise.all()Promise.race()
7. Futures in a ColdBox App
In server land!Start a worker that monitors something
Runs some unit of work when that something is triggeredPoor man's worker queuehttps://www.logicbig.com/tutorials/core-java-tutorial/java-nio/java-watch-service.htmlhttps://fullstackdeveloper.guru/2020/12/23/how-to-watch-a-folder-directory-or-changes-using-java/Watch a directory and create pdfs or scale images
8. Scheduled Tasks
This is the third item's accordion body. It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the .accordion-body, though the transition does limit overflow.
9. Bonus
Easier debugging with cbDebuggerAsync Programming & SchedulingWho is the audience for the workshop?Developers who are interested in learning about asynchronous, parallel programming and task scheduling. They must definitely have advanced experience in the CFML language.If you have ever wished that using <cfthread> or runAsync() was easier or more flexible; if you have ever wanted to work with something like JavaScript Promises in CFML; if you have ever got a headache trying to debug asynchronous code; if you have ever wanted to squeeze ever ounce of performance out of your application…Then this is the workshop for you.Why should Youtake the workshop?In this workshop, you will be introduced to the wonderful and complex world of concurrency. We will go over the basics of asynchronous/parallel programming and introduce the concepts of working with futures. We will also work on the concept of async processing pipelines, exception handling and debugging.Scheduled tasks have always been a point of soreness for many developers in ANY language. Especially choosing where to place them for execution: should it be cron? windows task scheduler? ColdFusion engine? Jenkins, Gitlab? and the list goes on and on. The ColdBox Scheduled Tasks offers a fresh, programmatic, fluent, chainable, and human approach to scheduling tasks on your server and multi-server application. So if you are interested in making your scheduled tasks portable, testable, and sane, then this workshop is for you.What will the workshop coverIntroduction to asynchronous programmingWhat are promises/futuresNative ways for asynchronous programming in CFMLIntro to ColdBox FuturesExecutorsMagical PipelinesDealing with ExceptionsParallel TasksFutures In a ColdBox AppScheduled TasksBonus SectionPrerequisitesCFScript KnowledgeAdvanced CFMLBeen bitten or (want to avoid being bitten) by <cfthread>!You will leave the workshop with:A headache A free day's supply of AdvilAll of Luis and Eric's hard learned lessons, without the battle scars or the crying An understanding on how to use ColdBox AsynchronouslyA desire to rewrite all your CFThread code to use ColdBox AsyncA desire to switch all your scheduled tasks to ColdBox SchedulingA desire to create async task workersTopicsCOLDBOXAsyncSpeakerLuis MajanoBrad Wood
↧