QUEUABLE APEX:
What is Queueable Apex?
It’s an extension of the Future Methods.
It uses the Queueable interface which is an enhanced way of
running your asynchronous Apex code compared to using future methods. The
limitations which Future methods have, through Queueable Apex, we can overcome
them.
How does Queueable Apex differ from Future methods?
Queueable Apex is similar to future
methods in that they’re both queued for execution, but they provide us these
additional benefits.
• When you queue a Queueable Apex,
you get a job ID, that can be used to trace it easily, which is not possible in
case of future methods.
• You can use non-primitive datatypes
in Queueable Apex, like objects and sObjects, which is not
possible in case of future methods, because it supports only primitive data
types as params.
• You can chain jobs, by calling another
starting a second job from a running job, which is not possible in case of
future methods, because we can’t call another future method from a future
context.
When will we use future methods instead of Queueable?
You use future methods instead of
queueable is when your functionality is sometimes executed synchronously, and
sometimes asynchronously. It’s much easier to refactor a method in this manner
than converting to a queueable class. This is handy when you discover that part
of your existing code needs to be moved to async execution. You can simply
create a similar future method that wraps your synchronous method.
Can you write a sample Queueable Job?
Create a class, implement the Queueable
interface, and override the execute method.
|
|
public
class QueueableApexExample
implements Queueable { public
void execute(QueueableContext
context) { //some process } } |
What is QueueableContext?
It is an interface that is implemented
internally by Apex, and contains the job ID. Once you queue the Queueable Job,
the Job Id will be returned to you, by apex through QueueableContext’s
getJobId() method.
How can I queue Queueable Job(QueueableApexExample)?
Using System.enqueueJob Method.
|
ID jobID = System.enqueueJob(new QueueableApexExample()); |
How can I use this Job Id to trace the Job?
Just perform a SOQL query on AsyncApexJob
by filtering on the job ID.
|
AsyncApexJob jobInfo = [SELECT Status,NumberOfErrors FROM
AsyncApexJob WHERE Id=:jobID]; |
I have 200 records to be processed using Queueable Apex, How Can
I divide the execution Context for every 100 records?
Similar to future jobs, queueable jobs
don’t process batches, so you can’t divide the execution Context. It will
process all 200 records, in a single execution Context.
Can I do callouts from a Queueable Job?
Yes, you have to implement the
Database.AllowsCallouts interface to do callouts from Queueable Jobs.
How Chaining works in Queueable Apex?
Chaining allows us to customize the
execution order of a set of jobs in a process.
let’s say there are N jobs, A, B, C and
so on. We have a process in which A has to run first and gives output to B and
then B runs and gives output to C and so on. So, instead of calling the
individual Queueable Jobs explicitly, we can link the calls, logically within
Queueable Jobs.
|
|
public
class A
implements Queueable { public
void execute(QueueableContext
context) { // Chain this job to next job by submitting the next job System.enqueueJob(new B());
} } public
class B
implements Queueable { public
void execute(QueueableContext
context) { // Chain this job to next job by submitting the next job System.enqueueJob(new C());
} } public
class C
implements Queueable { public
void execute(QueueableContext
context) { // Chain this job to next job by submitting the next job System.enqueueJob(new D());
} } |
Can I chain a job that has implemented allowsCalloutsfrom a Job
that doesn’t have?
Yes, callouts are also allowed in chained
queueable jobs.
How many numbers of jobs, can be chained at a time?
You can add only one job from an
executing job, which means that only one child job can exist for each parent
job.
Since, no limit is enforced on the depth
of chained jobs, you can chain one job to another. You can repeat this process
with each new child job to link it to a new child job.
How many numbers of jobs, I can queue using System.enqueueJob() at a time?
You can add up to 50 jobs to the queue
with System.enqueueJob in a single transaction in Synchronous apex. In
asynchronous transactions, you can add only one job to the queue.
Can I call Queueable from a batch?
Yes, But you’re limited to just one System.enqueueJob call per execute in the Database.Batchable class.
Salesforce has imposed this limitation to prevent explosive execution.
If I have written more than one System.enqueueJob call, what will
happen?
System will throw LimitException
stating “Too many queueable
jobs added to the queue: N”.
How can we handle this error, without using a try-catch?
We can check whether the current count of
queuable jobs has exceeded the limit or not, if not, queue them.
Limits.getQueueableJobs() returns the
currently queued jobs count.
Limits.getLimitQueueableJobs() returns
the limit.
|
|
If(Limits.getQueueableJobs() <
Limits.getLimitQueueableJobs()) { //System.enqueueJob() } |
I have a use case to call more than one Queueable Jobs from a
Batch apex, how can I achieve it?
Since we can’t call more than one
Queueable Job from each execution Context, We can go for scheduling the
Queueable Jobs.
The approach is we need to first check
how many queueable jobs are added in the queue in the current transaction by
making use of Limits class. If the number has reached the limit, then call a
schedulable class and enqueue the queueable class from the execute method of a
schedulable class.
how to test a Queueable Job?
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.
Also, The ID of a queueable Apex job
isn’t returned in test context — System.enqueueJob returns null in a running
test.
how to test Chaining?
You can’t chain queueable jobs in an Apex
test. So you have to write separate test cases for each chained queueable job.
Also, while chaining the jobs, add a check of Test.isRunningTest() before
calling the enqueueJob.
Test.isRunningTest() Returns true if the
currently executing code was called by code contained in a test method, false
otherwise
|
|
public
class A
implements Queueable { public
void execute(QueueableContext
context) { // Test to make sure that Unit test are not running before
chaining // the call. We don’t want to chain another job during a
Unit test run. if(!Test.isRunningTest()){ System.enqueueJob(new B()); } } } |
Queueable apex and future method both run
sdfs asynchronously but queueable apex provides these additional benefits.
1.
Getting
Id of your Job : ID JobId =system.enqueueJob(qe);
2. Chaining Job : In Queueable Apex we can chain one job to another.
3. Using Non-Primitive Data 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.
public class AccountQueueableExample
implements Queueable
{
public List<Account> accList ;
public AccountQueueableExample(List<Account> accList){
this.accList = accList ;
}
public void execute(QueueableContext
context) {
for(Account acc :accList){
// Update the Account Name
acc.Name = acc.Name + 'learnfrenzy';
}
update accList;
}
}
List<Account> accList = [Select Id ,
Name from Account ];
ID jobID = System.enqueueJob(new AccountQueueableExample(accList));
System.debug('jobID'+jobID);
No comments:
Post a Comment