Difference between Database.executeBatch and Queueable Apex

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.

Answer

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.

Attribution
Source : Link , Question Author : jayvardhan singh , Answer Author : Adrian Larson

Leave a Comment