Spring ’15 Feature: Apex Flex Queue

The Spring ’15 release is approaching quickly! As with every Salesforce release, it comes with a plethora of new features. Over the next few weeks, I am going to outline what I find to be some of the cooler features.

Apex Flex Queue

One of the nicest features of Spring ’15 in my mind is Apex Flex Queue. From the release notes:

Submit up to 100 batch jobs simultaneously and actively manage the order of the queued jobs to control which batch jobs are processed first. This enhancement provides you more flexibility in managing your batch jobs.

Previously, you could submit only up to five batch jobs simultaneously. The Apex flex queue enables you to submit up to 100 additional batch jobs for execution. Any jobs that are submitted for execution are in holding status and are placed in the Apex flex queue. Up to 100 batch jobs can be in the holding status. When system resources become available, the system picks up jobs from the top of the Apex flex queue and moves them to the batch job queue. The system can process up to five queued or active jobs simultaneously. The status of these moved jobs changes from Holding to Queued. Queued jobs get executed when the system is ready to process new jobs.

Administrators can modify the order of jobs that are held in the Apex flex queue to control when they get processed by the system. For example, administrators can move a batch job up to the first position in the holding queue so that it’s the first job that gets processed when the system fetches the next held job from the flex queue. Without administrator intervention, jobs are processed first-in first-out—in the order in which they’re submitted. To monitor and reorder held batch jobs in the Salesforce user interface, from Setup click Jobs | Apex Flex Queue.

My Observations

This is awesome! Being able to queue up 100 jobs rather than 5 is a huge difference (that is a 2000% increase)! The important thing to note about this change is the addition of the Holding status. From the release notes:

The AsyncApexJob object, which represents a batch job, has a new Status field value of Holding. This new status indicates that the job is placed in the flex queue and is waiting to be processed when system resources become available.

This aspect of the change is crucial. It doesn’t matter what version of Apex your code runs against, it needs to be able to support this new status. Specifically, this affects all queries against the AsyncApexJob. Take for instance (prior to Spring ’15):

List<AsyncApexJob> existingJobs = [
	SELECT Id, Status, CreatedBy.Name, CreatedDate 
	FROM AsyncApexJob 
	WHERE (Status = 'Queued' or Status = 'Processing' or Status = 'Preparing') 
	AND ApexClass.Name = 'TestBatchClass' 
];

This query would have found all existing batch jobs for the TestBatchClass prior to Spring ’15. However, due to the addition of the new Holding status, this no longer finds all of the existing jobs. This query needs to be modified slightly to:

List<AsyncApexJob> existingJobs = [
	SELECT Id, Status, CreatedBy.Name, CreatedDate 
	FROM AsyncApexJob 
	WHERE (Status = 'Queued' or Status = 'Processing' or Status = 'Preparing' or Status = 'Holding') 
	AND ApexClass.Name = 'TestBatchClass' 
];

As mentioned before, different than most changes to Apex, this is a data change. It requires code running on previous versions to take into account the proper Status values.

Enjoy and feel free to test it out on your own Spring ’15 Pre-Release Org!

One Response to “Spring ’15 Feature: Apex Flex Queue”

  1. May 11, 2016 at 11:56 am #

    Jesse,

    Have you been able to do any tests with scheduling jobs? I’m getting an error when one of my tests runs:

    “You’ve exceeded the limit of 100 jobs in the flex queue for org 00Dxxxxxxx. Wait for some of your batch jobs to finish before adding more. To monitor and reorder jobs, use the Apex Flex Queue page in Setup.”

    Any idea how I can get around that?

Leave a Comment