spring batch: hello spring batch
This commit is contained in:
@@ -14,6 +14,10 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-batch'
|
||||
implementation 'mysql:mysql-connector-java:8.0.29'
|
||||
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
annotationProcessor 'org.projectlombok:lombok'
|
||||
|
||||
runtimeOnly 'com.h2database:h2'
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package io.springbatch.basic.hello;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
|
||||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class DBJobConfiguration {
|
||||
|
||||
private final JobBuilderFactory jobBuilderFactory;
|
||||
private final StepBuilderFactory stepBuilderFactory;
|
||||
|
||||
@Bean
|
||||
public Job job() {
|
||||
return jobBuilderFactory.get("job")
|
||||
.start(step1())
|
||||
.next(step2())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step step1() {
|
||||
return stepBuilderFactory.get("step1")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("step1 was executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step step2() {
|
||||
return stepBuilderFactory.get("step2")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("step2 was executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package io.springbatch.basic.hello;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
|
||||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class HelloJobConfiguration {
|
||||
|
||||
private final JobBuilderFactory jobBuilderFactory;
|
||||
private final StepBuilderFactory stepBuilderFactory;
|
||||
|
||||
@Bean
|
||||
public Job helloJob() {
|
||||
return jobBuilderFactory.get("helloJob")
|
||||
.start(helloStep1())
|
||||
.next(helloStep2())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step helloStep1() {
|
||||
return stepBuilderFactory.get("helloStep1")
|
||||
.tasklet(new Tasklet() {
|
||||
@Override
|
||||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
|
||||
|
||||
System.out.println(" ======================= ");
|
||||
System.out.println(" >> Hello Spring Batch!! ");
|
||||
System.out.println(" ======================= ");
|
||||
|
||||
return RepeatStatus.FINISHED; // tasklet 은 무한반복하므로 FINISHED 또는 null 일 경우 종료
|
||||
}
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step helloStep2() {
|
||||
return stepBuilderFactory.get("helloStep2")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
|
||||
System.out.println(" ======================= ");
|
||||
System.out.println(" >> Step2 was execute!!! ");
|
||||
System.out.println(" ======================= ");
|
||||
|
||||
return RepeatStatus.FINISHED; // tasklet 은 무한반복하므로 FINISHED 또는 null 일 경우 종료
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1 +1,35 @@
|
||||
spring:
|
||||
profiles:
|
||||
active: local
|
||||
|
||||
---
|
||||
|
||||
spring:
|
||||
config:
|
||||
activate:
|
||||
on-profile: local
|
||||
|
||||
datasource:
|
||||
hikari:
|
||||
jdbc-url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
|
||||
username: sa
|
||||
password:
|
||||
driver-class-name: org.h2.Driver
|
||||
|
||||
---
|
||||
|
||||
spring:
|
||||
config:
|
||||
activate:
|
||||
on-profile: mysql
|
||||
|
||||
datasource:
|
||||
hikari:
|
||||
jdbc-url: jdbc:mysql://localhost:3306/springbatch?useUnicode=true&characterEncoding=utf8
|
||||
username: root
|
||||
password: 1234
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
|
||||
batch:
|
||||
jdbc:
|
||||
initialize-schema: always
|
||||
Reference in New Issue
Block a user