简体   繁体   中英

How to terminate step from a Spring batch job

In a processor that checks if a record is created before 90 days ago, I want to terminate the step, not the job, if the reader has read an outdated record.

I tried stepExecution.setStatus(), stepExecution.setEndTime(new Date()), and so on.

Can anyone suggest a direct and explicit way to terminate step? Thanks.

As far as I know a 'direct' way to stop a step doesn't exists.
To terminate a step you have to return null from ItemReader.read() so when you process an old object you can set a flag into step-execution context and use it to:

  1. prevent other items of current chunk to be processed
  2. prevent chunk processed item writing
  3. return null from reader to stop step execution

If someone known a better way let us know! :)

This may not be ideal for you scenario but it is a possible option. In section 5.3.3 of the spring batch doc it describes adding the stop element to a step. You can set a stop condition. So in your example you could fail the job by throwing an exception. Also in that stop element definition you can define a restart step which could be the next step in your processing.

I know not the perfect solution because of the start -> stop -> start but if nothing else works it might be manageable via external scripts/apps that are handling your batch start.

I found an alternative to what I described up here as well that might work. I have been testing it out a bit on something I am working on and it seems to be a means to fail a step and allow it to continue on to something else.

<step id="firststep" parent="fs" next="failDecision" />
<decision id="failDecision" decider="decider">
    <next on="FAILED" to="secondstep" />
    <next on="COMPLETED" to="thirdstep" />
</decision>
<step id="secondstep" parent="fs"/>
<step id="thirdstep" parent="fs"/>

In this case I am failing on the first step when I meet the criteria for that and it is going to the secondstep. When it finish successfully it is going to the thirdstep although I imagine you could forgo the thirdstep if you needed to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM