Can we callout and chain a Queueable class?

I’ll jump right into some code:

public class ChainQueue implements Queueable, Database.AllowsCallouts {
    public void execute(QueueableContext context) {
        if(QueueUtil.hasNext()) {
            MyWSUtil.makeCallout(QueueUtil.next());
        }
        if(QueueUtil.hasNext()) {
            System.enqueueJob(this);
        }
    }
}

Assuming MyWSUtil.makeCallout is a public static function that performs a callout from a queue item (being a custom object that queues up pending integrations), and QueueUtil has static methods that handle getting pending queue items and/or determining if there are more records to process, this code will fail.

If no callout happens, the chain ends and everything is okay. If a callout occurs, and we then attempt to chain, we receive an error about the “maximum callout depth has been reached.” Implementing Database.AllowsCallouts appears to prevent System.enqueueJob from being able to chain, and not including Database.AllowsCallouts causes a “Callout not allowed from this future method” when making the callout.

Is there some combination of attributes that will allow us to chain and queue? Using callouts from queueable would move us off a very unstable batch process that has to simulate parallelism by switching between a number of integrations (much like a multitasking OS does), rather than intuitively spawning off batches (which are limited to 5, and we have up to 14 integration threads that need to run, and more are expected later).

If this isn’t possible today, are the plans to support this in the future? Any documentation or advice would be helpful.

Answer

Spring ’17 provides:

Make Web Service Callouts from Chained Queueable Jobs

Apex now allows web service callouts from chained queueable jobs. Previously, a queueable job could make a web service callout, but additional chained jobs that made callouts would throw exceptions.

See Also:

Idea Exchange: Allow Callouts from Chained Queueable Apex Jobs

Attribution
Source : Link , Question Author : sfdcfox , Answer Author : Community

Leave a Comment