ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 21. ItemWriterAdapter
    BackEnd/Spring Batch 2021. 12. 31. 18:45
    반응형

      배치 Job 안에서 이미 존재하는 DAO나 다른 서비스를 ItemWriter 안에서 사용하고자 할 때 위임 역할을 합니다. setTargetObject에 존재하는 서비스를 setTargetMethod에 호출하려는 메서드명을 적용합니다.

    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.ItemReader;
    import org.springframework.batch.item.NonTransientResourceException;
    import org.springframework.batch.item.ParseException;
    import org.springframework.batch.item.UnexpectedInputException;
    import org.springframework.batch.item.adapter.ItemWriterAdapter;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @RequiredArgsConstructor
    @Configuration
    public class ItemWriterAdapterConfiguration {
    
        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" + i;
                        }
                    })
                    .writer(customItemWriter())
                    .build();
        }
    
    
    
        @Bean
        public ItemWriterAdapter customItemWriter() {
    
            ItemWriterAdapter<String>  writer = new ItemWriterAdapter<>();
             writer.setTargetObject(customService());
             writer.setTargetMethod("joinCustomer");
            return  writer;
        }
    
        @Bean
        public CustomService customService() {
            return new CustomService();
        }
    }

    CustomService.java

    package io.springbatch.springbatchlecture;
    
    public class CustomService<T> {
    
        public void customWrite(T item){
    
            System.out.println(item);
        }
    }

     

    [참고자료]

    인프런-스프링 배치 - Spring Boot 기반으로 개발하는 Spring Batch

    반응형

    'BackEnd > Spring Batch' 카테고리의 다른 글

    23. Repeat  (0) 2022.01.04
    22. ItemProcessor  (0) 2022.01.04
    20. ItemWriter (DB)  (0) 2021.12.31
    19. ItemWriter (Json)  (0) 2021.12.31
    18. ItemWriter (XML)  (0) 2021.12.31

    댓글

Designed by Tistory.