๐คฉ Spring Batch ์์คํ ์ ๋ง๋ค์ด๋ด ์๋ค !
SpringBoot ํ๋ก์ ํธ๋ IntelliJ๋ฅผ ์ฌ์ฉํ์ค ๊ฒฝ์ฐ, ํด ๋ด์์ ์์ฑ์ด ๊ฐ๋ฅํฉ๋๋ค. ์ฌ์ฉํ์ง ์์ผ์ค ๊ฒฝ์ฐ, spring.io๋ผ๋ ๊ณต์ ์ฌ์ดํธ๋ฅผ ์ด์ฉํ์๋ฉด ํธ๋ฆฌํ๊ฒ ์์ฑํ์ค ์ ์์ต๋๋ค.
ํด๋น ํฌ์คํธ๋ ์กฐ์กธ๋๋์ jojoldu.tistory.com/325?category=902551 ํฌ์คํธ๋ฅผ ๊ธฐ๋ฐํ ํฌ์คํธ์ ๋๋ค.
๋จผ์ , ์ ๋ง ์ ๋ง ์ ๋ง * 100 ๊ฐ๋จํ ๋ฐฐ์น ์์คํ ์ ๋ง๋ค์ด๋ณด๊ฒ ์ต๋๋ค.
์ผ๋จ BatchApplication.java์ @EnableBatchProcessing ์ด๋ ธํ ์ด์ ์ ์ถ๊ฐํ์ฌ Spring Batch๊ฐ ๋์ํ ์ ์๋ ์ดํ๋ฆฌ์ผ์ด์ ์ผ๋ก ๋ง๋ค์ด์ฃผ์ด์ผ ํฉ๋๋ค.
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// Spring Batch๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ๊ผญ ํ์ํ ์ด๋
ธํ
์ด์
@EnableBatchProcessing
@SpringBootApplication
public class BatchApplication {
public static void main(String[] args) {
SpringApplication.run(BatchApplication.class, args);
}
}
๊ทธ ํ, SimpleJobConfiguration.java๋ผ๋ ํ์ผ์ ์์ฑํ์ฌ Job๊ณผ Step์ ์์ฑํฉ๋๋ค.
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.Job;
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;
@Slf4j
@RequiredArgsConstructor // ์์ฑ์ DI๋ฅผ ์ํ lombok ์ด๋
ธํ
์ด์
@Configuration // SpringBatch์ ๋ชจ๋ job์ ํด๋น ์ด๋
ธํ
์ด์
์ด ํ์
public class SimpleJobConfiguration {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job simpleJob() {
return jobBuilderFactory.get("simpleJob") // Job ์ด๋ฆ์ Builder๋ฅผ ํตํด ์์ฑ
.start(simpleStep1()) //Step
.build();
}
@Bean
public Step simpleStep1() {
return stepBuilderFactory.get("simpleStep1") // Step ์ด๋ฆ์ Builder๋ฅผ ํตํด ์์ฑ
// step ์์์ ์คํ๋ ๊ธฐ๋ฅ๋ค์ tasklet ๋ฐฉ์์ ํตํด ์ง์
.tasklet((contribution, chunkContext) -> {
log.info(">>>>> ์ด๊ณณ์ simpleStep1 ์
๋๋ค.");
return RepeatStatus.FINISHED;
})
.build();
}
}
์ด๋ ๊ฒ ํ์ฌ, ์ดํ๋ฆฌ์ผ์ด์ ์ ์คํํ๊ฒ ๋๋ฉด ๋ค์๊ณผ ๊ฐ์ด Job๊ณผ ๊ทธ์ ์ํ Step์ ์คํํ๊ณ ์ดํ๋ฆฌ์ผ์ด์ ์ด ShutDown๋๋ ๊ฒ์ ๋ณด์ค ์ ์์ต๋๋ค. (์๋์ ์น์ด๋ผ๋ฉด ShutDown์ด ์๋๋๋ฐ ๋ง์ด์ฃ !)
์ด๋ ๊ฒ ํ๋์ Batch Application์ด ๋ง๋ค์ด์ก์ต๋๋ค..!
๐ค ๊ทผ๋ฐ, Batch Application์ ์ด๋ ๊ฒ ๊ฐ๋จํ์ง ์์ํ ๋ฐ์...?
๋ค, ๊ทธ๋์ ์ด์ DB๋ ์ฐ๊ฒฐํด๋ณด๊ณ .. Chunk๋ฐฉ์๋ ์ฌ์ฉํด ์ ์์ ๋ณด๋ค ์ข ๋ ๋ณต์กํ Batch Application์ ๋ง๋ค์ด๋ณผ๊ฒ์!
๊ทธ ์ ์ ๋จผ์ Meta Data๋ผ๋ ๊ฒ์ ์ข ๋ณผ๊น์...?
๐ฉ๐ป๐ซ Spring Batch MetaData
Spring์์ ์ ๊ณตํ๋ Meta Data Schema(docs.spring.io/spring-batch/docs/3.0.x/reference/html/metaDataSchema.html)๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
์ด MetaData๋ ๋ค์๊ณผ ๊ฐ์ ์ ๋ณด๋ค์ ๋ด๊ณ ์๋๋ฐ์.
- ์ด์ ์ ์คํํ Job์ ์ ๋ณด
- ์ต๊ทผ ์คํจํ Batch Parameter์ ์ ๋ณด
- ์ฑ๊ณตํ Job์ ์ ๋ณด
- ๋ค์ ์คํํ๋ค๋ฉด ์ด๋์ ๋ถํฐ ์์ํ๋ฉด ๋๋์ง์ ๋ํ ์ ๋ณด
- ์ด๋ค Job์ ์ด๋ค Step๋ค์ด ์์๊ณ , Step๋ค ์ค ์ฑ๊ณตํ Step๊ณผ ์คํจํ Step๋ค์ ์ด๋ค๊ฒ๋ค์ด ์๋์ง
๋ฑ๋ฑ...
ํ ๋ง๋๋ก Batch System์ด Run๋ ๋์ ๋ชจ๋ ์ ๋ณด๋ค์ ๋ด๊ณ ์๋ค๊ณ ์๊ฐํ์๋ฉด ๋ฉ๋๋ค...!
๋ค์๊ณผ ๊ฐ์ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง ํ ์ด๋ธ์ด ํ๋ก์ ํธ์ ์ฐ๊ฒฐ๋ DB์ ์กด์ฌํด์ผ์ง๋ง, SpringBatch๊ฐ ๋์ํ ์ ์๋๋ฐ์!
๊ธฐ๋ณธ์ ์ผ๋ก H2 DB๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ, ํด๋น ํ ์ด๋ธ์ Boot๊ฐ ์คํ๋ ๋ ์๋์ผ๋ก ์์ฑํด์ฃผ์ง๋ง,
MySQL์ด๋ Oracle๊ณผ ๊ฐ์ DB๋ฅผ ์ฌ์ฉํ ๋๋ ๊ฐ๋ฐ์๊ฐ ์ง์ ์์ฑํด์ผํฉ๋๋ค.
๋คํ์ค๋ฝ๊ฒ๋ ๋ฐ์ ์ด๋ฏธ์ง ๊ฒฝ๋ก์ ์ด ํ ์ด๋ธ๋ค์ ๋ํ SQLํ์ผ๋ค์ด ์์ฑ๋์ด ์์ผ๋, ๊ฐ์ DB์ ์ฝ์ ํด์ฃผ์๋ฉด ๋ฉ๋๋ค.
๐คฉ MySQL๊ณผ ์ฐ๋ํ๊ธฐ
๊ฐ ์ด์์ฒด์ ์ ๋ง์ถ์ด MySQL์ ์ค์นํ๊ณ ๊ณ์ ์ ๋ณด๋ฅผ ์ค์ ํฉ๋๋ค. (์น์์ ๋ง์ด ๋์์์ด ์๋ตํ๊ฒ ์ต๋๋ค.)
์ค์ ํ, ํ๋ก์ ํธ์ application.yml์ ๋ค์๊ณผ ๊ฐ์ด ์ถ๊ฐ๋ฅผ ํด์ค๋๋ค.
ํด๋น ์ฝ๋์ ์๋ฏธ๋ h2 / mysql ํ๊ฒฝ์ active๋ฅผ ํตํด ์กฐ์ํ ์ ์๋๋ก ํ๋ ๊ฒ์ ๋๋ค.
์ฐ๋ฆฌ๋ ๋ง์ ๋ฐ์ดํฐ๋ค์ ์ ์ฅํ ํ์๊ฐ ์๊ธฐ์ active์ local์ mysql๋ก ๋ฐ๊พธ์ด์ค๋๋ค.
spring:
profiles:
active: local
spring:
profiles: local
datasource:
hikari:
jdbc-url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username:
password:
driver-class-name: org.h2.Driver
spring:
profiles: mysql
datasource:
hikari:
jdbc-url: jdbc:mysql://localhost:3306/spring_batch
username: root
password:
driver-class-name: com.mysql.jdbc.Driver
'Spring > SpirngBatch' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring Batch (4) - MetaData (0) | 2021.05.30 |
---|---|
Spring Batch (2) (0) | 2021.05.06 |
SpringBatch (0) | 2021.03.24 |