programming/Spring Batch
Spring Batch) JobParameter 주입
코딩하는 핑가
2023. 12. 29. 10:04
반응형
- JobParameters : JobInstance ( 1:1 )
- 주입 방법
- 어플리케이션 실행 시 주입
- Java -jar TestBatch.jar requestDate=20231114
- 코드로 생성
- JobParameterBuilder, DefaultJobParametersConverter
- 주로 JobParameterBuilder 사용
- JobParameterBuilder, DefaultJobParametersConverter
- SpEL ( Spring Expression Language )
- @Value(“#{jobParameter[requestDate]}“), @JobScope, @StepScope 선언 필수
- 어플리케이션 실행 시 주입
- 예시
-
@Bean public Step step4() { return (Step) this.stepBuilderFactory.get("step4").tasklet((contribution, chunkContext) -> { StepLogger stepLogger = StepLogger.getLogger("importUserJob", "step4"); JobLogger jobLogger = JobLogger.getLogger("importUserJob"); stepLogger.info(">>>>> This is Step4"); jobLogger.info(">>>>> This is Step4"); JobParameters jobParams = contribution.getStepExecution().getJobExecution().getJobParameters(); jobParams.getString("ename"); stepLogger.info("jobParams >> " + jobParams.getString("ename")); Map<String, Object> jobParams2 = chunkContext.getStepContext().getJobParameters(); jobParams2.get("ename"); stepLogger.info("jobParams >> " + jobParams2.get("ename")); return RepeatStatus.FINISHED; }).build(); }
-
- 엑셀 파일 읽어서 Spring Batch 실행
-
// reader, writer @Bean public FlatFileItemReader<Emp> empReader() { logger.info("TestSpringBatchConfiguration empReader start "); return new FlatFileItemReaderBuilder<Emp>().name("empItemReader") .resource(new ClassPathResource("spring/sample-data2.csv")).delimited() .names(new String[] { "empno", "ename" }).fieldSetMapper(new BeanWrapperFieldSetMapper<Emp>() { { setTargetType(Emp.class); } }).build(); } @Bean public EmpItemProcessor empProcessor() { logger.info("TestSpringBatchConfiguration empProcessor start "); return new EmpItemProcessor(); } @Bean public JdbcBatchItemWriter<Emp> empWriter(@Qualifier("springBatchTestDataSource") DataSource dataSource) { logger.info("TestSpringBatchConfiguration empWriter start "); return new JdbcBatchItemWriterBuilder<Emp>() .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>()) .sql("INSERT INTO EMP (EMPNO, ENAME) VALUES (:empno, :ename)").dataSource(dataSource).build(); } // job @Bean public Job SampleEmpJob2(TestJobCompletionNotificationListener empListener, Step step0) { jobLogger.info("SampleEmpJob2 start "); return empJobRepository.get("SampleEmpJob2").incrementer(new RunIdIncrementer()).listener(empListener) .flow(step0).next(step2()).next(empStep4()).next(empStep5()).next(empStep6()).next(empStep7()).end().build(); } //step @Bean public Step step0(JdbcBatchItemWriter<Emp> empWriter) throws InterruptedException { step0Logger.info("step0 start "); return stepBuilderFactory.get("SampleEmpJob_step_0").<Emp, Emp>chunk(1).reader(empReader()) .processor(empProcessor()).writer(empWriter).build(); }
-
- JobParameter 받아서 step 실행
-
// step @Bean public Step step4() { return (Step) this.stepBuilderFactory.get("step4").tasklet((contribution, chunkContext) -> { StepLogger stepLogger = StepLogger.getLogger("importUserJob", "step4"); JobLogger jobLogger = JobLogger.getLogger("importUserJob"); stepLogger.info(">>>>> This is Step4"); jobLogger.info(">>>>> This is Step4"); JobParameters jobParams = contribution.getStepExecution().getJobExecution().getJobParameters(); jobParams.getString("ename"); stepLogger.info("jobParams >> " + jobParams.getString("ename")); Map<String, Object> jobParams2 = chunkContext.getStepContext().getJobParameters(); jobParams2.get("ename"); stepLogger.info("jobParams >> " + jobParams2.get("ename")); return RepeatStatus.FINISHED; }).build(); }
-
반응형