+ Start a Discussion
boBNunnyboBNunny 

Workaround for Sleep/Pause/Wait?

We have a webservice that sometimes gets inundated with requests from an APEX trigger we have.  So, we wrote a .NET batch program to do the requests via API through SFDC with a 45 second delay between requests.  We'd like to make that a Batch APEX, however, I can't find any way to make the APEX code pause between submittals.  I tried a loop checking on the time, but that causes 200K worth of script statements which exceeds governor limits too.

 

Does anyone have any ideas?

 

Thanx!

ScoobieScoobie

I'd be very surprised if SalesForce implemented a wait or sleep method as this would hold up transactions and wouldn't be very fair on a multi-tenanted SaaS platform.

 

When you say the .NET batch program fires through SFDC, what do you mean? Is the inundated webservice a SFDC webservice or another host and you are using SFDC as a proxy?

boBNunnyboBNunny

It doesn't need to be transactional to where it's holding up a transaction.  They can limit it to Batch APEX.  Then it can be checked to make sure it's not in the middle of a transaction.  But we really need it because calling web services in batches can overwhelm them and that makes the application the weakest link and not truly scalable.

boBNunnyboBNunny

BTW, when I say .NET through SFDC, I mean that we resubmit some items for a custom Lead Assignment by setting the flag for it to call the Lead Assignment web service.  That service gets flooded and can't handle hundreds of requests in a 1 minute span.

Force.comForce.com

I am stucked up in similar kind of problem. Did you succedded in resolving this ? Please share with me. I want to have some

knid of wait statement between the two submittals Apex batch.

 

Please help, its urgent

 

Thanks,

Pragati

 

 

 

Swagat.TusharSwagat.Tushar
Any workaround/solution for this.

Thanks,
Swagat
Martha_SenetaMartha_Seneta
Try something like this:

Long startingTime = System.now().getTime(); // Num milliseconds since Jan 1 1970
Integer delayInMilliseconds = 1000; // One-second delay
while (System.now().getTime() - startingTime < delayInMilliseconds)  {
            // Do nothing until desired delay has passed
 }
boBNunnyboBNunny
This may work now because CPU time is checked and not script statements, but it wouldn’t work before. Thanx!
SkyTreasureSkyTreasure
Call this function and pass number of seconds as the parameter. -

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);
    req.setMethod('GET');
    Http http = new Http();
    HTTPResponse res = http.send(req);
    Long duration = System.currentTimeMillis() - startTS;
    System.debug('Duration: ' + duration + 'ms');
}

Via - http://stackoverflow.com/a/16817451/1084806
Sales LogistixSales Logistix
Well, there is another way to avoid the governor limit on number of statements if you have a lot of Tasks or Cases in your system. These two objects are archived after a year's time, and that means queries can take longer.  Create a list called "count" (or whatever), and in the middle of your loop, use this
   count = [SELECT id FROM TASK LIMIT 50000 ALL ROWS ];
   count.clear();
Obviously, this wastes SOQL queries and you'll need to change the limit parameter depending on whatever else you have in memory. But in busy times of the day, that select may take a second or more each time it's executed.
OF COURSE this is a wasteful practice. SFDC has stated pretty clearly they'll never implement a sleep function, and if you use this technique sparingly it's not that sinful.
boBNunnyboBNunny
Actually, this has been pretty much solved by some of Salesforce’s latest changes to limits. Script statements are not limited anymore, but CPUTime. So a FOR loop delay waiting that many seconds is indeed possible if needed. Thanx!
Shefali Bansal 1Shefali Bansal 1

How Can I achieve this in Salesforce:

try {
        Thread.sleep(1 * 1000); // Wait for a second
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    }