Queueable Apex : Loose Coupling with Chainable Interface
Intro to Queueable Apex
Time and again, we come across use cases where we have to either perform an extensive processing, which can breach governor limits, or we want to perform some work asynchronously for improved user experience. Queueable apex provides us a means to create an asynchronous job (An asynchronous job means a piece of work, which is scheduled to run in background, for e.g. you may want to do custom rollup calculations on opportunity save).
Queueable apex is similar to future methods, wherein your future method is queued within a server queue. Server picks up the job and executes it, based on resource availability. However, there are some differences, quite notably:
- Queueable apex returns an ID for further enquiry
- Queueable apex methods can invoke external API (if Queueable apex class implements Database.AllowsCallouts interface)
- Queueable apex methods can enqueue other queueable apex jobs
Chaining Queueable Apex jobs
Tight coupling with Queueable Apex
I created a simple wrapper class to address this complexity around chaining queueable apex jobs at runtime, while keeping individual queueable apex classes loosely coupled. Another key design factor I kept in mind was to not add a complete wrapper on the Queueable apex class, rather extend its capabilities.
Implement Loose Coupling with Chainable Interface
- both classes now extend QueueableChainJob abstract class
- both classes have execute method calling enqueueNextJob();