아래 포스팅에서 이어진 내용입니다.
[springBoot] spring batch allowStartIfComplete
목차 아래 포스팅에서 이어진 내용입니다. [springBoot] spring batch preventRestart option 목차 아래 포스팅에서 이어진 내용입니다. [springBoot] spring batch scheduler modularization (feat. DB) 목차 아래 포스팅에서...
yaga.tistory.com
![[springBoot] spring batch JobScope, StepScope(테스트 영상 & 소스코드 포함) [springBoot] spring batch JobScope, StepScope(테스트 영상 & 소스코드 포함)](https://blog.kakaocdn.net/dn/bQ65Zs/btsAmzm7yxI/4Px0Z97exkk8OKNi0UdJq0/img.png)
@JobScope와 StepScope를 이용해서 외부에서 전달한 jobParameters를 받아서 로그 출력하는 예제
JobTestConfig
package com.dev.lsy.springbatchremind.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.JobScope;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
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.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Slf4j
@RequiredArgsConstructor
@Configuration
public class JobTestConfig {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job job1() {
return jobBuilderFactory.get("job1")
.start(step1(null))
.build();
}
@Bean
@JobScope
//SpEL
public Step step1(@Value("#{jobParameters[message]}") String message) {
log.info("step1 =====> SpEL 이용");
log.info("message ==> [{}]", message);
return stepBuilderFactory.get("step1")
.tasklet(tasklet1())
.build();
}
@Bean
// @StepScope
//SPL로 가져오는 방법
public Tasklet tasklet1() {
return ((contribution, chunkContext) -> {
log.info("tasklet =====> context 직접 접근");
log.info("message ==> [{}]", chunkContext.getStepContext().getStepExecution().getJobParameters().getString("message"));
return RepeatStatus.FINISHED;
});
}
}
@JobScope, StepScope 추가 설명
@JobScope나 @StepScope가 있는 @Bean 메소드는 애플리케이션 구동 시점에는 프록시 객체로 등록되었다가 해당 메소드가 실행되는 시점에 실제 빈으로 등록되고 jobParameters 데이터도 이 때 바인딩 된다. 심플하게 생각해서 SpEL(Spring Expression Language)로 jobParameters를 받으려면 @JobScope나 @StepScope를 반드시 선언하자.
어노테이션 | 선언부 | |||
@JobScope | Step | |||
@StepScope | Tasklet | ItemReader | ItemWriter | ItemProcessor |
개인 스터디 기록을 메모하는 공간이라 틀린점이 있을 수 있습니다.
틀린 점 있을 경우 댓글 부탁드립니다.
reference: https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%EB%B0%B0%EC%B9%98
다음 내용
[springBoot] spring batch step startLimit
아래 포스팅에서 이어진 내용입니다. [springBoot] spring batch JobScope, StepScope 목차 아래 포스팅에서 이어진 내용입니다. [springBoot] spring batch allowStartIfComplete 목차 아래 포스팅에서 이어진 내용입니다....
yaga.tistory.com
'IT > Live Coding' 카테고리의 다른 글
[springBoot] spring batch FlatFileItemWriter(테스트 영상 & 소스코드 포함) (55) | 2023.11.16 |
---|---|
[springBoot] spring batch step startLimit(테스트 영상 & 소스코드 포함) (54) | 2023.11.16 |
[springBoot] spring batch allowStartIfComplete(테스트 영상 & 소스코드 포함) (53) | 2023.11.15 |
[springBoot] spring batch preventRestart option(테스트 영상 & 소스코드 포함) (50) | 2023.11.14 |
[springBoot] spring batch scheduler modularization (테스트 영상 & 소스코드 포함) (49) | 2023.11.13 |
댓글