Wednesday, November 28, 2012

Sleep in PeopleCode

You may have found business requirements that need to wait for a given time before performing the next action. For instance, in order to check whether the execution of a batch process has finished or not, you may want to query the process monitor tables to verify the process status every 15 seconds.

Unfortunately, there is no function in PeopleCode that actually sleeps processing during a period of time. Of course you can build a loop until the target time arrives, but that approach consumes CPU while waiting, so it is not resource effective.

In other languages, the sleep function simply puts the process off until the time has passed and the process is wake up. This is a much more efficient approach.

Fortunately, we can benefit from PeopleCode integration with Java to use java.lang.Thread sleep method. A sample PeopleCode would be as follows:

&sleepSeconds = 15;
GetJavaClass("java.lang.Thread").sleep(&sleepSeconds * 1000);

No java class installation is needed, as this class is included in PeopleTools, so this code should work without any further installation adjustment.