- ๋ชฉ์ฐจ
- ๋ค์ด๊ฐ๋ฉฐ
- 1 ์คํ๋ง ๋ฐฐ์น Hello World
- 2 ๋ฐฐ์น ๊ตฌ์กฐ
- 3 ๋ฐฐ์น๋ฅผ ์คํ์ํค๋ ๋ค์ํ ๋ฐฉ๋ฒ (feat. @EnableBatchProcessing)
- ์ฐธ๊ณ
์ด๋ฒ ๊ธ๋ถํฐ๋ ๋ณธ๊ฒฉ์ ์ผ๋ก ์คํ๋ง ๋ฐฐ์น์ ๋ํด์ ์์๋ณธ๋ค.
๋ง์ ์คํ๋ง ๋ฐฐ์น ๊ด๋ จ ๊ธ์์ ์ฃผ๋ก ๋ฐฐ์น์์ ์ฌ์ฉ๋๋ ๋๋ฉ์ธ์ ๋ํด์ ๋ฐ๋ก ์ค๋ช ํ๋ค.
ํ์์ ๊ฒฝ์ฐ๋ ์ฒ์์ ์ด๋ถ๋ถ์ด ์กฐ๊ธ ๋นํน์ค๋ฌ์ ๋ค...
์ด๋ฒ ๊ธ์ ๋ฐฐ์น์ ๋๋ฉ์ธ์ ๊ดํด์ ๊น๊ฒ ๋ค๋ฃจ์ง ์๊ณ , ์ฐ์ Hello World ์์ค์ Job์ ํ๋ ๋ง๋ค๊ณ ์คํํด๋ณธ๋ค.
์คํํ๋ ๊ณผ์ ์ ํตํด ์คํ๋ง ๋ฐฐ์น์ ์ ์ฒด์ ์ธ ๊ตฌ์กฐ๋ฅผ ์ดํด๋ณด๊ณ , ๋ฐฐ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ๋์์ํค๋ ์ฌ๋ฌ๊ฐ์ง ๋ฐฉ๋ฒ์ ๋ํด์ ์์๋ณธ๋ค.
๊ฐ๋จํ ์คํ๋ง ๋ฐฐ์น ํ๋ก์ ํธ๋ฅผ ์์ฑํ๊ณ Hello World ์์ค์ Job์ ๊ตฌ์ฑ ๋ฐ ์คํํด๋ณธ๋ค.
์๋์ ๊ฐ์ด Spring Initializer๋ฅผ ํตํด Spring Boot ํ๋ก์ ํธ๋ฅผ ๋ง๋ ๋ค.
์์กด์ฑ์ ์๋์ ๊ฐ์ด ์ค์ ํด์ค๋ค.
์ด 4๊ฐ์ ์์กด์ฑ์ ๋ฃ์ด์ค ๊ฒ์ ๋ณผ ์ ์๋ค.
- Spring Batch
- Spring Boot ๊ธฐ๋ฐ์ Spring Batch๋ฅผ ์คํํ ๋ ํ์ํ ์์กด์ฑ.
- H2, MySQL
- Profile์ ๋ฐ๋ผ ๋ฐฐ์น ๋๋ฉ์ธ ๋ฉํ ๋ฐ์ดํฐ ํ ์ด๋ธ์ ์ ์ฅํ๊ธฐ ์ํ DB ๊ด๋ จ ์์กด์ฑ.
- Lombok
์ 4๊ฐ์ง์ ์์กด์ฑ์ ์ถ๊ฐํ๊ณ ํ๋ก์ ํธ๋ฅผ ์์ฑํด์ค๋ค.
ํ๋ก์ ํธ๋ฅผ ์์ฑํ ํ ์ 4๊ฐ์ง ์์กด์ฑ์ ์ค์ ํด์ค๋ ๋ฌด๋ฐฉํ๋ค.
ํ๋ก์ ํธ๊ฐ ์ ์์ฑ๋์๋ค๋ฉด, ์ด๋ฒ์ Hello World ์์ค์ Job์ ๊ตฌ์ฑํด๋ณธ๋ค.
HelloWorldJobConfiguration.java
@RequiredArgsConstructor
@Configuration
public class HelloWorldJobConfiguration {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job helloWorldJob() {
return jobBuilderFactory.get("helloWorldJob")
.start(helloWorldStep1())
.next(helloWorldStep2())
.build();
}
@Bean
public Step helloWorldStep1() {
return stepBuilderFactory.get("helloWorldStep1")
.tasklet((contribution, chunkContext) -> {
System.out.println(" >>> helloWorldStep1 ์คํ๋จ.");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step helloWorldStep2() {
return stepBuilderFactory.get("helloWorldStep2")
.tasklet((contribution, chunkContext) -> {
System.out.println(" >>> helloWorldStep2 ์คํ๋จ.");
return RepeatStatus.FINISHED;
})
.build();
}
}๊ทธ๋ฆฌ๊ณ ๋ฐฐ์น ์ ํ๋ฆฌ์ผ์ด์
์ ๋์์ํค๊ธฐ์ํด ์๋์ ๊ฐ์ด ์คํ๋ง ๋ถํธ ์ ํ๋ฆฌ์ผ์ด์
์คํํ๋ ๋ถ๋ถ์ @EnableBatchProcessing๋ฅผ ๋ฃ์ด์ค๋ค.
SpringBatchTestApplication.java
@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBatchTestApplication.class, args);
}
}๊ทธ๋ฆฌ๊ณ ์ด์ ํ์ Spring Boot ๊ธฐ๋ฐ์ ์ ํ๋ฆฌ์ผ์ด์ ์ ์คํํ๋ฏ์ด ์คํ์ ํด์ฃผ๋ฉด ์๋์ ๊ฐ์ด ๋ฐฐ์น๊ฐ ๋์ํ์ฌ ์ฝ์์ ์ถ๋ ฅ๋๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
@EnableBatchProcessing์ ์คํ๋ง ๋ถํธ ๋ฉ์ธ ํด๋์ค๋ ์๋ฌด ์ค์ ํ์ผ์ค ํ๋์ ๋ช ์ํด์ฃผ๊ธฐ๋ง ํ๋ฉด ๋๋ค. (๋๋ถ๋ถ ๋ถํธ ๋ฉ์ธ ํด๋์ค์ ์ ์ํ๋ค.)
์ด๋ค๊ฐ! ๊ต์ฅํ ๊ฐ๋จํ์ง ์์๊ฐ?? ๋ฐฐ์น๋ฅผ ๋์ํ๋๋ฐ ํ์ํ ์๋ฐ ๋น ๋ช๊ฐ์ง๋ฅผ ์ค์ ํด์ฃผ๋ฉด๊ณ ์คํํ๋ฉด ์ํ๋ ์์์ ๋ฐ๋ผ ๋ฐฐ์น๊ฐ ๋์ํ๋ค.
์ด๋ฒ์ Hello World๋ฅผ ๋ฒ์ด๋์ ๋ฐฐ์น ๊ตฌ์กฐ๋ฅผ ์ดํดํ๊ณ Hello World ์ฝ๋๋ฅผ ์ดํดํด๋ณธ๋ค.
Hello World Job ์ฝ๋๋ฅผ ๋ณด๋ฉด ๋์ ๋๋ ๋๋ฉ์ธ ๊ฐ์ฒด๊ฐ ์๋ค. ๋ฐ๋ก Job, Step, Tasklet์ด๋ค.
์ธ ๋๋ฉ์ธ์ ๊ด๊ณ๋ ์๋์ ๊ฐ๋ค.
- Job - ์ผ, ์ผ๊ฐ (์ ์ฒด์ ์ธ ์ผ)
- ๋ฐฐ์น ๊ตฌ์กฐ์์ Job์ด๋ ๊ฐ์ฅ ์์์ ์๋ ๊ฐ๋ ์ผ๋ก ํ๋์ ๋ฐฐ์น์์ ์์ฒด๋ฅผ ์๋ฏธํ๋ค.
- ํ๋์ Job์๋ ์ฌ๋ฌ ๊ฐ์ Step์ด ์กด์ฌํ๋ค. (์ ์ด๋ ํ๋ ์ด์)
- Step - ์ผ์ ๋จ์, ์ผ์ ํญ๋ชฉ (๋จ๊ณ)
- ๋ฐฐ์น Job์ ๊ตฌ์ฑํ๋ ๋ ๋ฆฝ์ ์ธ ํ๋์ ๋จ๊ณ๋ฅผ ์๋ฏธํ๋ฉฐ, ์ค์ ๋ฐฐ์น ์ฒ๋ฆฌ์ ๋ํ ๊ตฌํ์ ์ ์ํ๊ณ ์ปจํธ๋กคํ๋ ๋ฐ ํ์ํ ๋ชจ๋ ์ ๋ณด๋ฅผ ๊ฐ์ง๋ค.
- Step์
Taskletํน์Reader + Processor + Writer(Chunk ๊ธฐ๋ฐ ์ฒ๋ฆฌ)๋ก ๊ตฌ์ฑ๋๋ค.- ๊ฐ์ Step ์์์ ๋ ๋ฐฉ์์ ํผ์ฉํด์ ์ฌ์ฉํ ์ ์๋ค.
- Tasklet ์ผ์ ์์
๋ด์ฉ (๊ตฌํ, ๋จ๊ณ ๋ณ ๋น์ฆ๋์ค ๋ก์ง)
Step์์์ ๋จ์ผ ํ์คํฌ๋ก ์ํ๋๋ ๋ก์ง์ ๊ตฌํํ๋ค.- tasklet์ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฌดํ ๋ฐ๋ณต์ํจ๋ค.
RepeatStatus๋ฅผ ํตํด ๋ฐ๋ณตํ ๊ฒ์ธ์ง๋ฅผ ๋ฐํํด์ค์ผํ๋ค.
Chunk๊ธฐ๋ฐ์ ์ฒ๋ฆฌ ์์์ธ ItemReader, ItemProcessor, ItemWriter๋ ์ฐ์ ๋ฌด์ํด๋ ๋๋ค.
๊ทธ๋ฆผ์ ํตํด Job๊ณผ Step๊ฐ์ ๊ด๊ณ๋ฅผ ์์์ผ๋, ์ด์ ๋ฐ๋ก ์์์ ๊ตฌ์ฑํ๋ HelloWorldJob์ ์ค์ ํ์ผ์ ์ดํด๋ณด๋ฉด ๊ตฌ์กฐ๊ฐ ์กฐ๊ธ ์ดํด๊ฐ ๋๋ค.
- @Configuration
- ๋ณดํต ํ๋์ ๋ฐฐ์น Job์ ์ ์ํ ๋ ์ฌ์ฉ๋๋ ๋น ์ค์
- JobBuilderFactory, StepBuilderFactory
- Job๊ณผ Step ๋ชจ๋ ์ฌ๋ฌ ๊ฐ์ ๊ตฌํ์ฒด๋ฅผ ๊ฐ์ง๊ณ ์๋ค. ์ด์ ๊ด๋ จ๋ ๋น๋ ๋ํ ์ฌ๋ฌ๊ฐ์ง์๋ค.
- ์ ๋ ํฉํ ๋ฆฌ ๊ฐ์ฒด๋ ์ด๋ค ๋ฉ์๋ ์ฒด์ด๋์ ํ๋๋์ ๋ฐ๋ผ์ ๊ฐ๊ฐ ๊ตฌํ์ฒด์ ๋น๋๋ฅผ ๋ฐํํ๋๋ก ํด์ค๋ค.
- ex. Step์ ๊ฒฝ์ฐ ๋ฉ์๋ ์ฒด์ด๋์ผ๋ก
chunk๋ฅผ ์ฃผ๋ฉดSimpleStepBuilder๊ฐ ์์ฑ๋๊ณ ,tasklet์ ์ฃผ๋ฉดTaskletStepBuilder๊ฐ ์์ฑ๋๋ค.
- ex. Step์ ๊ฒฝ์ฐ ๋ฉ์๋ ์ฒด์ด๋์ผ๋ก
- ์ฝ๊ฒ ์๊ฐํ๋ฉด, Job์ Step์ ์์ฑํ๋ ๋น๋ ํฉํ ๋ฆฌ๋ผ๊ณ ๋ณด๋ฉด ๋๋ค. (์ด์ ๊ด๋ จ๋ ๋ ์์ธํ ๋ด์ฉ์ ์ถํ์ ๋ค๋ฃฐ ์์ ์ด๋ค.)
- Job
- Job์ ์ ์ํ๋ค.
- Step
- Step์ ์ ์ํ๋ค.
- Tasklet
- ๊ฐ Step์ด ์คํ๋์๋ค๋ ์ฝ์ ์ถ๋ ฅ์ ๋ํ ๊ตฌํ ์ฝ๋๋ฅผ ์์ฑํ์๋ค.
์ด์ Hello World ์ฝ๋์ ์ ๊ทธ๋ฆผ์ ํตํด ๋ฐฐ์น ๊ตฌ์กฐ์ ๋ํ ํฐ๊ทธ๋ฆผ์ ์ ์ ์์๋ค.
๋ ์ดํดํ๊ธฐ ์ฌ์ด ๋ฐฉ๋ฒ์ ๊ฐ ์์๊ฐ์ ์คํ FLOW๋ ์ด๋ป๊ฒ๋๋์ง๋ฅผ ๋ณด๋ฉด๋๋ค.
์๋ ๊ทธ๋ฆผ์ Job, Step, Tasklet ๊ฐ์ ๊ด๊ณ๋ฅผ ๋์ฑ ์ ๋ณด์ฌ์ฃผ๋ ๋ฐฐ์น ์คํ FLOW ์ด๋ค.

ํ์ดํ๊ฐ ์คํ๋๋ ๋ฐฉํฅ์ด๊ธฐ๋ ํ๊ณ , ๊ตฌ์ฑ ์์๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ฐฉํฅ๋ ๋๋ค.
- ์คํ ์์
- Job -> Step -> Tasklet
- ์คํ๋ง ๋ฐฐ์น๋ฅผ ์คํ์ํค๋ฉด ์์ ๊ฐ์ด ํ์ํ Job, Step, Tasklet์ ๋ํ ๋น์ด ๋ฑ๋ก๋๊ณ , ์ ์์๋ก ์คํ๋๊ฒ ๋๋ค.
Job์Step์ ๊ฐ์ง๊ณ ์๋ ์ปจํ ์ด๋ ์ญํ ์ ํ๋ค๊ณ ๋ณด๋ฉด ๋๋ค.
์ด์ ์ฒ์์ ๊ตฌ์ฑํ๋ Hello World Job์ด ์ดํด๊ฐ ์ฝ๊ฒ ๋ ๊ฒ์ด๋ผ ์๊ฐ๋ ๋ค.
์ด์ ๋ฐฐ์น ๊ตฌ์กฐ๋ฅผ ์ดํด๋ณด์์ผ๋ Job, Step, Tasklet์ ๊ด๊ณ์ ์ด๋ป๊ฒ ๊ตฌ์ฑ๋๊ณ ๋์ํ๋์ง ์๊ฒ๋์๋ค.
๊ทธ๋ ๋ค๋ฉด ์ด๋ป๊ฒ @EnableBatchProcessing ์ ๋
ธํ
์ด์
๋ง ๋ถ์ด๊ณ ๋ฐฐ์น๋ฅผ ์คํ์ํฌ ์ ์์์๊น?
๋ง์ฝ ํ ํ๋ก์ ํธ์ ์ฌ๋ฌ ๊ฐ์ ๋ฐฐ์น Job์ ๊ตฌ์ฑํ๊ณ ํน์ Job๋ง ์คํ์ํค๊ณ ์ถ๋ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น?
์ด๋ฒ ์ฑํฐ์์ ๋ฐฐ์น๋ฅผ ์คํ์ํค๋ ๋ค์ํ ๋ฐฉ๋ฒ์ ๋ํด์ ์์๋ณด๊ณ , @EnableBatchProcessing๋ฅผ ๋ถ์ด๋ฉด ์ด๋ค ํจ๊ณผ๊ฐ์๋์ง ์์๋ณธ๋ค.
์์์ Job, Step, Tasklet์ ๋ํด์๋ง ์๊ธฐํ์ง๋ง, ์ฌ์ค ์กฐ๊ธ ๋ ํฐ ๊ทธ๋ฆผ์ ๋ณด๋ฉด ๋ฐฐ์น์ ๊ตฌ์กฐ๋ ์๋์ ๊ฐ๋ค.

๊ณต์ ๋ฌธ์์ ๊ทธ๋ฆผ์ ์กฐ๊ธ ์์ ํ ์ด๋ฏธ์ง์
๋๋ค.
๊ฐ์ฅ ์ผ์ชฝ์ ๋ณด๋ฉด JobLauncher๋ผ๋ ๋๋ฉ์ธ์ ๋ณผ ์ ์๋ค.
์ด๋ฆ์์ ์ ์ ์๋ฏ์ด, ์ด ๋๋ฉ์ธ์ ๋ฐฐ์น Job์ ์คํ์ํค๋ ์ญํ ์ ํ๋ค.
์กฐ๊ธ ๋ ์์ธํ ์ดํด๋ณด๋ฉด ์๋ ์ฝ๋์ ๊ฐ๋ค.

JobLauncherCommandLineRunner.execute ๊ตฌํ๋ถ๋ถ
์ ์ฝ๋๋ ์ค์ ์คํ๋ง ๋ถํธ๊ธฐ๋ฐ์ Spring Batch์์ Job์ ์คํํ ๋ ์ฌ์ฉ๋๋ ์ฝ๋์ด๋ค.
JobLauncher ๊ฐ์ฒด์ Job๊ณผ JobParameters๋ฅผ ์ธ์๋ก ๋๊ฒจ run๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด ๋ฐฐ์น๊ฐ ์คํ๋๋ค.
์ฐ์ ์ JobLauncher๋ฅผ Job์ ์คํ์ํค๋ ๊ฐ์ฒด๋ก๋ง ์ดํดํ์. ๋ ์์ธํ ๋ด์ฉ์ ํ์ ์ ๋ฆฌํ ์์ ์ด๋ค.
์์์ JobLauncher๊ฐ ๋ฐฐ์น Job์ ์คํ์ํค๋ ์ฃผ์ฒด๋ผ๋ ๊ฒ์ ์ค๋ช
ํ๋ค.
์๊ฐ๋ณด๋ค ๊ฐ๋จํ์ง ์์๊ฐ? ์ด๋ฅผ ํ์ฉํ๋ฉด ๋ค์ํ ๋ฐฐ์น ์คํ ๋ฐฉ๋ฒ์ ๋ ์ฌ๋ฆด ์ ์๋ค.
์ด๋ฒ ๊ธ์์ ๋ํ์ ์ธ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ ์๊ฐํ๋ค.
1๏ธโฃ ์ฒซ๋ฒ์งธ ๋ฐฉ๋ฒ - ApplicationRunner ๊ตฌํํ์ฌ ์ง์ ์คํํ๋ ๋ฐฉ๋ฒ
์คํ๋ง ๋ถํธ (@SpringBootApplication)๋ ์คํ์ ApplicationRunner๋ฅผ ๊ตฌํํ ๋น์ run() ๋ถ๋ถ์ ํธ์ถํ๋ค.
๋ง์ฝ ์ฌ๋ฌ ๊ฐ์ ApplicationRunner๊ฐ ์กด์ฌํด์ ์์๋ฅผ ๋ณด์ฅํด์ผํ๋ค๋ฉด @Order ์ ๋
ธํ
์ด์
์ ์ด์ฉํ์ฌ ์์๋ฅผ ์ ํด์ค๋ค.
์คํ๋ง ๋ฐฐ์น์์ Job์ ์คํ์ํค๋ ์ฃผ์ฒด์ธ JobLauncher๋ ์ด ApplicationRunner ํน์ CommandLineRunner๋ฅผ ๊ตฌํํ ๋น์ ์ด์ฉํ๋ฉด ์ฝ๊ฒ ์คํ๋ง ์ ํ๋ฆฌ์ผ์ด์
์ด ์คํ๋๊ณ ์๋์ผ๋ก Job์ ์คํ์ํฌ ์ ์๋ค.
ManualJobLauncher.java
/**
* ์๋์ผ๋ก JobLauncher๋ฅผ ํตํด ๋ฐฐ์น๋ฅผ ์คํ์ํค๋ ApplicationRunner
* -> ์ค์ ํ์ผ (yml)์์ spring.batch.job.enabled = false๋ก ํด์ค์ผํ๋ค.
*/
@Component
public class ManualJobLauncher implements ApplicationRunner {
private final JobLauncher jobLauncher;
private final Collection<Job> jobs;
public ManualJobLauncher(JobLauncher jobLauncher, Collection<Job> jobs) {
this.jobLauncher = jobLauncher;
this.jobs = jobs;
}
@Override
public void run(ApplicationArguments args) throws Exception {
String jobName = "{์คํํ๊ณ ์ํ๋ job ์ด๋ฆ}"; // Job ์ด๋ฆ
// Job
Job job = determineJob(jobName);
// JobParameters
JobParameters jobParameters = new JobParametersBuilder()
.addString("name", "binghe")
.toJobParameters();
// Job (ํน์ ์ฌ๋ฌ Job) ์คํ
jobLauncher.run(job, jobParameters);
}
private Job determineJob(String jobName) {
return jobs.stream()
.filter(job -> job.getName().equals(jobName))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("์กด์ฌํ์ง ์๋ Job ์ด๋ฆ์
๋๋ค."));
}
}jobName์ ์คํ์ํค๊ณ ์ํ๋ Job์ ์
๋ ฅํ๊ณ ์คํ๋ง ๋ถํธ๋ฅผ ์คํํ๋ฉด ๋๋ค.
๋ฐ์์ ๋ค๋ฃจ๊ฒ ์ง๋ง, @EnableBatchProcessing๋ ์ด ApplicationRunner ๋ฐฉ์๊ณผ ์ ์ฌํ CommandLineRunner์ ์ด์ฉํ์ฌ Job์ ์๋์ผ๋ก ์คํ์ํจ๋ค.
์ค์ ์คํ๋ง ๋ถํธ์ ๋ฐฐ์น๋ฅผ ๊ฐ์ด ์ฌ์ฉํ๋ฉด์ ์์ ๊ฐ์ ๋ฐฉ๋ฒ์ ์ด์ฉํ๋ ค๋ฉด ์ค์ ํ์ผ (
ymlํน์properties)์์spring.batch.job.enabled = false์ค์ ์ ํด์ฃผ์ด์ผํ๋ค.
2๏ธโฃ ๋๋ฒ์งธ ๋ฐฉ๋ฒ - HTTP ์์ฒญ์ ์คํ
๋๋ฒ์งธ ๋ฐฉ๋ฒ์ ๊ต์ฅํ ์ฌ์ด.. HTTP ์น ์์ฒญ์ ์๋์ ๊ฐ์ด JobLauncher๋ฅผ ์ฃผ์
๋ฐ์ Job์ ์คํ์ํค๋ ๊ฒ์ด๋ค.
JobLauncherTestController.java
@Slf4j
@RequiredArgsConstructor
@RestController
public class JobLauncherTestController {
private final JobLauncher jobLauncher;
private final Job scopeSampleJob;
@GetMapping("/batch")
public ResponseEntity<String> launchBatch(@RequestParam("date") String date) {
JobParameters jobParameters = new JobParametersBuilder()
.addString("date", date)
.toJobParameters();
try {
jobLauncher.run(scopeSampleJob, jobParameters);
} catch (Exception e) {
System.out.println(e.getMessage());
return ResponseEntity.ok("error");
}
return ResponseEntity.ok("ok");
}
}์ ์์๋ ์น ํํ๊ณ์ธต์ JobLauncher์ Job์ ์ฃผ์
๋ฐ์ ์คํ์ํค๋ ์์์ด๋ค.
์ ๋ฐฉ์์ ์ฌ์ฉํ ๋ ์ฃผ์ํด์ผํ๋ค. ๋ง์ฝ ๋จ๊ธฐ๊ฐ์ ๋๋ฌด ๋ง์ API ์์ฒญํ๋ฉด, ๋๋ฌด ๋ง์ ๋ฐฐ์น๊ฐ ํ๋ฒ์ ์คํ๋์ ์๋ฒ์ ๋ถํ๊ฐ ๊ฑธ๋ฆฌ๊ธฐ ์ฝ๊ธฐ ๋๋ฌธ์ด๋ค.
์ผ๋ฐ์ ์ผ๋ก ๋ฐฐ์น์ ๊ฒฝ์ฐ ๋์ฉ๋์ ๋ค๋ฃจ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๊ธฐ์, CPU๋ ๋ฉ๋ชจ๋ฆฌ๊ฐ ๊ธฐ์กด์ ์น ์ ํ๋ฆฌ์ผ์ด์ ๋ณด๋ค ๋ง์ด ์์๋๋ค. ๊ทธ๋ฌ๊ธฐ์ API ์์ฒญ์ ๋ฐ์ ๋ฐฐ์น Job์ ์คํ์ํค๋ ๋ฐฉ์์ ์ง์ํ๋ ๊ฒ์ด ์ข๋ค.
์ค์ ๋ก ์คํ๋ง ๋ถํธ ๊ธฐ๋ฐ์ ์คํ๋ง ๋ฐฐ์น๋ ์์ ๊ฐ์ด JobLauncher๋ฅผ ์ด์ฉํ์ฌ Job์ ์คํ์ํจ๋ค.
๋ฌผ๋ก ์คํ๋ง ์ง์์์ Job ์คํ์ํค๋ ๋ถ๋ถ์ ๋ชจ๋ ์ถ์ํ์์ผ๋์๋ค.
๊ทธ๋์ ์๋์ ๊ฐ์ด ๊ฐ๋ฐ์๋กํ์ฌ๊ธ @EnableBatchProcessing๋ง ๋ถ์ด๋ฉด ์๋์ผ๋ก Job์ ์คํ์์ผ์ค๋ค.
@EnableBatchProcessing๋ ์คํ๋ง ๋ถํธ ํ๊ฒฝ์์ ์คํ๋ง ๋ฐฐ์น๊ฐ ์๋ํ๊ธฐ ์ํด ์ ์ธ๋๋ ์ ๋
ธํ
์ด์
์ด๋ค.
๐ค ์ ์ ์ธํด์ค์ผ ํ ๊น?
๋ฐ๋ก ๋ฐฐ์น ์์ ์ ํ์ํ ๋๋ฉ์ธ์ ์์ฑํ๊ธฐ ์ํ ๋น๋ ํฉํ ๋ฆฌ ๋น ์ค์ ๊ณผ ๋ฐฐ์น์ ํ์ํ ์ฃผ์ ๋๋ฉ์ธ์ ์๋์ผ๋ก ์ค์ ๋ฐ ์ด๊ธฐํํ๊ธฐ ์ํจ์ด๋ค. (AutoConfiguration)
์ฆ, ์คํ๋ง ๋ถํธ ๋ฐฐ์น์ ์๋ ์ค์ ํด๋์ค๋ฅผ ์คํ์ํด์ผ๋ก์จ ๋น์ผ๋ก ๋ฑ๋ก๋ ๋ชจ๋ Job์ ๊ฒ์ํด์ ํ์ํ ๋น์ ์ด๊ธฐํ์์ผ์ฃผ๋ ๊ฒ์ด๋ค.
๋ํ, ์ ํ๋ฆฌ์ผ์ด์
์ ApplicationRunner ๊ตฌํํ ๋น ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ Job์ ์๋์ผ๋ก ์คํ์์ผ์ค๋ค.
๋ฐฐ์น์ ์ฌ์ฉ๋๋ ํ์ํ ๋น์ ์์๋ก๋
JobBuilderFactory,StepBuilderFactory,JobRepository,JobLauncher๋ฑ๋ฑ์ด ์๋ค.
๐ค ์ด๋ป๊ฒ ์๋์ผ๋ก ์ค์ ํด์ค๋ค๋ ๊ฒ์ผ๊น? - ๋์ ์๋ฆฌ
@EnableBatchProcessing์ด ๋ถ์ ์ ํ๋ฆฌ์ผ์ด์
์ด ์๋ํ๋ฉด 4๊ฐ์ ์ค์ ํด๋์ค๊ฐ ์คํ๋๋ฉด์ ๋ฐฐ์น์ ๋ชจ๋ ์ด๊ธฐํ ๋ฐ ์คํ ๊ตฌ์ฑ์ด ์ด๋ฃจ์ด์ง๋ค.
SimpleBatchConfiguration- ์คํ๋ง ๋ฐฐ์น์ ์ฃผ์ ๊ตฌ์ฑ ์์๋ฅผ ์์ฑ ๋ฐ ์ด๊ธฐํ.
JobBuilderFactory,StepBuilderFactory,JobRepository,JobLauncher๋ฑ๋ฑ ๋ฐฐ์น๋ฅผ ๋๋ฆฌ๋๋ฐ ํ์ํ ๋น์ ์ด๊ธฐํํ๋ค.
- ์ฃผ์ ๊ตฌ์ฑ ์์๋ฅผ ์์ฑํ ๋, ์ค์ ํ๊ฒ ๊ฐ์ฒด๋ฅผ ์์ฑ ๋ฐ ์ด๊ธฐํํ์ง ์๊ณ ํ๋ก์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค. ๊ทธ๋ฆฌ๊ณ
BatchConfigurerConfiguration์ ๊ตฌํ์ฒด๋ค์ด ์คํ๋๋ฉด์ ํ๊ฒ ๊ฐ์ฒด๋ฅผ ์ค์ ํ๋ค. - ์ค์ InitializingBean์ ํ์ฉํ์ฌ ๋น์ ์์ฑํ ๋ ์์ ๊ฐ์ด ๋ฐฐ์น์ ํ์ํ ๋น๋ค์ ์ค์ ํด์ค๋ค.
- ์คํ๋ง ๋ฐฐ์น์ ์ฃผ์ ๊ตฌ์ฑ ์์๋ฅผ ์์ฑ ๋ฐ ์ด๊ธฐํ.
BatchConfigurerConfigurationBasicBatchConfigurerSimpleBatchConfigurer์์ ์์ฑ๋ ํ๋ก์ ๊ฐ์ฒด์ ์ค์ ๋์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ์ค์ ํด๋์ค์ด๋ค. - ์ค์- ๋น์ผ๋ก ์์กด์ฑ ์ฃผ์ ๋ฐ์์ ์ฃผ์ ๊ฐ์ฒด๋ค์ ์ฐธ์กฐํ์ฌ ์ฌ์ฉํ ์ ์๋ค.
JpaBatchConfigrer- JPA ๊ด๋ จ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ์ค์ ํด๋์ค์ด๋ค.
BatchAutoConfiguration- ์คํ๋ง ๋ฐฐ์น์ ์ฃผ์ ๊ตฌ์ฑ ์์๊ฐ ์์ฑ ๋ฐ ์ด๊ธฐํ๋๊ณ ์คํ๋๋ฉฐ,
ApplicationRunner๋ฅผ ๊ตฌํํJobLauncherApplicationRunner๋น์ ์์ฑ๊ณผ ๋์์ ์ง์ ๋ Job์ ์คํํ๋ค. - ์์์ ์ค๋ช
ํ๋ฏ์ด,
ApplicationRunner๋ ๋น์ด ์์ฑ๊ณผ ๋์์run()๋ฉ์๋๋ฅผ ํธ์ถํ๋ค. ์ด๋ Job์ ์คํํ๊ฒ ๋๋ ๊ฒ์ด๋ค.
- ์คํ๋ง ๋ฐฐ์น์ ์ฃผ์ ๊ตฌ์ฑ ์์๊ฐ ์์ฑ ๋ฐ ์ด๊ธฐํ๋๊ณ ์คํ๋๋ฉฐ,
์์ ๋งํ๋ฏ, @EnableBatchProcessing์ ๋ถ์ด๋ฉด ์คํ๋ง ๋ฐฐ์น ์ ํ๋ฆฌ์ผ์ด์
์ ์คํํ๋๋ฐ ํ์ํ ๋๋ฉ์ธ๊ณผ ์ธํ๋ผ ๋น ๊ฐ์ฒด๋ฅผ ์์ฑ ๋ฐ ์คํ์์ผ์ค๋ค.
์ฌ์ค ์คํ๋ง ๋ฐฐ์น๋ฅผ ์ด์ฉํ์ฌ ๋ฐฐ์น ์ฝ๋๋ฅผ ์์ฑํ ๋, ์ ๋ด์ฉ์ ๊ทธ๋ ๊ฒ ์ค์ํ์ง ์๋ค.
ํ๋ ์์ํฌ์ ์ฅ์ ์ค ํ๋๋ ๊ฐ๋ฐ์๋ก ํ์ฌ๊ธ ํต์ฌ ๋น์ฆ๋์ค์๋ง ์ง์คํ ์ ์๊ฒ ํ๋ ๊ฒ์ด๋ค.
์ด๋ฌํ ์ ์์ ์คํ๋ง ์ง์์ ์ถ์ํ๋ฅผ ์ ์ ์ฉํ๋ค๊ณ ๋ณผ ์๋ ์๋ค.
๊ทธ๋์ ์ด๋ฒ ๊ธ์ ๋ง์ง๋ง์ผ๋ก ์์๋ณผ ๋ด์ฉ์ ๊ฐ๋ฐ์๊ฐ ์ค์ ์ฒ์ ๋ฐฐ์น ์ ํ๋ฆฌ์ผ์ด์
์ ๊ถ๊ธํ ๋ถ๋ถ์ธ ํ๋ก์ ํธ๋ด์์ ์ํ๋ Job์ ์คํ์ํค๋ ๋ฐฉ๋ฒ์ด๋ค.
BatchAutoConfiguration ๋ด๋ถ ์ฝ๋๋ฅผ ์์ธํ ๋ณธ ์ฌ๋์ ์๊ฒ ์ง๋ง..
JobLauncher์ ๋น์ ์์ฑํ๋ ๋ถ๋ถ์ ๋ณด๋ฉด BatchProperties์์ ์คํํ Job์ ์ด๋ฆ์ ๊ฐ์ ธ์์ Setter๋ก ์ฃผ์
ํด์ค๋ค.
๊ทธ๋ฆฌ๊ณ BatchProperties์ ๋ด๋ถ ์ฝ๋๋ฅผ ๋ณด๋ฉด ์๋์ ๊ฐ์ด spring.batch.job.names์์ ํด๋น Job ์ด๋ฆ ๊ฐ์ ธ์ค๋ ๊ฒ์ ์ ์ ์๋ค.
์ฆ... ์ํ๋ Job์ ์คํ์ํค๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์น ์ ํ๋ฆฌ์ผ์ด์
์คํ์ spring.batch.job.names์ Job Name์ ๊ธฐ์
ํด์ฃผ๋ฉด ๋๋ค.

IntelliJ์์ ์คํํ๋ ์์
CLIํ๊ฒฝ์์ ์คํํ๋ค๋ฉด ์๋์ ๊ฐ์ด ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํด์ฃผ๋ฉด ๋๋ค.
java -jar batch-application.jar --job.name={jobNames}







