0JUUU / spring-batch

Spring Boot 기반으로 개발하는 Spring Batch
1 stars 0 forks source link

섹션 11. 스프링 배치 반복 및 오류 제어 #15

Open 0JUUU opened 2 years ago

0JUUU commented 2 years ago
0JUUU commented 2 years ago

Repeat

image

  1. Step : RepeatTemplate ➡️ Tasklet 반복적으로 실행
  2. ChunkOrientedTasklet : 내부적으로 ChunkProvider를 통해 ItemReader한테 데이터를 읽어올 것을 지시
  3. ChunkProvider : 내부적으로 RepeatTemplate 가짐 ➡️ 이를 이용해 반복적으로 ItemReader에게 반복적으로 데이터를 읽어오도록 처리

반복 종료 여부를 결정하는 3가지 항목 (3가지 중 하나에 의해 종료됨)

image

@Bean
    public Step step1() throws Exception {
        return stepBuilderFactory.get("step1")
            .<String, String>chunk(5)
            .reader(new ItemReader<String>() {
                int i = 0;

                @Override
                public String read()
                    throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
                    i++;
                    return i > 3 ? null : "item" + i;
                }
            })
            .processor(new ItemProcessor<String, String>() {

                RepeatTemplate repeatTemplate = new RepeatTemplate();

                @Override
                public String process(String item) throws Exception {
                    repeatTemplate.setExceptionHandler(simpleLimitExceptionHandler());  // 몇 번의 예외가 발생하면 종료
                    repeatTemplate.iterate(new RepeatCallback() {
                        int i = 0;

                        @Override
                        public RepeatStatus doInIteration(RepeatContext context) throws Exception {
                            i++;
                            System.out.println("repeatTemplate testing " + i);
                            throw new RuntimeException(">> Exception is occurred");
//                            return RepeatStatus.CONTINUABLE;
                        }
                    });
                    return item;
                }
            })
            .writer(System.out::println)
            .build();
    }
image
0JUUU commented 2 years ago

FaultTolerant

0JUUU commented 2 years ago

Skip

image

image

image image image
0JUUU commented 2 years ago

Retry (1)

image

image

0JUUU commented 2 years ago

Retry (2)

public class RetryItemProcessor implements ItemProcessor<String, String> {

    private int cnt = 0;

    @Override
    public String process(String item) throws Exception {
        if (item.equals("2") || item.equals("3")) {
            cnt++;
            throw new RetryableException("failed cnt : " + cnt);
        }
        return item;
    }
}
0JUUU commented 2 years ago

Retry (3)

RetryTemplate 사용

public class RetryItemProcessor2 implements ItemProcessor<String, Customer> {

    @Autowired
    private RetryTemplate retryTemplate;
    private int cnt = 0;

    @Override
    public Customer process(String item) throws Exception {
        Classifier<Throwable, Boolean> rollbackClassifier = new BinaryExceptionClassifier(true);

        Customer customer = retryTemplate.execute(new RetryCallback<Customer, RuntimeException>() {
            @Override
            public Customer doWithRetry(RetryContext context) throws RuntimeException {
                if (item.equals("1") || item.equals("2")) {
                    cnt++;
                    throw new RetryableException("failed cnt : " + cnt);
                }
                return new Customer(item);
            }
        }, new RecoveryCallback<Customer>() {
            @Override
            public Customer recover(RetryContext context) throws Exception {
                return new Customer(item);
            }
        }, new DefaultRetryState(item, rollbackClassifier));
        return customer;
    }
}
image
0JUUU commented 2 years ago

Skip & Retry 아키텍처

ItemReader

image

ItemProcessor

image

ItemWriter

image