반응형
목차
아래 포스팅에서 이어진 내용입니다.
10초에 한번 씩 로그를 출력하는 아주 간단한 배치 스케줄러 예제
Batch1 🙂
package com.dev.lsy.springbatchlog.batch;
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.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Slf4j
@RequiredArgsConstructor
@Configuration
public class Batch1 {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
//batchjob여기서는 batchStep을 실행, 실제 배치 로직은 step에서 수행
public Job batchjob() {
return jobBuilderFactory.get("batchjob")
.start(batchStep())
.build();
}
@Bean
//정말 간단한 로그 출력
public Step batchStep() {
return stepBuilderFactory.get("batchStep")
.tasklet(((contribution, chunkContext) -> {
log.debug("insert Batch start~~~~~");
return RepeatStatus.FINISHED;
}))
.build();
}
}
Scheduler 😄
package com.dev.lsy.springbatchlog.scheduler;
import com.dev.lsy.springbatchlog.batch.Batch1;
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.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component
@RequiredArgsConstructor
@EnableScheduling
@Slf4j
public class Scheduler {
private final JobLauncher jobLauncher;
private final Job batch1;
@Scheduled(fixedRate = 10000)
//스케줄러에서 batch1잡을 실행
public void schedule() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
jobLauncher.run(batch1, new JobParametersBuilder()
.addString("date", LocalDateTime.now().toString())
.toJobParameters());
}
}
최초 배치 프로젝트 생성 시 메타 테이블도 만들어야 한다.
동영상에서는 기존 메타 테이블을 재사용 해 에러가 발생하지 않았다.
Spring Community: Streamlining Batch Processing, Thanks Open Source
더보기
Thank you to the Spring community and the open-source developers who have made it easy to assemble and integrate batch processing and scheduling in Spring. Your contributions have greatly simplified the development process, and we appreciate your efforts.
개인 스터디 기록을 메모하는 공간이라 틀린점이 있을 수 있습니다.
틀린 점 있을 경우 댓글 부탁드립니다.
다음 내용
반응형
'IT > development' 카테고리의 다른 글
[springBoot] spring batch scheduler modularization (feat. DB) (49) | 2023.11.13 |
---|---|
[springBoot] spring batch scheduler jpaRead/Writer (feat. DB) (54) | 2023.11.12 |
[springBoot] spring batch JsonReader Filter Write (feat. JSON) (52) | 2023.11.11 |
[springBoot] spring batch JsonReader logPrint (feat. JSON) (52) | 2023.11.11 |
[springBoot] spring batch csvFileReader write new File (feat. file) (53) | 2023.11.11 |