When creating SAP batch jobs to run a program, most of the scheduling can be accomplished by setting a periodic time for when the job is executed. For example, execute the job every 10min, 30min, or 1 hour. But what if you needed more control of when a batch job was executed?
At one of my clients they had a requirement to run a batch job every 5 minutes. Sometimes the batch job would take longer than 5 minutes to complete. Thus, the same program would start in a new batch job before the previous batch job had time to finish. The same program would collide with itself and cause issues.
It seems that SAP’s built-in job scheduler does not have a standard way of preventing overlapping job executions. I was hoping to find a checkbox that would deny an execution of a job if it finds the same active job running, but that would be too easy. However, SAP does have some different options in its toolbelt to satisfy this requirement. The approach that I will demonstrate will use an event-driven approach to execute a SAP batch job.
First I will detail the High level overview of what needs to happen. Then, after you understand the big picture, then I will go into more detail about how to code and configure this scenario.
High level Overview
Basically 2 SAP batch jobs are needed:
1) SAP Job #1
- This job will execute every 5min or so depending on your requirment and execute a simple custom program that will check the status of Job #2.
- If the status of job Job #2 is not running, then the custom program will raise an event
- If the status of Job #2 is running, then the custom program will do nothing
2) SAP Job #2
- This job is setup to start only when an event is raised
- If an event is Raised from Job #1 then Job #2 will start. Job #2 will contain the actual programs that you need to execute.
Sound easy enough?
Now let’s get to business and see what configuration and coding we need to do.
1) First let’s define an Event.
Transaction SM64 – I defined my event as Z_JOB_START_XXXXX
2) Next, Create simple Custom program ZJOB_CREATE_EVENT to Raise the Event. This program will basically check if the specified job is currently running. If it is not running then Raise an event which was created in the previous step above. In my case, I will raise event ‘Z_JOB_START_XXXXX’. Create a variant here with the Event Name(Z_JOB_START_XXXXX) and the Job #2 name (ZJOB_XXXXX).
3) For Job #1. Basically set this job as Periodic Time driven job to run every 5 minutes or so with the custom program ZJOB_CREATE_EVENT. Also make sure you include the variant with the Job #2 name and Event.
4) For Job #2. Define this job as an Event Driven batch job and define the actual program (I used test program ZSPTEST) that needs to be executed. Specify the event Z_JOB_START_XXXXX that this job will be waiting for. Also checkbox ‘Periodic job’ to schedule another event job.
That’s it…Now the batch job will only execute if the same batch job is not running.
For testing purposes, I will demonstrate by manually execute program ZJOB_CREATE_EVENT to show that how this all works.
The event ZJOB_START_XXXXX has been raised by program ZJOB_CREATE_EVENT. Now you can see the ZJOB_XXXXX is Active/Running
During the time the job is active, I attempted to execute program ZJOB_CREATE_EVENT again. This time, the program knows that the job is still active and, therefore, it does not raise the event. The only time the event will be raised again is once the job is finished .
This is only one scenario in where an event driven batch job is needed. Now you can see how you can utilize an event driven batch job to put some control around how some programs are executed.