function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kendall BuchananKendall Buchanan 

Asynchronous tasks are often very slow

Our org has one asynchronous task, triggered by a @future. It's simply an HTTP request to an external webservice. Usually the @future processes within a second or two. Often, however, it can take 30, 60, 120 seconds. We've seen it take as long as *three or four minutes* to process!

Is this normal? Is there any way to overcome this?

Here's the relevant code (simplified):
 
public class SyncClass {
  public static void requestSync() {
    String endpoint = System.Label.Sponsors_App_Endpoint;

    HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    Http http = new Http();

    req.setEndpoint(endpoint + '/my-endpoint');
    req.setMethod('POST');
    req.setBody('');

    try {
      res = http.send(req);
    } catch(System.CalloutException e) {
      System.debug('Callout error: '+ e);
      System.debug(res.toString());
    }
  }
  
  @future(callout=true)
  public static void asyncRequestSync() {
  	SyncClass.requestSync();
  }
}

 
Balaji Chowdary GarapatiBalaji Chowdary Garapati

As per the salesforce documentation, there is no way to control the order of execution of async calls as salesforce picks them up in order they were put through, async calls will be processed when ever there are enough resources to utilize on the saleforce.com server side. Please refer to the below document:
http://www.salesforce.com/us/developer/docs/async_processing/salesforce_async_processing.pdf

Time for execution depends on lot more factors which can be found in the reference link.

Hope that content helps:

Thanks,
Balaji
Kendall BuchananKendall Buchanan
Okay, that's disappointing. Is there no way to achieve quicker outbound web requests?
Balaji Chowdary GarapatiBalaji Chowdary Garapati
It depends on where you are using the call outs!! If you were using that in the triggers there is no way around, else if you were using that from a vfpage controller you can remove the @future annotation and handle it in a different way( in the sense that the call out shuoldnt occur in a method where DML operation is being performed, instead the DML can be moved prior to or later to  call out happens in a seperate method using onComplete functionality from visualforce page tags).