We took the simplicity of future methods and the power of Batch Apex and mixed them together to form Queueable Apex! It gives you a class structure that the platform serializes for you, a simplified interface without start and finish methods and even allows you to utilize more than just primitive arguments! It is called by a simple System.enqueueJob() method, which returns a job ID that you can monitor.

Queueable Apex allows you to submit jobs for asynchronous processing similar to future methods with the following additional benefits:

Queueable Syntax

To use Queueable Apex, simply implement the Queueable interface.

public class SomeClass implements Queueable { 
    public void execute(QueueableContext context) {
        // awesome code here
    }
}

A common scenario is to take some set of sObject records, execute some processing such as making a callout to an external REST endpoint or perform some calculations and then update them in the database asynchronously. Since @future methods are limited to primitive data types (or arrays or collections of primitives), queueable Apex is an ideal choice. The following code takes a collection of Account records, sets the parentId for each record, and then updates the records in the database.

public class UpdateParentAccount implements Queueable {
    
    private List<Account> accounts;
    private ID parent;
    
    public UpdateParentAccount(List<Account> records, ID id) {
        this.accounts = records;
        this.parent = id;
    }

    public void execute(QueueableContext context) {
        for (Account account : accounts) {
          account.parentId = parent;
          // perform other processing or callout
        }
        update accounts;
    }
}

To add this class as a job on the queue, execute the following code:

// find all accounts in ‘NY’
List<Account> accounts = [select id from account where billingstate = ‘NY’];
// find a specific parent account for all records
Id parentId = [select id from account where name = 'ACME Corp'][0].Id;

// instantiate a new instance of the Queueable class
UpdateParentAccount updateJob = new UpdateParentAccount(accounts, parentId);

// enqueue the job for processing
ID jobID = System.enqueueJob(updateJob);

After you submit your queueable class for execution, the job is added to the queue and will be processed when system resources become available. You can use the new job ID to monitor progress, either through the Apex Jobs page or programmatically by querying AsyncApexJob :

SELECT Id, Status, NumberOfErrors FROM AsyncApexJob WHERE Id = :jobID

Testing Queueable Apex

The following code sample shows how to test the execution of a queueable job in a test method. It looks very similar to Batch Apex testing. To ensure that the queueable process runs within the test method, the job is submitted to the queue between the Test.startTest  and Test.stopTest  block. The system executes all asynchronous processes started in a test method synchronously after the Test.stopTest  statement. Next, the test method verifies the results of the queueable job by querying the account records that the job updated.