11

If Apex Flex Queue is enabled in your organization, you can submit up to 100 batch jobs without getting an error.

The outcome of Database.executeBatch is as follows:

  • The batch job is placed in the Apex flex queue and its status is set to Holding as long as the number of jobs in the flex queue hasn’t reached the maximum of 100.
  • If the Apex flex queue has the maximum number of jobs, Database.executeBatch throws a LimitException and doesn’t add the job to the queue.

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

public class AsyncExecutionExample implements Queueable {
    public void execute(QueueableContext context) {
        Account a = new Account(Name='Acme',Phone='(415) 555-1212');
        insert a;        
    }
}

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

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

by both these options we can use add batch or job in queue. Please provide details.

3 Answers 3

9

One of the biggest differences, as you mention, is in the number that you can enqueue. Only 5 batches can be run at one time, whereas 100 Queueables can be run concurrently.

Another big difference is that a Queueable can be instantiated with object types beyond just primitives. For instance I can enqueue an operation that stores complex deferred state.

Unlike batches, Queueables cannot be scheduled.

3
  • 1
    Now somewhat dated. I should update this one.
    – Adrian Larson
    Jul 23, 2016 at 16:51
  • 2
    Queueables can be scheduled now AFAIK. May 3, 2018 at 8:44
  • @AdrianLarson what is the source of information about "100 Queueables can be run concurrently"? I was not able to find it in documentation about Apex Limits. Oct 26, 2020 at 9:28
5

The key difference can be found in the Queueable Apex documentation:

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.

So with Database.executeBatch you still get our Batch Apex done, and the Apex Flex Queue, while with enqueueJob you only get the Apex Flex Queue and the AsyncApexJob Id (and no batches).

-4

Unlike batches, Queueables cannot be scheduled.

This is NOT true, queueable CAN be scheduled.

I had situation where I needed to do a callout and then after that callout was done I needed to perform another callout to another service.

first you cannot do a callout without a @future in scheduled class, then another problem is if you are doing that trying to make callout after that with also @future annotated method is going to throw an error: First error: Future method cannot be called from a future or batch method:

Then you can remove future call from second callout but then if you are doing DML in same context of callout without @future you are also getting an error:

First error: You have uncommitted work pending. Please commit or rollback before calling out

Then to be able to fulfill this case scenario successfully I used:

public class calloutServices implements Queueable, Database.AllowsCallouts {

        public void execute(QueueableContext context){
            .....
       }
    }

Please notice that you also need to implement Database.AllowsCallouts otherwise you will get this error: Callout not allowed from this future method. Please enable callout by annotating the future method. eg: @Future(callout=true)

This is rather confusing message but I found out that Database.AllowsCallouts is the thing you need if you are using callouts in Queueable.

So to wrap this out: You can schedule Queueable and from Queueable you can call @future annotated methods and it will work like a charm. At least it worked for me :)

p.s. sorry for a longer post I wanted to share my experience so maybe someone else finds it usefull.

1
  • 3
    They meant scheduling as in; "Execute at 5PM every Monday" or "Execute once at 6:30PM tonight". Only Batchable allows that.
    – S Walsh
    Aug 11, 2015 at 23:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .