-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
118bced
commit 047fb86
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/java/kr/co/studyhubinu/studyhubserver/StudyHubServerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/kr/co/studyhubinu/studyhubserver/config/StudyPostJobConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/kr/co/studyhubinu/studyhubserver/studypost/scheduler/StudyPostScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} | ||
} |