Spring-boot Batch with H2

์žญ์žญ์ดยท2021๋…„ 4์›” 21์ผ
1

Spring-boot

๋ชฉ๋ก ๋ณด๊ธฐ
5/11
post-thumbnail

Spring-boot Batch with H2

๐ŸŽ ๋ชฉ์ฐจ

0. ๊ฐœ์š”

Batch๋ž€ ์ผ๊ด„์ฒ˜๋ฆฌ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
batch application์€ ๋‹ค์Œ์„ ๋งŒ์กฑํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

  • ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ: ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
  • ์ž๋™ํ™”: ์‚ฌ์šฉ์ž ๊ฐœ์ž… ์—†์ด ์‹คํ–‰
  • ๊ฒฌ๊ณ ์„ฑ: ์ž˜๋ชป๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถฉ๋Œ/์ค‘๋‹จ ์—†์ด ์ฒ˜๋ฆฌ
  • ์‹ ๋ขฐ์„ฑ: ๋กœ๊น…, ์•Œ๋ฆผ ๋“ฑ ์˜ค๋ฅ˜ ์ถ”์ 
  • ์„ฑ๋Šฅ: ์ง€์ •ํ•œ ์‹œ๊ฐ„ ์•ˆ์— ์ฒ˜๋ฆฌ ์™„๋ฃŒ, ํƒ€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ฐฉํ•ด์—†๋Š” ๋…๋ฆฝ์„ฑ

batch๋Š” Job์˜ ์ง‘ํ•ฉ์ด๋‹ค.

  • Job์€ Step์˜ ์ง‘ํ•ฉ
  • Step์€ ItemReader + ItemProcessor + ItemWriter๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.
  • ItemRearder: batch data๋ฅผ ์ฝ์–ด์˜ค๋Š” ์ธํ„ฐํŽ˜์ด์Šค. (DB, file, xml ๋“ฑ)
  • ItemProcessor: ์ฝ์–ด์˜จ data๋ฅผ ๊ฐ€๊ณต/์ฒ˜๋ฆฌ
  • ItemWriter: ๊ฐ€๊ณต/์ฒ˜๋ฆฌํ•œ ๋ฐ์ดํ„ฐ๋ฅผ DB, file ๋“ฑ์— ์ €์žฅ

Spring-boot batch๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”์ด ํ•„์š”ํ•˜๋‹ค.

  • BATCH_STEP_EXECUTION
  • BATCH_STEP_EXECUTION_CONTEXT
  • BATCH_JOB_INSTANCE
  • BATCH_JOB_EXECUTION
  • BATCH_JOB_EXECUTION_PARAMS
  • BATCH_JOB_EXECUTION_CONTEXT

h2์™€ ๊ฐ™์€ ์ธ๋ฉ”๋ชจ๋ฆฌ db๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด spring-boot์ด ํ…Œ์ด๋ธ”๋“ค์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด์ค€๋‹ค
๋ณธ ๋ฌธ์„œ์—์„œ๋Š” h2๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Batch๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

1. Dependency ์ถ”๊ฐ€

build.gradle์— ๋‹ค์Œ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

dependencies {  
  implementation 'org.springframework.boot:spring-boot-starter-batch'
  implementation 'com.h2database:h2'
}
  • org.springframework.boot:spring-boot-starter-batch
    • spring-boot batch ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
  • com.h2database:h2
    • H2(Inmemory database) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

2. Batch ํ™œ์„ฑํ™”

  1. DemoApplication.java์ˆ˜์ •
    main class์— @EnableBatchProcessing์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
@EnableBatchProcessing
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}
  • @EnableBatchProcessing
    • ๋ฐฐ์น˜๊ธฐ๋Šฅ ํ™œ์„ฑํ™”

3. Job ์ž‘์„ฑ

  1. package ์ƒ์„ฑ
    job ํด๋”๋ฅผ ๋งŒ๋“ ๋‹ค.
  2. job class ์ƒ์„ฑ
    ํ•ด๋‹น ํด๋” ์•„๋ž˜์— SimpleJobConfiguration.java๋ฅผ ๋งŒ๋“ ๋‹ค.
@Slf4j
@RequiredArgsConstructor
@Configuration
public class SimpleJobConfiguration {
  private final JobBuilderFactory jobBuilderFactory; 
  private final StepBuilderFactory stepBuilderFactory; 

  @Bean
  public Job simpleJob() {
    return jobBuilderFactory.get("simpleJob")
            .start(simpleStep1())
            .build();
  }

  @Bean
  public Step simpleStep1() {
    return stepBuilderFactory.get("simpleStep1")
            .tasklet((contribution, chunkContext) -> {
              log.info(">>>>> This is Step1");
              return RepeatStatus.FINISHED;
            })
            .build();
  }
}
  • @Slf4j
    • Log ์‚ฌ์šฉ์„ ์œ„ํ•œ Lombok ์–ด๋…ธํ…Œ์ด์…˜
  • @RequiredArgsConstructor
    • ์ƒ์„ฑ์ž ์ž๋™ ์ƒ์„ฑ
  • @Configuration
    • spring batch์˜ ๋ชจ๋“  job์€ @Configuration์œผ๋กœ ๋“ฑ๋กํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.
  • jobBuilderFactory.get("simpleJob")
    • simpleJob์ด๋ž€ ์ด๋ฆ„์˜ batch job์„ ์ƒ์„ฑํ•œ๋‹ค.
  • stepBuilderFactory.get("simpleStep1")
    • simpleStep1 ์ด๋ž€ ์ด๋ฆ„์˜ batch Step์„ ์ƒ์„ฑํ•œ๋‹ค.
  • tasklet()
    • step ์•ˆ์—์„œ ์ˆ˜ํ–‰๋  ๊ธฐ๋Šฅ์„ ๋ช…์‹œํ•œ๋‹ค.

4. ์‹คํ–‰

# ๋นŒ๋“œ
./gradlew build
# Spring-boot ์‹คํ–‰
java -jar ./build/lib/*.jar

job์ด ์‹คํ–‰๋˜๋Š” ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.


๋ชจ๋“  ์†Œ์Šค๋Š” ๊นƒํ—ˆ๋ธŒ์— ์˜ฌ๋ ค๋†“์•˜๋‹ค.
์ฐธ๊ณ ๋งํฌ: jojoldu ๋ธ”๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€