Skip to content

Commit

Permalink
feat: 배치 스케줄러 설정
Browse files Browse the repository at this point in the history
- 매일 3시 15분에 최대 500개의 로그 삭제
  • Loading branch information
eckrin committed May 7, 2024
1 parent 8ffc52c commit 7bdcb45
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Job eventLogClearJob(JobRepository jobRepository, PlatformTransactionMana
@JobScope
public Step eventLogClearStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new StepBuilder(STEP_NAME, jobRepository)
.<EventLog, EventLog>chunk(5, transactionManager) // chunkSize: 몇 개 단위로 데이터를 처리할 것인지 지정. 참고로 파라미터로 받아서 사용할 수는 없다.
.<EventLog, EventLog>chunk(500, transactionManager) // chunkSize: 몇 개 단위로 데이터를 처리할 것인지 지정. 참고로 파라미터로 받아서 사용할 수는 없다.
.reader(eventLogClearReader())
.processor(eventLogClearProcessor())
.writer(eventLogClearWriter())
Expand Down Expand Up @@ -100,9 +100,9 @@ public ItemReader<EventLog> eventLogClearReader() {
.repository(eventLogRepository)
.methodName("findByCreatedAtBefore")
.arguments(List.of(dateTime)) // 의미없는 PageRequest 전달
.pageSize(5) // chunkSize와 일치하게 설정
.pageSize(500) // chunkSize와 일치하게 설정
.sorts(Collections.singletonMap("id", Sort.Direction.ASC))
.build();
.build();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.kusitms.jipbap.event.scheduler;

import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Collections;

/**
* 스케줄러를 통해서 Job 실행
*/
@Component
@RequiredArgsConstructor
public class ClearJobScheduler {

private final Job eventLogClearJob;
private final JobLauncher jobLauncher; // 스케줄링을 활용하여 Job 실행

@Scheduled(cron = "0 3 15 * * *") // 매일 특정 시간(3시 15분)에 실행
public void testJobRun() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {

JobParameters jobParameters = new JobParameters(
Collections.singletonMap("requestTime", new JobParameter(System.currentTimeMillis(), Long.class))
);

jobLauncher.run(eventLogClearJob, jobParameters);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ spring:
initialize-schema: always
job:
name: eventLogClearJob
# enabled: false # Job 실행을 스케줄러에게 위임하기 위하여 설정
enabled: false # Job 실행을 스케줄러에게 위임하기 위하여 설정

cloud:
aws:
Expand Down

0 comments on commit 7bdcb45

Please sign in to comment.