Skip to content

Commit

Permalink
[Feat] : 배포환경 Batch 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
wellbeing-dough committed Dec 8, 2023
1 parent 118bced commit 047fb86
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 추가
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
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;
};
}

}
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());
}
}
}

0 comments on commit 047fb86

Please sign in to comment.