Spring Framework/Spring & Spring Boot

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

soogoori 2024. 7. 31. 10:24

Spring Batch 5 

  • ์ตœ์†Œ Java 17 ํ•„์š” 
  • @EnableBatchProcessing ์–ด๋…ธํ…Œ์ด์…˜ ๊ถŒ์žฅ X ๐Ÿ‘‰ ์‚ฌ์šฉํ•  ์‹œ autoConfiguration ์ผ๋ถ€ ๊ธฐ๋Šฅ ์‚ฌ์šฉ ๋ถˆ๊ฐ€ 

 

 

Spring Batch ์ดˆ๊ธฐ ์„ค์ •

spring:
  profiles:
    active: local
  batch:
    job:
      names: ${job.name:NONE}

---
spring:
  config:
    activate:
      on-profile: local
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/house
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 
  jpa:
    show-sql: true
    generate-ddl: false
    hibernate:
      ddl-auto: none
  batch:
    jdbc:
      initialize-schema: ALWAYS

batch ๊ด€๋ จํ•ด์„œ initialize-schema: ALWAYS๋กœ ์„ค์ •ํ•˜๋ฉด DB schema๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•œ๋‹ค. 

DB schema์—๋Š” ์‹คํ–‰์ •๋ณด์™€ ์ƒํƒœ์ •๋ณด ๋“ฑ์„ ์ €์žฅํ•ด ์Šคํ”„๋ง ๋ฐฐ์น˜์˜ ์‹คํ–‰ ๋ฐ ๊ด€๋ฆฌ ๋ชฉ์ ์œผ๋กœ ์ €์žฅ, ์—…๋ฐ์ดํŠธ, ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. 

์ด ๋ฉ”ํƒ€ ํ…Œ์ด๋ธ”์ด ํ•„์ˆ˜์ ์œผ๋กœ ์ƒ์„ฑ๋˜์–ด์•ผ ํ•œ๋‹ค !

์ƒ์„ฑ ์™„๋ฃŒ


โœณ๏ธ Job ์ƒ์„ฑ

  • ๊ธฐ์กด Spring Batch 4์—์„œ๋Š” JobBuilderFactory๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ƒ์„ฑํ•˜์˜€์ง€๋งŒ Spring Batch 5์—์„œ๋Š” deprecated ๋˜์—ˆ๋‹ค.

 

  • JobBuilder(String name, JobRepository jobRepository) ์‚ฌ์šฉ
  • incrementer : Job ์‹คํ–‰ ํšŸ์ˆ˜ ์ผ์ •ํ•˜๊ฒŒ ์ฆ๊ฐ€
@Bean("myJob")
public Job helloJob(JobRepository jobRepository, Step helloStep){
    return new JobBuilder("myJob", jobRepository)
            .incrementer(new RunIdIncrementer())
            .start(helloStep)
            .build();
}

 

โœณ๏ธ Step ์ƒ์„ฑ

@JobScope
@Bean("myStep")
public Step helloStep(Tasklet myTasklet, JobRepository jobRepository, PlatformTransactionManager transactionManager) {
    return new StepBuilder("myStep", jobRepository)
            .tasklet(myTasklet, transactionManager)
            .build();
}

 

 

โœณ๏ธ Tasklet ์ƒ์„ฑ

@StepScope
@Bean
public Tasklet myTasklet(){
    return ((contribution, chunkContext) -> {
        log.info(">>>>> This is Step1");
        return RepeatStatus.FINISHED;
    });
}

 

 

Spring Batch ์‹คํ–‰ ๋ฐฉ์‹

  • Quartz Scheduler
  • CI ํ”„๋กœ๊ทธ๋žจ ์ด์šฉ ๐Ÿ‘‰ Jenkins
    • ๋งˆ์Šคํ„ฐ์—์„œ ์Šฌ๋ ˆ์ด๋ธŒ๋กœ ๋ช…๋ น์„ ์ „๋‹ฌํ•ด ๋ฐฐ์น˜ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰
    • Jenkins์—์„œ ์ง€์›ํ•˜๋Š” ์Šค์ผ€์ค„๋ง ๊ธฐ๋Šฅ์„ ํ†ตํ•ด ์‹คํ–‰
  • Spring Cloud Data Flow 
    • Kubernetes ํด๋Ÿฌ์Šคํ„ฐ ๊ตฌ์ถ•

 

 

 

์‹คํ–‰์‹œํ‚ค๊ธฐ

๐Ÿ”น ์ธํ…”๋ฆฌ์ œ์ด๋กœ ์‹คํ–‰์‹œํ‚ค๊ธฐ

job ์ด๋ฆ„์„ myJob์œผ๋กœ ์„ค์ •ํ•ด์„œ ํ•ด๋‹นํ•˜๋Š” job ํƒ์ƒ‰ ํ›„ ์‹คํ–‰ 

 

 

 

๐Ÿ”น ํ„ฐ๋ฏธ๋„๋กœ ์‹คํ–‰์‹œํ‚ค๊ธฐ

java -jar build/libs/spring-batch-0.0.1-SNAPSHOT.jar --job.name=myJob

 

 

 

 

์ฐธ๊ณ ์ž๋ฃŒ

https://docs.spring.io/spring-batch/reference/schema-appendix.html

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