The last post was a comparison between batch apex and @future apex. There are some advantage and disadvantage with both of them. When batch and @future needs to meet in between – queueable comes for rescue.

There are a few things that @future did not provide:

  1. @future requires the arguments be primitives, which means reconstructing a structure once the method is called.
  2. Difficult access to job ID. The executeBatch method returns a jobID, while calling an @future job does not give you the ID of the related job.
  3. No chaining – Chaining of batch apex is not allowed.
Q – Why we go for queueable Apex?
A – When the job such as extensive database operations or external Web service callouts is long running and is running asynchronously and you want to monitor the job status. It can be done via queueable apex and adding a job to the ApexJob 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.

Queueable jobs are similar to future methods in that they’re both queued for execution, but they provide you with these additional benefits.

Getting an ID for your job: When you submit your 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.
This ID helps you 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.

Using non-primitive types: Your queueable class can contain member variables of non-primitive data types, such as sObjects or custom Apex types.

Chaining Jobs: In queueable you can also chain Jobs from a running job. It is very helpful if some processing needs to be done which depends on the last results.

Example:
This example is an implementation of the Queueable interface. The execute method in this example inserts a new account.

public class QueueableExample implements Queueable {

    public void execute(QueueableContext context) {
        Account acc = new Account(Name=’Test Account Neeraj’);
        Insert acc;     
    }
}

To add this class as a job on the queue, call this method:
ID jobID = System.enqueueJob(new QueueableExample());

After you submit your queueable class for execution, the job is added to the queue and will be processed when system resources become available. We can monitor the status of our job programmatically by querying AsyncApexJob or through the user interface Setup –> Monitor –> Jobs –> Apex Jobs.

Test class for queueable apex:

@isTest
public class AsyncExecutionExampleTest {
    static testmethod void testAccount() {
     
        Test.startTest();     
        System.enqueueJob(new QueueableExample());
        Test.stopTest();

        Account acct = [SELECT Name FROM Account WHERE Name=’Test Account Neeraj’ LIMIT 1];
        System.assertEquals(‘Test Account Neeraj’, acct.Name);
    }
}

Few things can be noted here:

  • The execution of a queued job counts against the shared limit for asynchronous Apex method executions.
  • We can add up to 50 jobs to the queue with System.enqueueJob in a single transaction.
  • Use Limits.getQueueableJobs() to check how many queueable jobs have been added in one transaction.
  • No limit is enforced on the depth of chained jobs.
  • We can add only one job from an executing job with System.enqueueJob, that means only child job can exist for parent queueable job.
10 thoughts on “Queueable Apex – The middle way”
  1. fantastic submit, very informative. I wonder why the other experts of this sector don’t realize this. You must continue your writing. I am confident, you have a great readers’ base already!

  2. Great post. I was checking continuously this blog and I’m impressed! Extremely useful info particularly the last part 🙂 I care for such information a lot. I was seeking this certain info for a long time. Thank you and best of luck.

  3. Very great post. I just stumbled upon your weblog and wished to say that I’ve truly enjoyed browsing your weblog posts. After all I’ll be subscribing in your rss feed and I’m hoping you write once more soon!

  4. Hello, i read your blog from time to time and i own a similar one and i was just wondering if you get a lot of spam feedback? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me crazy so any support is very much appreciated.

  5. There are some interesting points in time in this article but I don?t know if I see all of them center to heart. There is some validity but I will take hold opinion until I look into it further. Good article , thanks and we want more! Added to FeedBurner as well

Leave a Reply

Your email address will not be published. Required fields are marked *