๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring/Batch

Spring Batch 5 ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

by ํ‘์‹œ๋ฐ” 2023. 7. 30.

๐Ÿ˜ข ๋ฐฐ๊ฒฝ

ํ† ์ด ํ”„๋กœ์ ํŠธ Spring Boot 3 ํ™˜๊ฒฝ์—์„œ Spring Batch 5๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, ๊ธฐ์กด์— Spring Boot 2์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ Job, Step ์„ ์–ธ ๋ฐฉ์‹์œผ๋กœ๋Š” ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฑธ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

 

0. ๊ธฐ๋ณธ ์กฐ๊ฑด

Spring Batch 5๋Š” Spring framework 6, Spring Boot 3 ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์ตœ์†Œ Jdk 17์„ ํ•„์š”๋กœ ํ•œ๋‹ค.

 

1. StepBuilderFactory, JobBuilderFactory = Deprecated

๊ธฐ์กด Spring Batch 4์—์„œ๋Š” JobBuilderFactory, StepBuilderFactory๋ฅผ ์‚ฌ์šฉํ•ด Job๊ณผ Step ๊ตฌ์„ฑํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ Spring Batch 5์—์„œ๋Š” BuilderFactory๊ฐ€ Deprecated ๋˜์—ˆ์œผ๋ฏ€๋กœ, ์ง์ ‘ ๋ช…์‹œํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

BuilderFactory๋Š” ์‚ฌ์‹ค Job, Step์„ ๊ตฌ์„ฑ๋ฟ ์•„๋‹ˆ๋ผ, JobRepository๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์„ค์ •ํ•˜๋Š” ๊ธฐ๋Šฅ๋„ ํฌํ•จํ•˜๊ณ  ์žˆ์—ˆ๋‹ค.

 

๊ธฐ์กด JobBuilderFacotry ๋‚ด๋ถ€

๊ทธ๋Ÿฌ๋ฏ€๋กœ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ Job, Step ์„ ์–ธ๋ฟ ์•„๋‹ˆ๋ผ JobRepository ๋˜ํ•œ ๋ช…์‹œ์ ์œผ๋กœ ์ œ๊ณตํ•ด์•ผ ํ•œ๋‹ค.

 

@Bean
public Job todayScheduleJob(JobRepository jobRepository, Step todayScheduleStep) {
	return new JobBuilder("today-schedule-job", jobRepository)
			.incrementer(new RunIdIncrementer())
			.start(todayScheduleStep)
			.build();
}

 

Step๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•„์š”ํ•œ Repository๋ฅผ ์ œ๊ณตํ•ด์•ผ ํ•˜๋ฉฐ Tasklet ๋˜๋Š” Chunk๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, TransactionManager๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ œ๊ณตํ•ด์•ผ ํ•œ๋‹ค.

 

@Bean
public Step todayScheduleStep(JobRepository jobRepository,  PlatformTransactionManager transactionManager) {
	return new StepBuilder("today-schedule-step", jobRepository)
			.tasklet((stepContribution, chunkContext) -> {
				System.out.println("hello");
				return RepeatStatus.FINISHED;
			}, transactionManager).build();
}

 

2. @EnableBatchProcessing ์‚ฌ์šฉ์€ ํ•„์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค.

๊ธฐ์กด์—๋Š” @EnableBatchProcessing๋ฅผ ๋ฉ”์ธ ํด๋ž˜์Šค์— ์„ ์–ธํ•˜๊ณ  Job, Step๋“ค์€ ๋ณ„๋„์˜ ์„ค์ • ํด๋ž˜์Šค์—์„œ Bean์œผ๋กœ ๋“ฑ๋กํ•˜๋Š” ํ˜•์‹์„ ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋” ์ด์ƒ @EnableBatchProcessing์„ ํ•„์ˆ˜๋กœ ์„ ์–ธํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

 

3. ์„ค์ • ์ปค์Šคํ„ฐ ๋งˆ์ด์ง•์ด ํ•„์š”ํ•˜๋‹ค๋ฉด DefaultBatchConfiguration๋ฅผ ์ƒ์†ํ•ด ๋ณด์ž. 

Spring Batch 5์—์„œ๋Š” DefaultBatchConfiguration ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•ด์„œ ์—ฌ๋Ÿฌ ์„ค์ •๋“ค์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•œ๋‹ค. ํ•„์š”ํ•˜๋‹ค๋ฉด ํ•ด๋‹น ํด๋ž˜์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” getTransactionManager(), getDataSource(), getCharset() ๋“ฑ์„ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

@Override
protected Charset getCharset() {
	return StandardCharsets.UTF_8;
}

 

์ฃผ์˜ํ•  ์ ์€ @EnableBatchProcessing์™€ DefaultBatchConfiguration๋ฅผ ๋™์‹œ์— ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ ๋œ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ , ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค๋ฉด ๋‘˜ ๋‹ค ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

REFERENCE

https://spring.io/guides/gs/batch-processing/

https://github.com/spring-projects/spring-batch/wiki/Spring-Batch-5.0-Migration-Guide

https://docs.spring.io/spring-batch/docs/current/reference/html/whatsnew.html

๋Œ“๊ธ€