Queueable Apex

Queueable Jobs

Queueable Jobs are asynchronous Apex processes queued for execution
but they provide jobId when you submit to monitor job. You can use AsyncApexJob table for more information about the job.
You can use non-primitive types.
you can chain job

Queueable Interface

This interface enables you to add jobs in to the queue.
Due to Asynchronous in nature some governer limit are higher than synchronous apex.
These jobs run in their own thread when system resources become available.

you have to override the public void execute(QueueableContext context) method

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

Monitor

  1. To monitor or stop the execution of the batch Apex job, from Setup, enterApex Jobs in the Quick Find box, then select Apex Jobs.
  2. ID jobID = System.enqueueJob(object); use the id returned by the method and table AsyncApexJob
ID jobID = System.enqueueJob(new AsyncExecutionExample());
AsyncApexJob jobInfo =[SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID];

Chaining jobs

To run a job after some processing is done by first job.
Both jobs runs in parallel. It just add the job into the queue.

public class AsyncExecutionExample implements Queueable {
public void execute(QueueableContext context) {
// Your processing logic here

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

Limits

  • The execution of a queued job counts once against the shared limit for asynchronous Apex method executions.
  • You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction. In asynchronous transactions (for example, from a batch Apex job), you can add only one job to the queue with System.enqueueJob.

Leave a Reply

Scroll to top