-
22. ItemProcessorBackEnd/Spring Batch 2022. 1. 4. 21:25반응형
CompositeItemProcessor
ItemProcessor들을 연결(Chaining)해서 위임하면 각 ItemProcessor를 실행시킵니다. 이전 ItemProcessor 반환 값은 다음 ItemProcessor 값으로 연결됩니다.
API
package io.springbatch.springbatchlecture; import lombok.RequiredArgsConstructor; import org.springframework.batch.core.*; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.launch.support.RunIdIncrementer; import org.springframework.batch.item.*; import org.springframework.batch.item.adapter.ItemWriterAdapter; import org.springframework.batch.item.support.CompositeItemProcessor; import org.springframework.batch.item.support.builder.CompositeItemProcessorBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; import java.util.List; @RequiredArgsConstructor @Configuration public class CompositionItemConfiguration { private final JobBuilderFactory jobBuilderFactory; private final StepBuilderFactory stepBuilderFactory; @Bean public Job job() throws Exception { return jobBuilderFactory.get("batchJob") .incrementer(new RunIdIncrementer()) .start(step1()) .build(); } @Bean public Step step1() throws Exception { return stepBuilderFactory.get("step1") .<String, String>chunk(10) .reader(new ItemReader<String>() { int i = 0; @Override public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException { i++; return i > 10 ? null : "item"; } }) .processor(customItemProcessor()) .writer(new ItemWriter<String>() { @Override public void write(List<? extends String> items) throws Exception { System.out.println(items); } }) .build(); } @Bean public CompositeItemProcessor customItemProcessor() { CompositeItemProcessor<String,String> compositeProcessor = new CompositeItemProcessor<>(); List itemProcessors = new ArrayList(); itemProcessors.add(new CustomItemProcessor1()); itemProcessors.add(new CustomItemProcessor2()); return new CompositeItemProcessorBuilder<>() .delegates(itemProcessors) .build(); } }
ClassifierCompositeItemProcessor
Classifier로 라우팅 패턴을 구현해서 ItemProcessor 구현체 중에서 하나를 호출하는 역할을 합니다.
API
package io.springbatch.springbatchlecture; import lombok.RequiredArgsConstructor; import org.springframework.batch.core.*; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.launch.support.RunIdIncrementer; import org.springframework.batch.item.*; import org.springframework.batch.item.adapter.ItemWriterAdapter; import org.springframework.batch.item.support.ClassifierCompositeItemProcessor; import org.springframework.batch.item.support.CompositeItemProcessor; import org.springframework.batch.item.support.builder.CompositeItemProcessorBuilder; import org.springframework.classify.PatternMatchingClassifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @RequiredArgsConstructor @Configuration public class ClassifierConfiguration { private final JobBuilderFactory jobBuilderFactory; private final StepBuilderFactory stepBuilderFactory; @Bean public Job job() throws Exception { return jobBuilderFactory.get("batchJob") .incrementer(new RunIdIncrementer()) .start(step1()) .build(); } @Bean public Step step1() throws Exception { return stepBuilderFactory.get("step1") .<ProcessorInfo, ProcessorInfo>chunk(10) .reader(new ItemReader<ProcessorInfo>() { int i = 0; @Override public ProcessorInfo read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException { i++; ProcessorInfo processorInfo = ProcessorInfo.builder().id(i).build(); return i > 3 ? null : processorInfo; } }) .processor(customItemProcessor()) .writer(new ItemWriter<ProcessorInfo>() { @Override public void write(List<? extends ProcessorInfo> items) throws Exception { System.out.println(items); } }) .build(); } @Bean public ItemProcessor customItemProcessor() { ClassifierCompositeItemProcessor<ProcessorInfo, ProcessorInfo> processor = new ClassifierCompositeItemProcessor<>(); ProcessorClassifier<ProcessorInfo, ItemProcessor<?, ? extends ProcessorInfo>> classifier = new ProcessorClassifier(); Map<Integer, ItemProcessor<ProcessorInfo, ProcessorInfo>> processorMap = new HashMap<>(); processorMap.put(1, new CustomItemProcessor1()); processorMap.put(2, new CustomItemProcessor2()); processorMap.put(3, new CustomItemProcessor3()); classifier.setProcessorMap(processorMap); processor.setClassifier(classifier); return processor; } }
[참고자료]
반응형'BackEnd > Spring Batch' 카테고리의 다른 글
24. FaultTolerant (0) 2022.01.04 23. Repeat (0) 2022.01.04 21. ItemWriterAdapter (0) 2021.12.31 20. ItemWriter (DB) (0) 2021.12.31 19. ItemWriter (Json) (0) 2021.12.31