API/Apex for modifying Batch Apex Flex Queue

UPDATE: Winter ’16 made huge improvements since I wrote this question after Spring ’15. Most notably, the various methods on the FlexQueue object (and related methods on Test object)

My question as it was originally written:

I looked forward to the Apex Flex Queue that became GA with Spring ’15. However, in actually trying to use it, I’m baffled as to how this is useful in its current implementation:

  • How do I reorder the queue via API or Apex? It looks like the only option is via the Setup UI — and I can’t prioritize any ‘Holding’ jobs without aborting another one in the Queued state. See: http://screencast.com/t/uOAfg4Z73
  • Again, referencing the screen shot above, this UI is not helpful to anyone without the Job ID being visible — Our application enqueues background workers via a common class (SupremeQueryBatchableWrapper) so they are indistinguishable from one another
  • Why does it throw a LimitException if you exceed the 100 slots? This type of exception can’t be rescued in Apex so instead, you need to have code similar to (see related question: Apex Batch job limits not clear Queued vs Scheduled) this:

Integer enqueuedJobs = [SELECT COUNT() FROM AsyncApexJob WHERE JobType='BatchApex' AND Status IN ('Processing','Preparing','Queued','Holding')];

if(enqueuedJobs >= 5){
    throw new TooManyBatchApexJobsException();

Am I overlooking something here? I can probably deal with the third bullet point (since we already have that code written). But the first two are head-scratchers


Winter ’16 allows you to programmatically manipulate the queue. The above complaints are no longer relevant.

The following methods on FlexQueue are relevant:

moveAfterJob(jobToMoveId, jobInQueueId)

moveBeforeJob(jobToMoveId, jobInQueueId)



Additionally, if your Apex class is Version 35 or higher, a System.AsyncException is thrown instead of System.LimitException when you exceed 100 jobs in the Holding queue.

Source : Link , Question Author : daveespo , Answer Author : daveespo

Leave a Comment