Spring batch job scheduling with Spring TaskScheduler

In enterprise applications, you will need to execute spring batch jobs periodically on fixed schedule using some cron expression passed to Spring TaskScheduler. In this example, we will execute our example spring batch job using spring’s inbuilt scheduling capability.

Configure batch job scheduler

To configure, batch job scheduling is done in two steps:

  1. Enable scheduling with @EnableScheduling annotation.
  2. Create method annotated with @Scheduled and provide recurrence details using cron job. Add the job execution logic inside this method.
package com.howtodoinjava.demo;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@SpringBootApplication
@EnableScheduling
public class App
{
	@Autowired
	JobLauncher jobLauncher;
	
	@Autowired
	Job job;
	
	public static void main(String[] args) 
	{
		SpringApplication.run(App.class, args);
	}
	
	@Scheduled(cron = "0 */1 * * * ?")
    public void perform() throws Exception 
	{
		JobParameters params = new JobParametersBuilder()
				.addString("JobID", String.valueOf(System.currentTimeMillis()))
				.toJobParameters();
		jobLauncher.run(job, params);
	}
}

Above batch job will run every one minute after application is started.

To refer the job and task sourcecode, please read Spring batch Java config example.

Demo

Now if you run the application and verify logs, you will see that job is running every minute.

2018-07-04 15:57:00.073  INFO 7320 --- [pool-1-thread-1] o.s.b.c.l.support.SimpleJobLauncher      
: Job: [SimpleJob: [name=demoJob]] launched with the following parameters: [{JobID=1530700020003}]

2018-07-04 15:57:00.097  INFO 7320 --- [pool-1-thread-1] o.s.batch.core.job.SimpleStepHandler     
: Executing step: [stepOne]

MyTaskOne start..
MyTaskOne done..

2018-07-04 15:57:00.118  INFO 7320 --- [pool-1-thread-1] o.s.batch.core.job.SimpleStepHandler     
: Executing step: [stepTwo]

MyTaskTwo start..
MyTaskTwo done..

2018-07-04 15:57:00.125  INFO 7320 --- [pool-1-thread-1] o.s.b.c.l.support.SimpleJobLauncher      
: Job: [SimpleJob: [name=demoJob]] completed with the following parameters: [{JobID=1530700020003}] 
and the following status: [COMPLETED]

2018-07-04 15:58:00.007  INFO 7320 --- [pool-1-thread-1] o.s.b.c.l.support.SimpleJobLauncher      
: Job: [SimpleJob: [name=demoJob]] launched with the following parameters: [{JobID=1530700080002}]

2018-07-04 15:58:00.011  INFO 7320 --- [pool-1-thread-1] o.s.batch.core.job.SimpleStepHandler     
: Executing step: [stepOne]

MyTaskOne start..
MyTaskOne done..

2018-07-04 15:58:00.021  INFO 7320 --- [pool-1-thread-1] o.s.batch.core.job.SimpleStepHandler     
: Executing step: [stepTwo]

MyTaskTwo start..
MyTaskTwo done..

2018-07-04 15:58:00.029  INFO 7320 --- [pool-1-thread-1] o.s.b.c.l.support.SimpleJobLauncher      
: Job: [SimpleJob: [name=demoJob]] completed with the following parameters: [{JobID=1530700080002}] 
and the following status: [COMPLETED]

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

10 thoughts on “Spring batch job scheduling with Spring TaskScheduler”

  1. Hi Lockesh,

    I am having a standalone server where these spring batch jobs are scheduled via CRON jobs. Want to know how to achieve HA with another backup server. Incase of any Job failed in middle or server down – other server should be able to pick the job execution (from scratch is also fine). Any suggestions? Thanks in advance.

    Reply
  2. HI , I want to Schedule two different Job, what i need to do.
    lets say i have JobA i want to run at 10 PM and JobB i want to run 11 PM , Can you give me example how to do that using annotations

    Reply
  3. Hi Lokesh,
    I have a requirement where I need to read a particular column value from oracle database and pass that to a different database. Every day we are expecting to read millions of records from database. Please let me know if a simple Listener job would suffice or we need to something extra?

    Reply
    • You could instead use the API to set a flag in cache or somewhere and use that flag to proceed in the scheduled job.

      Reply
  4. Hi

    Thanks for Good Explanation.

    but i have some different requirement instated of passing fixed value to 1 minutes, i need to do it dynamic.
    @Scheduled(cron = “0 */x * * * ?”)

    can its possible to do?

    Thanks in advances

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.