Salesforce Queueable Apex

1.Introduction

We are going to understand async apex with the Queueable interface and how to invoke external systems API with Queueable apex which was introduced in  Winter ’15 which superset of future methods. For Apex processes that run for a long time, such as extensive database operations or external Web service callouts, you can run them asynchronously by implementing the Queueable interface and adding a job to the Apex job queue.In this way, your asynchronous Apex job runs in the background in its own thread and doesn’t delay the execution of your main Apex logic. Each queued job runs when system resources become available. A benefit of using the Queueable interface methods is that some governor limits are higher than for synchronous Apex, such as heap size limits.Queueable jobs are similar to future methods in that they’re both queued for execution, but they provide you with these additional benefits.After you submit your queueable class for execution, the job is added to the queue and will be processed when system resources become available.

  1. Implement Apex Class with Queueable Interface which contains only one method execute. Implements DatabaseAllowsCallouts in case if you wanted to process the callouts from the Queueable apex.

     
  2. Execute the Queueable apex with  System.enqueueJob method which will return the job Id. Once you enqueue a new job, you get a job ID that you can actually monitor from the ApexJobs sections or by using AsyncApexJob object.

Here the sample code

trigger the job from the execute anonymous with below code

 

2.How Queueable different than @Feature

Queueable jobs are similar to future methods in that they’re both queued for execution, but they provide you with these additional benefits.

  •   Get your Job id  When you are calling a @future job does not give you the ID of the related job. But When you submit Queueable 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
  • Using non-primitive types: Your queueable class can contain member variables of non-primitive data types, such as sObjects or custom Apex types. But in case of @future, it only supports primitive data types
  • Chaining jobs: You can chain one job to another job by starting a second job from a running job. Chaining jobs are useful if you need to do some sequential processing

3.Queueable Job chaining 

One of the great features of  Queueable Apex is job chaining.To chain a job to another job, submit the second job from the execute()method of your queueable class. You can add only one job from an executing job, which means that only one child job can exist for each parent job. Queueable Apex is job chaining has considerations.

  • No Test Classes Rule: Job chaining is not allowed in Apex test classes.
  • You can add only one job from an executing job, which means that only one child job can exist for each parent job

 

4.Queueable Apex has limits 

Queueable apex has following limits.

1.You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction.
2.Limits.getQueueableJobs() helps to check how many queueable jobs have been added in one transaction
3. No limit is enforced on the depth of chained jobs, which means that you can chain one job to another job and repeat this.
4.Can add only one job from an executing job with System.enqueueJob, means that only child job can exist for parent queueable job
5.The execution of a queued job counts once against the shared limit for asynchronous Apex method executions.