Queueable Apex

Queueable Apex in Salesforce

The class which implements the Queueable interface are basically called as Queueable apex class. This interface enables you to add jobs to the queue and monitor them, which is an enhanced way of running your asynchronous Apex code compared to using future methods. The interface has only one method execute which takes the parameter of QueableContext.

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.

It allows you to submit jobs for asynchronous processing similar to future methods with with these additional benefits:

Non-primitive types: Your Queueable class can contain member variables of non-primitive data types, such as sObjects or custom Apex types. Those objects can be accessed when the job executes.

Monitoring:    When you submit your job by invoking the System.enqueueJob method, the method returns 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 job by starting a second job from a running job. Chaining jobs is useful if you need to do some sequential processing.

1
2
3
4
5
6
7
public class QueueableExample implements Queueable {
 
    public void execute(QueueableContext context) {
        Account acc = new Account(Name='Biswajeet');
        Insert acc;       
    }
}

            To add this class as a job on the queue, call this method:

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

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 monitor the status of your job programmatically by querying AsyncApexJob or through the user interface Setup || Monitoring || Apex Jobs.


To query information about your submitted job, perform a SOQL query on AsyncApexJob by filtering on the job ID that the System.enqueueJob method returns.

AsyncApexJob jobInfo = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID];

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 

  1. 1.You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction.
  2. 4.Can add only one job from an executing job with System.enqueueJob, means that only child job can exist for parent queueable job
  3. 5.The execution of a queued job counts once against the shared limit for asynchronous Apex method executions.


Queueable Apex asynchronously runs in background in its own thread. All jobs are added to a Queue and each job runs when the system resource are available. Also, we can monitor the progress of these jobs using the job id.

Comments

Popular posts from this blog

Security model

EVENTS IN Lightning Web Components

Lightning web Components.