Today, I show you how to create a Spring Batch Scheduler with Spring Boot Example. This post introduces very clear to help you understand job scheduling with Spring Boot. Moreover, we also mention the cron expression and some cron patterns then you can declare your own cron pattern by yourself. Let’s dive into
Spring Batch Scheduler with Spring Boot Example
Project Setup
Tools and framework that we use:
Spring Boot 2.2.2 RELEASE
Spring Batch 4.2.1
Maven 3.6
Java 8
Project Directory
Our project will have a structure like below
Project Dependencies
We’re using the dependencies like below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
... <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <java.version>8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency> </dependencies> ... |
Cron expression
Cron expression is represented by six fields:
1 |
second, minute, hour, day of month, month, day(s) of week |
Or you may refer to the graphically cron expression below:
From the above cron expression, we give you some example patterns:
1 2 3 4 5 6 7 |
"0 0 * * * *" = the top of every hour of every day. "*/10 * * * * *" = every ten seconds. "0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day. "0 0 8,10 * * *" = 8 and 10 o'clock of every day. "0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day. "0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays "0 0 0 25 12 ?" = every Christmas Day at midnight |
Configure batch job scheduler with spring boot
Configuring batch job scheduler very easy with spring boot, we just do two steps:
- Enable scheduling with
@EnableScheduling
annotation. - Create a method annotated with
@Scheduled
and a cron job pattern.
Here is the detail implementation of the above steps in MySpringBootApplication
class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
@SpringBootApplication( exclude = {DataSourceAutoConfiguration.class}) @EnableScheduling public class MySpringBootApplication { @Autowired JobLauncher jobLauncher; @Autowired Job job; public static void main(String[] args) { SpringApplication.run(MySpringBootApplication.class, args); } @Scheduled(cron = "*/10 * * * * *?") public void perform() throws Exception { System.out.println("-------Job is starting ----------"); JobParameters params = new JobParametersBuilder() .addString("JobID", String.valueOf(System.currentTimeMillis())) .toJobParameters(); jobLauncher.run(job, params); System.out.println("-------Job ends ----------"); } } |
Dig deeper: Above, we implement the job scheduler with cron patternĀ */10 * * * * *?
. That means it runs every ten seconds.
DEMO
To show you how Spring batch job scheduler works, we reuse the source code from the post Spring Batch Hello World Example with Spring Boot
MySpringBootApplication
class like above. It’s quite easy, isn’t it?
To demo our Spring Batch Scheduler with Spring Boot Example, we run the below command in the console.
1 |
mvn spring-boot:run |
Note: You must be in your project directory before run that command.
While running that command, you will see the output like below
Let’s have a look at the screenshot, our program is reading employee information from CSV
file. Every ten minutes, it repeats reading again. So this program works like what we want, congratulation!
If you’re not sure about the implementation, you can download the complete source code of Spring Batch Scheduler with Spring Boot Example at the bottom of this post. That’s all about Spring Batch Scheduler with Spring Boot Example.
If you find this post useful, don't hesitate to share it with your friends or other people for growing knowledge together and is an effort to contribute us.
References
Spring Boot Tutorial for Beginners
Spring Batch Frameworks
Spring Scheduling
Spring cron expression