Spring Quartz 를 사용하라는데 맞는지 모르겠어요 >> 튜터님한테 질문하고 싶어요 “지훈”
https://stackoverflow.com/questions/33188368/spring-batch-vs-quartz-jobs
https://stackoverflow.com/questions/13743864/scheduling-jobs-using-spring-batch-or-just-quartz-scheduler
Quartz is a scheduling framework. Like "execute something every hour or every last Friday of the month"
Spring Batch is a framework that defines that "something" that will be executed. You can define a job, that consists of steps. Usually, a step is something that consists of an item reader, an optional item processor, and an item writer, but you can define a custom step. You can also tell Spring Batch to commit every 10 items and a lot of other stuff.
동시성 처리를 방지 처리 이렇게 하는 게 맞을까 싶어요 >> 튜터님한테 질문하고 싶어요 “지훈”
REDIS로 분산락 걸고, 배치 작업 이후 락 해제
일단 Spring Batch로 구현
참고자료
https://medium.com/hprog99/mastering-job-scheduling-in-spring-boot-from-basics-to-best-practices-74ab938d80fa
@Component
@RequiredArgsConstructor
@Slf4j
public class BuyDeadlineScheduler {
private final BuyService buyService;
private final StringRedisTemplate redisTemplate;
@Retryable(
retryFor = RuntimeException.class,
maxAttempts = 3,
backoff = @Backoff(delay = 2000)
)
@Scheduled(cron = "59 59 23 * * ?")
public void closeBuyOfPassedDealine() {
Boolean hasLock = redisTemplate.opsForValue().setIfAbsent("CLOSE_BUY_SCHEDULE_LOCK", "LOCKED", 2, TimeUnit.SECONDS);
if (hasLock) {
buyService.deleteAllBuyBidsOfOutDatedFrom(LocalDateTime.now());
redisTemplate.delete("CLOSE_BUY_SCHEDULE_LOCK");
}
}
@Recover
void recover(RuntimeException exception) {
log.error("Exception Message : " + exception.getMessage());
log.error("Exception Stack Trace : " + Arrays.toString(exception.getCause().getStackTrace()));
}
}
Quartz를 이용한 구현 << DataSource를 지정해줘야하는 이슈가 있음
참고자료
https://velog.io/@park2348190/Spring-Boot-환경의-Quartz-Scheduler-활용