Spring Batch-将“ onSkipInRead”可抛出消息保存在batch_step_execution表的EXIT_MESSAGE中

时间:2019-01-30 10:11:16

标签: spring-boot spring-batch

我希望在读取过程中跳过哪些记录,并将其插入batch_step_execution的“ EXIT_MESSAGE”中。因此,这是我的“ SkipListener”类。

public class IntroductionSkipListener {

private static final Logger LOG = Logger.getLogger(IntroductionSkipListener.class);

@OnSkipInRead
public void onSkipInRead(Throwable t) {
    LOG.error("Item was skipped in read due to: " + t.getMessage());
}

@OnSkipInWrite
public void onSkipInWrite(Introduction item, Throwable t) {
    LOG.error("Item " + item + " was skipped in write due to : " + t.getMessage());
}

@OnSkipInProcess
public void onSkipInProcess(Introduction item, Throwable t) {
    LOG.error("Item " + item + " was skipped in process due to: " + t.getMessage());

}

我希望在表中保存以下可抛出消息。

    2019-01-30 15:37:53.339 ERROR 10732 --- [nio-8080-exec-1] c.s.a.b.config.IntroductionSkipListener  : Item was skipped in read due to: Parsing error at line: 2 in resource=[URL [file:E:/survey-data-repo/Web/Source_Code/survey_v0.12/survey-data/1546580364630/1.Introduction.csv]], input=[fa1e9a60603145e3a1ec67d513c594cb,as,1,4,4,New Salmamouth,Chauncey Skyway,53566,27.216799:75.598685,Aglae Rice,580242662,2,12/2/2001 10:01]

然后将EXIT_STATUS设置为“ SKIPPED”或类似的名称。有可能吗?

PS 。我是Spring Batch的新手。

1 个答案:

答案 0 :(得分:0)

是的,您可以将Throwable方法的onSkipInRead参数强制转换为FlatFileParseException,并使用该异常来获取已跳过的原始行及其编号。

现在,为了将ExitStatus更改为SKIPPED,您需要在步骤(@AfterStep)之后添加回调方法,如果跳过了某些行,则需要设置退出状态,例如喜欢:

@AfterStep
public ExitStatus checkForSkips(StepExecution stepExecution) {
    if (stepExecution.getSkipCount() > 0) {
        return new ExitStatus("SKIPPED");
    }
    else {
        return null;
    }
}

您可以在SkipCheckingListener中找到示例。