복붙노트

[SPRING] Spring Batch - TaskletStep에서 건너 뛸 수있는 예외

SPRING

Spring Batch - TaskletStep에서 건너 뛸 수있는 예외

특정 예외가 발생하면 BatchStatus.FAILED가 발생하지 않도록 작업을 시도하고 있습니다.

docs는 내에서 skippable-exception-classes를 사용하는 것에 대해 이야기하지만 TaskletStep 내에서 어떻게 동일한 작업을 수행 할 수 있습니까? 아래 코드는 작동하지 않습니다.

<batch:step id="sendEmailStep">
    <batch:tasklet>
        <bean class="com.myproject.SendEmail" scope="step" autowire="byType">
            <batch:skippable-exception-classes>
                <batch:include class="org.springframework.mail.MailException" />
            </batch:skippable-exception-classes>
        </bean>
    </batch:tasklet>
</batch:step>

해결법

  1. ==============================

    1.Michael Minella가 제안한대로 Tasklet에서이 기능을 구현했습니다.

    Michael Minella가 제안한대로 Tasklet에서이 기능을 구현했습니다.

    abstract class SkippableTasklet implements Tasklet {
    
        //Exceptions that should not cause job status to be BatchStatus.FAILED
        private List<Class<?>> skippableExceptions;
    
        public void setSkippableExceptions(List<Class<?>> skippableExceptions) {
            this.skippableExceptions = skippableExceptions;
        }
    
        private boolean isSkippable(Exception e) {
            if (skippableExceptions == null) {
                return false;
            }
    
            for (Class<?> c : skippableExceptions) {
                if (e.getClass().isAssignableFrom(c)) {
                    return true;
                }
            }
            return true;
        }
    
        protected abstract void run(JobParameters jobParameters) throws Exception;
    
        @Override
        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
                throws Exception {
    
            StepExecution stepExecution = chunkContext.getStepContext().getStepExecution();
            JobExecution jobExecution = stepExecution.getJobExecution();
            JobParameters jobParameters = jobExecution.getJobParameters();
    
            try {
                run(prj);
            } catch (Exception e) {
                if (!isSkippable(e)) {
                    throw e;
                } else {
                    jobExecution.addFailureException(e);
                }
            }
    
            return RepeatStatus.FINISHED;
        }
    }
    

    SkippableTasklet 예제를위한 Spring XML 설정 :

    <batch:tasklet>
        <bean class="com.MySkippableTasklet" scope="step" autowire="byType">
            <property name="skippableExceptions">
                <list>
                    <value>org.springframework.mail.MailException</value>
                </list>
            </property>
        </bean>
    </batch:tasklet>
    
  2. ==============================

    2.Tasklet 내에서 예외 처리에 대한 책임은 Tasklet 구현에 있습니다. 청크 지향 처리에서 사용할 수있는 스킵 논리는 ChunkOrientedTasklet에서 제공하는 예외 처리로 인해 발생합니다. 자신의 Tasklet 구현에서 예외를 건너 뛰려면 자체 구현 내에서 예외를 수행하는 코드를 작성해야합니다.

    Tasklet 내에서 예외 처리에 대한 책임은 Tasklet 구현에 있습니다. 청크 지향 처리에서 사용할 수있는 스킵 논리는 ChunkOrientedTasklet에서 제공하는 예외 처리로 인해 발생합니다. 자신의 Tasklet 구현에서 예외를 건너 뛰려면 자체 구현 내에서 예외를 수행하는 코드를 작성해야합니다.

  3. from https://stackoverflow.com/questions/27509585/spring-batch-skippable-exception-in-a-taskletstep by cc-by-sa and MIT license