What is the purpose/use of Queueable Apex?
Queueable interface.What processes in Apex can be run asynchronously using Queueable interface ?
Queueable interface and adding a job to the Apex job queue.Queueable interface methods is that some governor limits are higher than for synchronous Apex, such as heap size limits.What benefits do Queueable jobs have over future methods ?
Queueable jobs have these additions benefits over future methods :
System.enqueueJob method, the method returns the ID of the new job. This ID corresponds to the ID of the AsyncApexJob record. Use this ID to identify and monitor your job either through the Salesforce UI (Apex Jobs page), or programmatically by querying your record from AsyncApexJob.What is maximum stack depth of chained Queueable Jobs ?
You can set a maximum stack depth of chain Queueable jobs overriding the default limit of five in Developer and Trial Edition organizations.
What happens to variables declared as transient in Queueable Apex ?
Variables that are declared transient are ignored by serialization and deserialization and the value is set to null in Queueable Apex.
Which method is used to add the queueable apex job to the apex queue ?
The System.enqueueJob(queueable) method is used to add the job to the queue.
To add a queueable class as a job on the queue, call this method: ID JobID = System.enqueueJob(new AsyncExecutionExample());
Which method and interface must the Queueable Class implement ?
executemethod.public void execute(QueueableContext context)public class AsyncExecutionExample implements Queueable {
public void execute(QueueableContext context) {
Account a = new Account(Name='Acme',Phone='(415) 555-1212');
insert a;
}
}How to monitor the status of a submitted queueable job ?
Apex Jobs in the Quick Find box, then selecting Apex Jobs.System.enqueueJob method returns.AsyncApexJob jobInfo = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id =: jobID];
Do queueable jobs process batches ?
Similar to future jobs, queueable jobs don’t process batches, and so the number of processed batches and the number of total batches are always zero.
How to add Queueable Job with a Specified Minimum Delay ?
System.enqueueJob(queueable,delay) method to add queuable jobs to the asynchronous execution queue with a specified minimum delay (0 - 10 minutes).What are some situations in which adding a minimum delay would be advisable ?
It would be beneficial to adjust the timing before the queueable job is run
Integer delayInMinutes = 5; ID jobID = System.enqueueJob(new MyQueueableClass(), delayInMinutes);
Setting a default org-wide delay for Queueable Jobs in the org ?
System.enqueueJob(queueable,delay) method ignores any org-wide enqueue delay setting.What are the ways to define org-wide delay for queueable jobs ?
Apex Settings, and then enter a value (1-600 seconds) for Default minimum enqueue delay (in seconds) for queueable jobs that do not have a delay parameter.ApexSettings in the Metadata API Developer Guide.How to add a queueable job with a specified stack depth ?
System.enqueueJob(queueable,asyncOptions) method where you can specify the maximum stack depth and the minimum queue delay in the asyncOptions parameter.System.AsyncInfo class properties contain the current and maximum stack depths and the minimum queueable delay.System.AsyncInfo class has methods to help you determine if maximum stack depth is set in your Queueable request and to get the stack depths and queue delay for your queueables that are currently running.These are the methods in the System.AsyncInfo class
Example of using stack depth to terminate a chained job and prevent it from reaching the daily maximum number of asynchronous Apex method executions ?
// Fibonacci
public class FibonacciDepthQueueable implements Queueable {
private long nMinus1, nMinus2;
public static void calculateFibonacciTo(integer depth) {
AsyncOptions asyncOptions = new AsyncOptions();
asyncOptions.MaximumQueueableStackDepth = depth;
System.enqueueJob(new FibonacciDepthQueueable(null, null), asyncOptions);
}
private FibonacciDepthQueueable(long nMinus1param, long nMinus2param) {
nMinus1 = nMinus1param;
nMinus2 = nMinus2param;
}
public void execute(QueueableContext context) {
integer depth = AsyncInfo.getCurrentQueueableStackDepth();
// Calculate step
long fibonacciSequenceStep;
switch on (depth) {
when 1, 2 {
fibonacciSequenceStep = 1;
}
when else {
fibonacciSequenceStep = nMinus1 + nMinus2;
}
}
System.debug('depth: ' + depth + ' fibonacciSequenceStep: ' + fibonacciSequenceStep);
if(System.AsyncInfo.hasMaxStackDepth() &&
AsyncInfo.getCurrentQueueableStackDepth() >=
AsyncInfo.getMaximumQueueableStackDepth()) {
// Reached maximum stack depth
Fibonacci\_\_c result = new Fibonacci\_\_c(
Depth\_\_c = depth,
Result = fibonacciSequenceStep
);
insert result;
} else {
System.enqueueJob(new FibonacciDepthQueueable(fibonacciSequenceStep, nMinus1));
}
}
}How to test queueable jobs ?
A queueable job is an asynchronous process.
To ensure that this 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 method.
@isTest
public class AsyncExecutionExampleTest {
@isTest
static void test1() {
// startTest/stopTest block to force async processes
// to run in the test.
Test.startTest();
System.enqueueJob(new AsyncExecutionExample());
Test.stopTest();
// Validate that the job has run
// by verifying that the record was created.
// This query returns only the account created in test context by the
// Queueable class method.
Account acct = [SELECT Name,Phone FROM Account WHERE Name='Acme' LIMIT 1];
System.assertNotEquals(null, acct);
System.assertEquals('(415) 555-1212', acct.Phone);
}
}How to chain queueable jobs ?
execute() method of your queueable class.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());
}
}How to allow HTTP and web service callouts from queueable jobs ?
Database.AllowsCallouts marker interface.What are some queueable apex limits ?
System.enqueueJob in a single transaction.System.enqueueJob.Limits.getQueueableJobs().System.enqueueJob, you can add only one job from an executing job. Only one child job can exist for each parent qeueuable job. Starting multiple child jobs from the same queueable job isn’t supported.What is the Transaction Finalizers feature ?
System.Finalizer interface, to asynchronous Apex jobs that use the Queueable framework.Before Transaction Finalizers what were the only two ways for asynchronous job failures ?
AsyncApexJob using a SOQL query and re-enqueue the job if it fails.BatchApexErrorEvents when a batch Apex method encounters an unhandled exception.What is the main purpose of Transaction Finalizers ?
With transaction finalizers, you can attach a post action sequence to a Queueable Job take relevant actions based on the job execution result.
How many times can a queueable job that failed due to an unhandled exception be re-enqueued ?
Can a finalizers be implemented as inner classes ?