JSCODE Logo
블로그후기멘토진
회사명 : JSCODE대표 : 박재성사업자 등록번호 : 244-22-01557통신판매업 : 제 2023-인천미추홀-0381 호
학원 명칭 : 제이에스코드(JSCODE)원격학원학원설립ㆍ운영 등록번호 : 제6063호

서울특별시 구로구 경인로 20가길 11(오류동, 아델리아)

Copyright ⓒ 2025 JSCODE - 최상위 현업 개발자들의 프로그래밍 교육 All rights reserved.

이용약관개인정보처리방침
← 블로그 목록으로 돌아가기

[실습] 카페 시뮬레이션으로 익히는 Job 구성과 실행 흐름

JSCODE 시니
JSCODE 시니
2026. 04. 04.
author
JSCODE 시니
category
Spring Batch
createdAt
Feb 7, 2026 12:49 AM
isPublic
isPublic
series
Spring Batch 입문: 3시간 만에 끝내는 대용량 처리의 기초
slug
spring-batch-job-flow-simulation
type
post
updatedAt
Apr 4, 2026 10:00

✅ 1. 코드 작성

CafeJobConfig.java
package com.system.batch; @Configuration public class CafeJobConfig { private final JobRepository jobRepository; private final PlatformTransactionManager transactionManager; public CafeJobConfig(JobRepository jobRepository, PlatformTransactionManager transactionManager) { this.jobRepository = jobRepository; this.transactionManager = transactionManager; } private int coffeeCount = 0; // 만든 커피 잔 수 private final int ORDER_TARGET = 5; // 주문 받은 커피 수 (5잔) // 1. 카페 문열기 => openCafeStep // 2. 커피 만들기 (5잔 될 때 까지 반복) => makeCoffeeStep // 3. 마감 청소 및 퇴근 => closeCafeStep @Bean public Job cafeJob(){ return new JobBuilder("cafeJob", jobRepository) .start(openCafeStep()) .next(makeCoffeeStep()) .next(closeCafeStep()) .build(); } @Bean public Step openCafeStep(){ return new StepBuilder("openCafeStep", jobRepository) .tasklet((contribution, chunkContext) -> { System.out.println("[오픈] 카페 문을 열고 머신을 예열 합니다."); coffeeCount = 0; // 해당 배치 재실행시 카운트 초기화 return RepeatStatus.FINISHED; // 준비 끝! 다음단계로~ }, transactionManager) .build(); } @Bean public Step makeCoffeeStep(){ return new StepBuilder("makeCoffeeStep", jobRepository) .tasklet((contribution, chunkContext) -> { coffeeCount++; System.out.println("[제조] 아메리카노 " + coffeeCount + "잔째 완성!"); if(coffeeCount < ORDER_TARGET){ return RepeatStatus.CONTINUABLE; }else{ System.out.println("[완료] 주문하신 커피 " + ORDER_TARGET + "잔 모두 나왔습니다."); return RepeatStatus.FINISHED; } } ,transactionManager) .build(); } @Bean public Step closeCafeStep(){ return new StepBuilder("closeCafeStep", jobRepository) .tasklet((contribution, chunkContext) -> { System.out.println("[마감] 머신을 끄고 퇴근합니다. 오늘도 수고하셨습니다."); return RepeatStatus.FINISHED; } ,transactionManager) .build(); } }
 

✅ 2-1. 배치 실행

# 맥, 리눅스 사용자 ./gradlew bootRun --args='--spring.batch.job.name=cafeJob' # 윈도우 사용자 # PowerShell 사용 시 .\gradlew bootRun --args="--spring.batch.job.name=cafeJob" # CMD 사용 시 gradlew bootRun --args="--spring.batch.job.name=cafeJob"
👩‍💼
운영체제에 따라 배치 명령문이 다릅니다. 자신의 운영체제를 꼭 확인하고 명령문을 실행시켜주세요. 해당 강의자료는 Mac OS를 기준으로 제작되었습니다. 추후 실행 명령문을 잘 확인 후 실습해주시길 바랍니다.
 

✅2-2. H2 데이터베이스 추가

runtimeOnly 'com.h2database:h2'
 

✅ 3. 실행 결과

notion image
 
author
category
Spring Batch
createdAt
Mar 5, 2026 10:56 PM
isPublic
isPublic
series
Spring Batch 입문: 3시간 만에 끝내는 대용량 처리의 기초
slug
type
series-footer
updatedAt
Apr 4, 2026 10:00
📎
이 글은 Spring Batch 입문 강의의 수업 자료 중 일부입니다.