Spring Boot Batch - stellaxxx/batch GitHub Wiki

Spring Batch + Boot

  • Batch ์ˆ˜ํ–‰์— ๋Œ€ํ•ด Boot์—์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•ด ์คŒ. (@EnableBatchProcessing (from Spring Batch))
  • ๋ชจ๋“  Job์„ ์‹คํ–‰์‹œํ‚ค๋Š”๊ฒŒ ๋””ํดํŠธ์ด์ง€๋งŒ ํŠน์ • JOB์„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Œ. (ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์„ ํ†ตํ•ด ์ง€์ •)

Spring Batch auto configuration is enabled by adding @EnableBatchProcessing (from Spring Batch) somewhere in your context. By default it executes all Jobs in the application context on startup (see JobLauncherCommandLineRunner for details). You can narrow down to a specific job or jobs by specifying spring.batch.job.names (comma separated job name patterns). If the application context includes a JobRegistry then the jobs in spring.batch.job.names are looked up in the registry instead of being autowired from the context. This is a common pattern with more complex systems where multiple jobs are defined in child contexts and registered centrally. See BatchAutoConfiguration and @EnableBatchProcessing for more details.

  • Boot ์—์„œ ๋‚ด๋ถ€์ ์œผ๋กœ JobLauncherCommandLineRunner๋ฅผ ์ƒ์„ฑ, ์ง€์ •ํ•œ JOB๋“ค์„ ์‹คํ–‰์‹œํ‚จ๋‹ค.
  • Job ๊ตฌ๋™ ํ™˜๊ฒฝ์— ๋Œ€ํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค์ • ๊ฐ€๋Šฅ
@Configuration
@ComponentScan
@EnableBatchProcessing
@EnableAutoConfiguration
public class MelonApplication {
        public static void main( String [] args ) {
               SpringApplication app = new SpringApplication(MelonApplication . class);
               app .run(args );
       }
}

JOB ์‹คํ–‰ ๊ด€๋ จ

  • Job Initialize๋ฅผ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ”„๋กœํผํ‹ฐ ์„ค์ • spring.batch.job.enabled=true #default๊ฐ€ true์ž„.
  • job Name์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ๋“ฑ๋ก๋œ ๋ชจ๋“  JOB์ด ์‹คํ–‰๋˜๋ฏ€๋กœ ์ฃผ์˜!!
  • main ํด๋ž˜์Šค ์‹คํ–‰ ์‹œ ์ปค๋งจ๋“œ๋ผ์ธ ํŒŒ๋ผ๋ฉ”ํ„ฐ๋กœ job name ์ง€์ • --spring.batch.job.names=myJob
  • ๋ณต์ˆ˜๊ฐœ์˜ JOB๋„ ์ง€์ • ๊ฐ€๋Šฅํ•จ. (์ฝค๋งˆ๋กœ ๊ตฌ๋ถ„)

JOB ํŒŒ๋ผ๋ฉ”ํ„ฐ

  • ์ปค๋งจ๋“œ๋ผ์ธ ํŒŒ๋ผ๋ฉ”ํ„ฐ๋กœ ์ค„ ๊ฒฝ์šฐ @Value ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•จ
@Value("${date}")
private String date;