Spring Batch with Cron

When we want a certain task to happen at a scheduled time we can make use of Cron Expressions. Spring has support for these expressions from the get-go. Lets take a look at how they work.

An expression is build up out of 6 parts. Like this: “* * * * * *”. Each * stands for the following:

  1. seconds with the values: 0-59
  2. minutes with the values: 0-59
  3. hours with the values: 0-23
  4. day of month with the values: 1-31
  5. month with the values: 0-11 or JAN-DEC
  6. day of week with the values: 0-7 or SUN-SAT

The * itself means always. There are a few more special symbols that can be used at the spot of a * in combination with 1 of the 6 values above:

  • – means from before – to after – ; example: 0-10
  • / means the value to the left of / times the value to the right of / ; example: 1/10 is every 10 days in a month if placed at the day of month spot; 0/5 is every 5 hours if placed at the hour spot
  • , is used to separate a list; example: MON,FRI,SAT

More information can be found here and here.

We are going to make a Spring Batch job repeat itself every hour. For that we need a Spring Batch job. In the example we use the batch job we created here, but any Spring Batch job will suffice.

First we create a class to launch our application:

@SpringBootApplication
public class ApplicationLauncher {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(ApplicationLauncher.class);
    }
}

After that we modify our BatchConfiguration.

@Configuration
@EnableBatchProcessing
class BatchConfiguration {

    private final JobBuilderFactory jobBuilderFactory;
    private final StepBuilderFactory stepBuilderFactory;
    private final DataSource dataSource;

    @Autowired
    BatchConfiguration(StepBuilderFactory stepBuilderFactory, 
    JobBuilderFactory jobBuilderFactory, DataSource dataSource) {
        this.stepBuilderFactory = stepBuilderFactory;
        this.jobBuilderFactory = jobBuilderFactory;
        this.dataSource = dataSource;
    }
}

We remove the main method, because that’s now in our ApplicationLauncher. We change @SpringBootApplication to @Configuration, because this functionality happens in our ApplicationLauncher now.

To start using the batch job with scheduling we need to disable the default configuration of running a job on application startup. We do this by adding the following line to application.properties:

spring.batch.job.enabled=false

Finally we can create the component to schedule our batch job.

@Component
@AllArgsConstructor
@EnableScheduling
public class ScheduledJobs {
    private final JobLauncher jobLauncher;

    private final Job ourJob;

    @Scheduled(cron="0 0 * * * *")
    public void startBatchJob() throws Exception{
        JobParameters jobParameters = new JobParametersBuilder()
        .addString("time", new Date().toString()).toJobParameters();

        jobLauncher.run(ourJob, jobParameters);
    }
}
  • @Component makes Spring able to auto detect the class.
  • @AllArgsConstructor create a constructor for all class level attributes. This is provided by Lombok.
  • @EnableScheduling makes Spring able to use @Scheduled inside the component.
  • @Scheduled makes Spring able to schedule the job. In this case we use a Cron Expression, but you can use other ways to schedule the job.
  • The JobLauncher is created by @EnableBatchProcessing on the BatchConfiguration class.

The Cron Expression in scheduled makes sure that every hour the job will run. Because we give the parameter of the time the job starts every job has an unique instance. If we did not give that parameter the job would only run once and after that say every hour that it has already been completed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s