Queueable Apex in salesforce is similar to Future Method in salesforce i.e both are queued for execution however Queueable Apex provide additional benefits.
Each queued job runs when system resources become available to it. The future method always requires id's as parameter, Objects are not allowed. Objects are not allowed because there is a possibility that when future method runs i.e when resources are available to it user might have changed the data inside database which can lead to processing on wrong data where as queueable can also contains non primitives data types and it fetches data from database again before making any update thus avoiding wrong data getting inserted inside date base.
Note: Queueable is an interface where as Future is a method. If an Apex transaction rolls back, any queueable jobs queued for execution by the transaction are not processed.
This interface enables us to add jobs to the queue and monitor them. Using the interface is an enhanced way of running our asynchronous Apex code compared to using Future Method. Apex processes that run for a long time and includes extensive database operations or external web service callouts can be run asynchronously by implementing the Queueable interface and adding a job to the Apex job queue. In this way, our asynchronous Apex job runs in the background in its own thread and doesn’t delay the execution of our 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.
You can visit our article on future method for more details: Future method in salesforce
Benefits of using Queueable Apex:
1) When we submit our 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. We can use this ID to identify our job and monitor its progress, either through the Salesforce user interface in the Apex Jobs page, or programmatically by querying your record from AsyncApexJob.
2) Our queueable class can contain member variables of non-primitive data types, such as sObjects or custom Apex types.
3) We can chain one job to another job by starting a second job from a running job. Chaining jobs is useful if our process depends on another process to have run first.
Let us understand the structure of queueable apex with below example,
public class QueueableApexExample implements Queueable {
public void execute(QueueableContext context) {
Account a = new Account(Name='Test Account Using Queueable Apex' );
insert a;
}
}
Note: Queueable interface has only 1 method i.e execute method.
To add the above class as a job on the queue,
ID jobID = System.enqueueJob(new QueueableApexExample ());
To query information about above submitted job, we can perform a SOQL query on AsyncApexJob object as shown below,
AsyncApexJob jobInfo = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID];
No comments:
Post a Comment