Spring Batch Job Scheduling with @Scheduled

In enterprise applications, we will need to execute spring batch jobs periodically on a 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.

1. Scheduling a Batch Job with @Scheduled

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

Enable scheduling with @EnableScheduling annotation.

@SpringBootApplication
@EnableScheduling
public class App
{
   public static void main(String[] args)
  {
    SpringApplication.run(App.class, args);
  }
}

Create a method annotated with @Scheduled and provide recurrence details using the cron job. Add the job execution logic inside this method.

@Component
public class ScheduledJobBean
{
  @Autowired
  JobLauncher jobLauncher;
   
  @Autowired
  Job job;
   
  @Scheduled(cron = "0 */1 * * * ?")
  public void perform() throws Exception 
  {
    JobParameters params = new JobParametersBuilder()
        .addString("JobID", String.valueOf(System.currentTimeMillis()))
        .toJobParameters();

    jobLauncher.run(job, params);
  }
}

The above batch job will run every 1 minute after the application is started.

2. Stop Future Executions of Batch Job

Since we are scheduling with @Scheduled annotation, a bean processor ScheduledAnnotationBeanPostProcessor is registered automatically when the application is started. We can explicitly call its postProcessBeforeDestruction() method to destroy the scheduled bean.

@Autowired
ScheduledAnnotationBeanPostProcessor scheduledAnnotationBeanPostProcessor;

@Autowired
SpringBatchScheduler schedulerBean;

public void stopJobExecution() throws Exception {

	scheduledAnnotationBeanPostProcessor.postProcessBeforeDestruction(
	      schedulerBean, "ScheduledJobBean");
	    await().atLeast(10, SECONDS);
}

3. 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 the comments section.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.