Spring Batch: Limit How Frequent a Batch Job Runs

Spring Batch: Limit How Frequent a Batch Job Runs

Often in designing batch jobs you want to limit the frequency that a job can run. For example, if you want a batch job to run once a day, you can use @Scheduled annotation to run the batch job once a day. This is enough if you only run one instance of the batch program and there are no other ways to launch the job.

But if you have manual ways to launch the same job, or you are you using kubernetes to run multiple instances of your batch program, the batch job may be attempted to run multiple times in one day, even if you intend to only run once a day.

The fix

JobParameters are the key to limiting the number of times a job is ran in a given time.

The JobParameters in Spring Batch limit Spring Batch from launching the same job instance twice. (Caveat - if the job FAILS it can be restarted with the same job parameters, but if it is SUCCESS it will throw an exception if you attempt to restart it);

So if you want a job to run only ONCE_A_DAY then create the job parameters DD/MM/YY. The job will be unique across different days but with be the same on the same day, allowing it to only run once a day.

Code Example

public JobParameters buildFrequencyLimitedJobParameters() {
    Calendar cal = Calendar.getInstance().now();
    
    // Creates a unique value on any given day
    // To limit to once an hour use "dd/mmyy HH" and so on....
    SimpleDateFormat format = new SimpleDate("dd/mm/yyyy");
    String value = format.format(cal.getTime());

    // Any batch job using these job parameters are now frequency limited, 
    // since the job params are created using a time interval
    return new JobParametersBuilders()
    .addString("TIME_INTERVAL_LIMIT",value);
}