0

I need to implement the time delay of 10 mins between two HTTP requests to remoter service within the webservice class. Webservice class is called from Apex-batch

Apex Batch --> Calls Webservice class - Make 1st HTTP request to remote service - introduce 10 mins delay - Make 2nd HTTP request.

How to introduce 5 mins delay ? How to avoid script errors ?

2
  • 1
    Schedulable Interface
    – Adrian Larson
    Commented Dec 1, 2015 at 23:44
  • The Appleman Asynchronous Apex pattern provides a useful platform for an extensible scheduling mechanism that can work for this use case and all your future use cases. Documented in his book Advanced Apex and explained (an earlier version) here on YouTube from Dreamforce: youtube.com/watch?v=ZqqiIg_mynY
    – cropredy
    Commented Dec 2, 2015 at 1:49

3 Answers 3

1

There are no direct ways to implement delays in Synchronous execution.

The best approach to implement your solution will be to Schedule jobs to run your batch classes.

  1. Batch class makes the first API call and submits a "Schedule Job" to execute after a predefined duration.

  2. Schedule job invokes the Schedular class and initiates the Second API call.

  3. You can keep scheduling jobs from your next execution if you want to repeat this process through out the day.

Note: Store the Cron job id when you schedule the job in the first step and delete it after the 2nd steps completion. As there is a limit on Number of total schedule jobs at a time(Active or Inactive)

Here is the link for Schedular execution : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

Let me know if you nee more help and I can provide the code snippet to do the same.

Cheers

2

You can call a class that uses a schedulable interface with a delay this way:

==Schedulable Class==

global class scheduledCallout implements Schedulable, Database.AllowsCallouts {
    global void execute(SchedulableContext SC) {
       scheduledCallout.myCallout();
   }

   @future(callout=true) 
   public static void myCallout() {
      //Do Callout here
   }
}

== Calling it===

private void callInMinutes(integer minutes) {
  DateTime in10Mins = datetime.now().addMinutes(10);

  String CRON_EXP = '0 ' + in10Mins.minute() + ' ' + in10Mins.hour() + ' ' + in10Mins.day() + ' ' + in10Mins.month() + ' ? ' + dateTime.year();

 system.schedule('Callout', CRON_EXP, scheduledCallout);
}
0

I faced the same issue wherein I have to add delaytimer between callouts and did the following: (basically you make a fake callout between callouts as follows)

Within your batch class, add a method as follows:

public static void sleep(Integer sleepSeconds) {
    Long startTS = System.currentTimeMillis();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('http://1.cuzillion.com/bin/resource.cgi?sleep=' + (sleepSeconds/1000));
    req.setMethod('GET');
    Http http = new Http();
    HTTPResponse res = http.send(req);
    req.setTimeOut(300);
}  

then in your execute method, after the callout is done add this line:

if(!test.isRunningTest()){sleep(3);}

Make sure to add 'http://1.cuzillion.com' to your remote site settings

2
  • I have included above code in the execute method. In my batch class, i put sleep time of 60 seconds.. getting following error message.. System.CalloutException: Read timed out I want a time delay of 5 to 10 mins.. any solution please ?
    – Razu
    Commented Dec 2, 2015 at 16:38
  • make your timeout 120000
    – cropredy
    Commented Dec 2, 2015 at 17:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .