Rakesh Gurung
Rakesh Gurung

Reputation: 95

Spring Batch - Save "onSkipInRead" throwable message in EXIT_MESSAGE of batch_step_execution table

I want which records were skipped during reading and insert that in "EXIT_MESSAGE" of batch_step_execution. So, this is my "SkipListener" class.

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());

}

I want the following throwable message to be saved in the table.

    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]

And make the EXIT_STATUS as "SKIPPED" or something like that. Is it possible ?

P.S. I am new in Spring Batch.

Upvotes: 0

Views: 956

Answers (1)

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31640

Yes, you can cast the Throwable parameter of your onSkipInRead method to FlatFileParseException and use that exception to get the raw line that was skipped and its number.

Now in order to change the ExitStatus to SKIPPED, you need to add a callback method after the step (@AfterStep) and set the exit status if some lines were skipped, something like:

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

You can find an example in the SkipCheckingListener.

Upvotes: 2

Related Questions