개발 기록

quartz job parameters processor에서 사용 본문

TIL

quartz job parameters processor에서 사용

수염차 2024. 2. 22. 14:33

job

@Log4j2
@DisallowConcurrentExecution
@Component
public class TestJob extends QuartzJobBean {

    @Autowired
    private JobLauncher jobLauncher;
    @Qualifier("TestJob")
    @Autowired
    private Job job;

    @Override
    protected void executeInternal(JobExecutionContext context) {
        try {
            log.info("[TestJob Job 실행]");

            JobParameters jobParameter = new JobParametersBuilder()
                    .addString("type", "Test")
                    .toJobParameters();

            jobLauncher.run(job, jobParameter);
        } catch (JobInstanceAlreadyCompleteException | JobRestartException |
                 JobParametersInvalidException | JobExecutionAlreadyRunningException e) {
            log.error(e);
        }
    }
}

 

spring batch 

configuration

    @Bean
    @StepScope
    public TargetItemProcessor processor(@Value("#{jobParameters['type']}") String type) {
        return new TargetItemProcessor(type);
    }

 

processor

@StepScope
public class TargetItemProcessor implements ItemProcessor<Map<String,Object>, Map<String,Object>> {

    private static final Logger log = LoggerFactory.getLogger(TargetItemProcessor.class);

    public TargetItemProcessor(String type) {
        this.type = type;
    }

    @Value("#{jobParameters['myParameter']}")
    private String type;

    @Override
    public Map<String,Object> process(final Map<String,Object> map) throws Exception {

        log.info("type = {}", type);

        return map;
    }
}

 

 

https://stackoverflow.com/questions/73478318/access-job-parameter-in-custom-itemprocessor

 

Access Job Parameter in Custom ItemProcessor

I am implementing a custom ItemProcessor<I, O> in spring batch for processing data from a Rest api . I want access some values from jobParameter inside my ItemProcessor class . Any suggestion...

stackoverflow.com

 

Comments