Queueable Interface for Asynchronous Apex Processes

blog_Queueable-Interface_320pxSalesforce has added the Queueable Interface in the Winter’15 release, which sounds like an alternative to the Future methods. Both Queueable Interface methods and Future methods are Asynchronous Apex Processes that add jobs to the job queue and each job runs when system resources become available, so it doesn’t delay the execution of the main Apex logic. They also share a benefit of having some higher governor limits than synchronous Apex, such as heap size limits (12 MB),  number of SOQL queries issued (200) and Maximum CPU time on the Salesforce servers (60k ms). But the Queueable interface methods are a step up from the future methods because they also come with these additional benefits (according to Salesforce release notes):

  • Getting an ID for your job: When you submit your job by invoking the System.enqueueJob method, the method returns the ID of the new job. This ID corresponds to the ID of the AsyncApexJob record. You can use this ID to identify your job and monitor its progress, either through the Salesforce user interface in the Apex Jobs page, or programmatically by querying your record from AsyncApexJob.
  • Chaining jobs: You can chain one job to another by starting a second job from a running job. Chaining jobs is useful if you need to do some processing that depends on another process to have run first.

Below is an example of how to implement the Queueable interface.

public class AsyncExecutionExample implements Queueable {
     public void execute(QueueableContext context) {
          Account a = new Account(Name='Acme',Phone='(415) 555-1212');
          insert a; 
     }
}

You can add the class to a job queue by calling this method.

ID jobID = System.enqueueJob(new AsyncExecutionExample());

If you have a second class that also implements the Queueable interface and you want it to run after the class above is done, you can chain them together like this:

public class AsyncExecutionExample implements Queueable {
     public void execute(QueueableContext context) {
          Account a = new Account(Name='Acme',Phone='(415) 555-1212');
          insert a; 

         // Chain this job to next job by submitting the next job
         System.enqueueJob(new SecondJob());
     }
}

And here is list of limits from Queueable Apex vs Future methods

Queueble Future Methods
The execution of a queued job counts once against the shared limit for asynchronous Apex method executions. Same
You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction. No more than 50 method calls per Apex invocation.
The maximum stack depth for chained jobs is two, which means that you can have a maximum of two jobs in the chain. A future method can’t invoke another future method.
When chaining jobs, you can add only one job from an executing job with System.enqueueJob. Methods with the future annotation cannot take sObjects or objects as arguments.

 

These are just the limits from the Salesforce release notes. I haven’t had a chance to play much with the Queueable interface, since it just came out in the Winter’15, so I haven’t discovered some other limits yet. When I find some new issues with the Queueable interface, I will post them in another blog post. Stay tune!