From 047fb86aac8bddc1512822f2e3d13ec12b605f3f Mon Sep 17 00:00:00 2001 From: wellbeing-dough Date: Sat, 9 Dec 2023 00:11:59 +0900 Subject: [PATCH] =?UTF-8?q?[Feat]=20:=20=EB=B0=B0=ED=8F=AC=ED=99=98?= =?UTF-8?q?=EA=B2=BD=20Batch=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 5 ++ .../StudyHubServerApplication.java | 4 ++ .../config/StudyPostJobConfig.java | 57 +++++++++++++++++++ .../scheduler/StudyPostScheduler.java | 39 +++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 src/main/java/kr/co/studyhubinu/studyhubserver/config/StudyPostJobConfig.java create mode 100644 src/main/java/kr/co/studyhubinu/studyhubserver/studypost/scheduler/StudyPostScheduler.java diff --git a/build.gradle b/build.gradle index 48f8b97c..c41df3b8 100644 --- a/build.gradle +++ b/build.gradle @@ -42,6 +42,7 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' + // swagger implementation 'io.springfox:springfox-boot-starter:3.0.0' implementation 'io.springfox:springfox-swagger-ui:3.0.0' @@ -61,6 +62,10 @@ dependencies { implementation 'com.querydsl:querydsl-jpa' implementation 'com.querydsl:querydsl-apt' + //Spring Batch + implementation 'org.springframework.boot:spring-boot-starter-batch' + testImplementation 'org.springframework.batch:spring-batch-test' + } //querydsl 추가 diff --git a/src/main/java/kr/co/studyhubinu/studyhubserver/StudyHubServerApplication.java b/src/main/java/kr/co/studyhubinu/studyhubserver/StudyHubServerApplication.java index 10a356c3..9dcc4c79 100644 --- a/src/main/java/kr/co/studyhubinu/studyhubserver/StudyHubServerApplication.java +++ b/src/main/java/kr/co/studyhubinu/studyhubserver/StudyHubServerApplication.java @@ -1,11 +1,15 @@ package kr.co.studyhubinu.studyhubserver; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableCaching +@EnableBatchProcessing +@EnableScheduling public class StudyHubServerApplication { public static void main(String[] args) { diff --git a/src/main/java/kr/co/studyhubinu/studyhubserver/config/StudyPostJobConfig.java b/src/main/java/kr/co/studyhubinu/studyhubserver/config/StudyPostJobConfig.java new file mode 100644 index 00000000..83c9b585 --- /dev/null +++ b/src/main/java/kr/co/studyhubinu/studyhubserver/config/StudyPostJobConfig.java @@ -0,0 +1,57 @@ +package kr.co.studyhubinu.studyhubserver.config; + +import kr.co.studyhubinu.studyhubserver.studypost.repository.StudyPostRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.*; +import org.springframework.batch.core.launch.support.RunIdIncrementer; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.RepeatStatus; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Slf4j +@Configuration +@EnableBatchProcessing +@RequiredArgsConstructor +public class StudyPostJobConfig { + + public final JobBuilderFactory jobBuilderFactory; + + public final StepBuilderFactory stepBuilderFactory; + + private final StudyPostRepository studyPostRepository; + + @Bean("studyPostJob") + public Job studyPostJob() { // 빌더로 초기화하는 과정 + return jobBuilderFactory.get("studyPostJob") + .incrementer(new RunIdIncrementer()) + .start(changeStudyPostCloseByDeadline()) + .on("FAILED") + .stopAndRestart(changeStudyPostCloseByDeadline()) + .on("*") + .end() + .end() + .build(); + } + + @JobScope + @Bean("changeStudyPostCloseByDeadline") + public Step changeStudyPostCloseByDeadline() { + return stepBuilderFactory.get("changeBoardStatus") + .tasklet(studyPostTasklet()) + .build(); + } + + @StepScope + @Bean("studyPostTasklet") + public Tasklet studyPostTasklet() { + return (contribution, chunkContext) -> { + log.info("***********실행중 실행중 실행중**************"); + return RepeatStatus.FINISHED; + }; + } + +} \ No newline at end of file diff --git a/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/scheduler/StudyPostScheduler.java b/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/scheduler/StudyPostScheduler.java new file mode 100644 index 00000000..f3d9dfbe --- /dev/null +++ b/src/main/java/kr/co/studyhubinu/studyhubserver/studypost/scheduler/StudyPostScheduler.java @@ -0,0 +1,39 @@ +package kr.co.studyhubinu.studyhubserver.studypost.scheduler; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobParametersBuilder; +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.time.LocalDateTime; + +@Component +@Slf4j +@RequiredArgsConstructor +public class StudyPostScheduler { + + private final JobLauncher jobLauncher; + private final Job job; + + @Scheduled(cron = "0 * * * * *") + public void runJob() { + + try{ + jobLauncher.run( + job, new JobParametersBuilder().addString("dateTime", LocalDateTime.now().toString()).toJobParameters() + ); + } catch (JobExecutionAlreadyRunningException | JobInstanceAlreadyCompleteException | + JobParametersInvalidException | JobRestartException e) { + log.error(e.getMessage()); + } catch (Exception e) { + log.error(e.getMessage()); + } + } +}