TODO: Batch process introduction
TODO: Spring batch architecture introduction
Project Initialization
Spring batch can be initialized with Spring intializr.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-test</artifactId>
<scope>test</scope>
</dependency>
One of the essential dependencies of spring batch is Spring data-jpa, as it needs to store and maintain the metadata information of the Job and step execution history.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
The main SpringBootApplication class may look similar as shown below. Run it and ensure that the context is loading properly without any issues.
package com.kesavan.springbatchexp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbatchexpApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbatchexpApplication.class, args);
}
}
Step-1 : Adding a scheduler to the application.
For our understanding purpose, let us use a Scheduler to launch the jobs. In this step, let’s just introduce a scheduler that runs every second.
The following annotations can be used to configure a scheduler.
- @EnableScheduling – to your SpringBootApplication class
- @Scheduled – to the method in your scheduler class
- A scheduler class needs to be created – Annotated with @Component annotation
package com.kesavan.springbatchexp.job;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class DataSummarizerJobScheduler {
@Scheduled(fixedRate = 1000)
public void launchJob() {
System.out.println("Scheduler runs now : "+System.currentTimeMillis());
}
}
Leave a Reply