【问题标题】:How to catch Spring Batch's ItemStreamException after @Retryable maxAttempts exceeded?@Retryable maxAttempts 超出后如何捕获 Spring Batch 的 ItemStreamException?
【发布时间】:2016-08-25 15:05:31
【问题描述】:

我有以下ItemReader

import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.LineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

@Service
public class MyReader extends FlatFileItemReader<Holding> {

    @Autowired
    public MyReader(LineMapper<Holding> lineMapper, File loadFile) {
        setResource(new FileSystemResource(loadFile));
        final int NUMBER_OF_HEADER_LINES = 1;
        setLinesToSkip(NUMBER_OF_HEADER_LINES);
        setLineMapper(lineMapper);
    }

    @Override
    @Retryable(value=ItemStreamException.class, maxAttempts=5,  backoff=@Backoff(delay=1800000))
    public void open(ExecutionContext executionContext) throws ItemStreamException {
                super.open(executionContext);
    }
}

在第 5 次尝试后,如果 loadFile 不存在,则会引发 ItemStreamException。我怎样才能捕捉到这个异常?

我想通过向管理员发送电子邮件来处理此异常。我试过用 try catch 子句包围jobLauncher.run(job, jobParameters);,但它不起作用。

【问题讨论】:

    标签: spring spring-batch spring-retry


    【解决方案1】:

    将重试拦截器配置为bean,并在interceptor属性中指定;有一个RetryInterceptorBuilder 可以帮助组装 bean。

    你可以给拦截器添加一个recoverer,在重试结束后调用。

    【讨论】:

      【解决方案2】:

      您可以为您的阅读器应用可跳过的异常类,这样 Spring Batch 就不会失败。

      为了在失败的情况下发送电子邮件,我建议你使用 Job Listener 而不是 try/catch。

      安吉亚

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-25
        • 2019-11-03
        • 1970-01-01
        • 2019-01-29
        • 2023-03-15
        • 2021-12-30
        • 1970-01-01
        • 2022-12-19
        相关资源
        最近更新 更多