Queueable Apex – Performing asynchronous logic within Salesforce – Looking into the @future



Queueable Apex – Performing asynchronous logic within Salesforce – Looking into the @future

0 0


queueable-apex

Working with Queueable Apex. Presentation given at DC Salesforce Dev Meetup February 2016 event.

On Github scottbcovert / queueable-apex

Queueable Apex

Performing asynchronous logic within Salesforce

Presented by Scott Covert / @scottbcovert

Looking into the @future

global class MyFutureClass {

  @future 
  static void myMethod(String a, Integer i) {
    System.debug('Method called with: ' + a + ' and ' + i);
    // Perform long-running code
  }
}
						

Allows you to kick-off long-running processes asynchronously to prevent CPU timeout

  @future (callout=true)
  public static void doCalloutFromFuture() {
   //Add code to perform callout
}						
  • Adding the @future annotation allows a method to be called asynchronously
  • Popular use case: making a callout from within a Trigger or after a DML statement

Limitations

  • @future methods cannot call @future methods (including via triggers)
  • Parameters can only be instances/collections of primitive types (no passing SObjects)
  • Unknown execution order
  • Future methods cannot call other future methods
  • Parameters can only be primitives or collections thereof
  • You have no guarantees one async method will fire before another even if it was initiated first

Workaround

Dan Appleman discusses a popular workaround to some asynchronous Apex limitations within his book, 'Advanced Apex Programming' He also gave a great talk on this topic, as well as the topic of concurrency (which we'll see later is related) at DF13

Scheduler Ping Pong

* Slides taken from Dan Appleman's DF13 Session on Asynchronous Apex (Presentation) (Slides)

Through self-aborting scheduled apex you can bounce between a scheduled job and a batch job, which restarts the scheduled apex upon finishing Async requests can be stored within a custom object to act as an async queue These records can store parameter information as a workaround for the restriction on parameters allowed within @future methods These records can also store scheduled time and as the queue is queried they will execute in order Technically the same can be done between scheduled apex and @future apex, but there are concurrency issues with this approach as we'll soon see

Queueable Apex

  • Queueable apex provides job ids upon being enqueued, similar to batch apex
  • Queueable apex classes can store complex data types within member variables
  • Chaining jobs is possible without any sort of workarounds
  • This makes the Scheduler Ping Pong method obsolete; especially because with the release of Queueable Apex Salesforce slowed down the scheduler method to enforce a 5 minute delay time between chains

Limitations

  • Count against shared async method call limit
  • 50 jobs added to the queue within a single transaction
  • Only one job can be enqueued by queued apex
  • Each queued apex call counts against a shared governor limit regarding async method calls within 24 hours
  • You can enqueue only 50 queued apex jobs in one transaction
  • Only one job can be added to the queue by queued apex
  • Originally when this was first released in W15 you could only chain once, but now that limit has been removed

Schmimitations

  • Each queued apex call counts against a shared governor limit regarding async method calls within 24 hours, but this is not a big deal as it's 250,000 or the number of user licenses in your organization multiplied by 200, whichever is greater
  • You can enqueue 50 queued apex jobs in one transaction, but really you should only have to do this once and just create Async Request records elsewhere
  • Only one job can be added to the queue by queued apex; again this is fine since we will just be chaining to run through our async request records

Chaining

Source

Queueable Apex Performing asynchronous logic within Salesforce Presented by Scott Covert / @scottbcovert