PeopleCode | Putting your code into a ‘sleep’ state.
A. What’s So Good about Sleep Then?
Most programming code is intended to be run immediately to completion. However, in some cases you may want to temporarily halt all processing midway through the code. For instance, you may be waiting for a process to finish on the Process Scheduler or you may be polling the file server for a certain file to appear.
In these cases, it is highly inefficient to continue calling the same statement over and over again without halting your code between each call. If you fail to implement a sleep procedure, your code will clog up the server with unnecessary CPU time. For example, your function checking to see if a particular process has finished might end up being called a hundred times per second, which will only slow down (and irritate) other users.
A better option is to use a ‘sleep’ function and pause the code for a specific amount of time. This post considers two ways to temporarily put your PeopleCode to sleep.
B. Method 1 for Sleep: Using dbms_lock
SQLExec("exec dbms_lock.sleep (10)");
This method only works if your DBA has granted permission to the Oracle ‘dbms_lock’ package. In some cases, access to this package is blocked to all users due to the potentially destructive nature of some of the functions contained within the package. If you are lucky enough to have access, the above statement will halt your code for 10 seconds.
C. Method 2 for Sleep: Using Java class
Fortunately, the Java approach requires no knowledge of Java. Just include the above statement into your code, passing in the desired amount of ‘sleep time’. A value of ‘1000’ is equivalent to one second, so ‘10000’ equals ten seconds.
D. Sleepy Code
Here is an example of using the ‘dbms_lock’ sleep function within an actual block of code. This checks to see if a scheduled process has completed:
/* Wait here until process finished */ If &RQST.ProcessInstance <> 0 Then &finished = False; While Not &finished /* Wait 10 seconds */ SQLExec("exec dbms_lock.sleep (10)"); &processRec = CreateRecord(Record.PSPRCSRQST); &processRec.PRCSINSTANCE.Value = &RQST.ProcessInstance; &processRec.SelectbyKey(); If &processRec.RUNSTATUS.Value = "3" /* Error */ Or &processRec.RUNSTATUS.Value = "8" /* Cancelled */ Or &processRec.RUNSTATUS.Value = "9" /* Success */ Or &processRec.RUNSTATUS.Value = "10" Then /* Not Successful */ &finished = True; End-If; End-While; End-If;