[SpringBatch 실습] 22. ItemReader, ItemProcessor, ItemWriter의 메서드 직접 구현해보기

반응형
728x90
반응형

Job 생성

ItemConfiguration.java
import com.project.springbatch._40_item.custom.CustomItemProcessor;
import com.project.springbatch._40_item.custom.CustomItemReader;
import com.project.springbatch._40_item.custom.CustomItemWriter;
import com.project.springbatch._40_item.custom.Customer;
import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/*
--job.name=itemJob
 */
@Configuration
@RequiredArgsConstructor
public class ItemConfiguration {
    // job 생성
    private final JobBuilderFactory jobBuilderFactory;
    private final StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job itemJob() {
        return this.jobBuilderFactory.get("itemJob")
                /* step start */
                .start(itemStep1())
                .next(itemStep2())
                .build();
    }

    @Bean
    public Step itemStep1() {
        return stepBuilderFactory.get("itemStep1")
                .<Customer, Customer>chunk(3)
                .reader(new CustomItemReader(Arrays.asList(
                        new Customer("user1"),
                        new Customer("user2"),
                        new Customer("user3"))))
                .processor(new CustomItemProcessor())
                .writer(new CustomItemWriter())
                .build();
    }

    @Bean
    public Step itemStep2() {
        return stepBuilderFactory.get("itemStep2")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("chunkJobConfiguration step2 was executed");
                        return RepeatStatus.FINISHED;
                    }
                })
                .build();
    }
}

 

Customer.java
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Customer {
    private String name;
}

 

 

ItemReader

CustomItemReader.java
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 java.util.ArrayList;
import java.util.List;

public class CustomItemReader implements ItemReader<Customer> {
    private List<Customer> list;

    public CustomItemReader(List<Customer> list) {
        this.list = new ArrayList<>(list);
    }

    @Override
    public Customer read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        if (!list.isEmpty()) {
            return list.remove(0); // 1개씩 읽을때마다 데이터를 삭제
        }

        return null; // null 을 반환하면 더이상 읽을 아이템이 없다는 것으로 설정됨
    }
}

1) ItemReader 구현

public class CustomItemReader implements ItemReader<Customer> {

 

2) read() 메서드

데이터를 read() 할때마다 리스트의 데이터를 제거한다.

if (!list.isEmpty()) {
    return list.remove(0); // 1개씩 읽을때마다 데이터를 삭제
}

 

 

ItemProcessor

CustomItemProcessor.java
import org.springframework.batch.item.ItemProcessor;

import java.util.Locale;

public class CustomItemProcessor implements ItemProcessor<Customer, Customer> {
    @Override
    public Customer process(Customer customer) throws Exception {
        customer.setName(customer.getName().toUpperCase(Locale.ROOT));
        return customer;
    }
}

1) ItemProcessor 구현

public class CustomItemProcessor implements ItemProcessor<Customer, Customer> {

 

2) process() 메서드

문자를 대문자로 변환한다.

customer.setName(customer.getName().toUpperCase(Locale.ROOT));

 

 

ItemWriter

CustomItemWriter.java
import org.springframework.batch.item.ItemWriter;

import java.util.List;

public class CustomItemWriter implements ItemWriter<Customer> {

    @Override
    public void write(List<? extends Customer> items) throws Exception {
        items.forEach(System.out::println);
    }
}

1) ItemWriter 구현

public class CustomItemWriter implements ItemWriter<Customer> {

 

2) write() 메서드

List 타입의 items 원소를 모두 출력한다.

items.forEach(System.out::println);

 

 

 

실행결과

Customer(name=USER1)
Customer(name=USER2)
Customer(name=USER3)
chunkJobConfiguration step2 was executed

 

 

DB 테이블 조회

1) BATCH_JOB_EXECUTION

COLUMN VALUE
STATUS COMPLETED
EXIT_CODE COMPLETED
JOB_EXECUTION_ID 38

 

2) BATCH_JOB_INSTANCE

COLUMN VALUE
JOB_NAME itemJob

 

3) BATCH_STEP_EXECUTION

  • itemStep1
COLUMN VALUE
STEP_NAME itemStep1
EXIT_CODE COMPLETED
STATUS COMPLETED
JOB_EXECUTION_ID 38

 

  • itemStep2
COLUMN VALUE
STEP_NAME itemStep2
EXIT_CODE COMPLETED
STATUS COMPLETED
JOB_EXECUTION_ID 38

 

 

반응형

Designed by JB FACTORY