๐ค ์ฌ์ฉํ๋ ์ด์
Spring Batch๋ ๋๋์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐ ์ฌ์ฉ๋๋ ์คํ ์์ค ํ๋ ์์ํฌ์ ๋๋ค. Spring Batch๋ ๋งค์ฐ ์ ์ฐํ๋ฉฐ ์ผ๊ด ์ฒ๋ฆฌ (Batch Processing) ์์ ์ ์ฒ๋ฆฌํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ์ด๋ฅผ ์ํด Spring Batch๋ ์์ ์ ๋จ๊ณ๋ณ๋ก ๋ถํ ํ์ฌ ๊ฐ ๋จ๊ณ๋ฅผ ์คํํ๊ณ ํธ๋์ญ์ ๊ด๋ฆฌ์ ์์ธ ์ฒ๋ฆฌ๋ฅผ ์ ๊ณตํฉ๋๋ค.
์ํ Spring Batch ํ๋ก์ ํธ๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
1. Spring Boot ํ๋ก์ ํธ ์์ฑ
Spring Boot CLI๋ฅผ ์ฌ์ฉํ์ฌ ์ Spring Boot ํ๋ก์ ํธ๋ฅผ ์์ฑํฉ๋๋ค.
$ spring init --dependencies=web,batch my-batch-project
2. ์์กด์ฑ ์ถ๊ฐ
build.gradle ํ์ผ์ Spring Batch ๋ฐ ๊ด๋ จ ์์กด์ฑ์ ์ถ๊ฐํฉ๋๋ค.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-batch'
implementation 'org.springframework.batch:spring-batch-core'
implementation 'org.springframework.batch:spring-batch-infrastructure'
testImplementation 'org.springframework.batch:spring-batch-test'
}
3. Job๊ณผ Step ์์ฑ
Spring Batch ์์ (Job)์ ํ๋ ์ด์์ ๋จ๊ณ (Step)๋ก ๊ตฌ์ฑ๋ฉ๋๋ค. Job๊ณผ Step์ ๋ง๋ค๊ธฐ ์ํด ๋ค์๊ณผ ๊ฐ์ด Java ํด๋์ค๋ฅผ ๋ง๋ญ๋๋ค.
@Configuration
public class BatchConfiguration {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.tasklet((stepContribution, chunkContext) -> {
System.out.println("Hello, World!");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Job job(Step step1) {
return jobBuilderFactory.get("job")
.incrementer(new RunIdIncrementer())
.start(step1)
.build();
}
}
์ด ์ฝ๋๋ ์์ (Job)๊ณผ ์ฒซ ๋ฒ์งธ ๋จ๊ณ (Step)์ ์ ์ํฉ๋๋ค. ์ฒซ ๋ฒ์งธ ๋จ๊ณ์์๋ "Hello, World!" ๋ฉ์์ง๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค.
4. ๋ฐฐ์น ์คํ
๋ฐฐ์น๋ฅผ ์คํํ๋ ค๋ฉด SpringApplication.run ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ Spring Boot ์ ํ๋ฆฌ์ผ์ด์ ์ ์์ํฉ๋๋ค.
@SpringBootApplication
@EnableBatchProcessing
public class MyBatchProjectApplication {
public static void main(String[] args) {
SpringApplication.run(MyBatchProjectApplication.class, args);
}
}
์ด์ ํ๋ก์ ํธ๋ฅผ ๋น๋ํ๊ณ ์คํํฉ๋๋ค.
$ ./gradlew bootRun
์ด๊ฒ์ผ๋ก ๊ฐ๋จํ Spring Batch ์ํ ํ๋ก์ ํธ๋ฅผ ๋ง๋ค์์ต๋๋ค! ์ด์ Job๊ณผ Step์ ์ถ๊ฐํ๊ณ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์ธ ์ ์์ต๋๋ค.
'BACK-END > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring Batch ๊ธฐ๋ณธ ๊ตฌ์กฐ (0) | 2023.04.10 |
---|---|
REST API ๋์์ธ ๊ฐ์ด๋ (1) | 2022.10.27 |
[Spring Batch] Scheduled cron Expression ํฌ๋ก ํํ์ (3) | 2022.03.16 |
[Spring Boot] Failed to determine a suitable driver class ์๋ฌ (0) | 2021.12.14 |