Spring -Batch: how can I return Job STATUS user exit from StepListener to solve the next step

The problem is this: I have a Spring batch job with several steps. Based on the first step, I have to decide the next steps. Can I set the status in STEP1-passTasklet based on the job parameter so that I can set the exit status to user status and define it in the job definition file to go to the next step.

Example <job id="conditionalStepLogicJob"> <step id="step1"> <tasklet ref="passTasklet"/> <next on="BABY" to="step2a"/> <stop on="KID" to="step2b"/> <next on="*" to="step3"/> </step> <step id="step2b"> <tasklet ref="kidTasklet"/> </step> <step id="step2a"> <tasklet ref="babyTasklet"/> </step> <step id="step3"> <tasklet ref="babykidTasklet"/> </step> </job> 

Ideally, I want my own EXIT STATUS to be used between steps. I can do it? will the OOTB flow break? is it really done

+4
source share
1 answer

These are several ways to do this.

You can use StepExecutionListener and override the afterStep method:

 @AfterStep public ExitStatus afterStep(){ //Test condition return new ExistStatus("CUSTOM EXIT STATUS"); } 

Or use JobExecutionDecider to select the next step based on the result.

 public class CustomDecider implements JobExecutionDecider { public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) { if (/* your conditon */) { return new FlowExecutionStatus("OK"); } return new FlowExecutionStatus("OTHER CODE HERE"); } } 

Xml config:

  <decision id="decider" decider="decider"> <next on="OK" to="step1" /> <next on="OHTER CODE HERE" to="step2" /> </decision> <bean id="decider" class="com.xxx.CustomDecider"/> 
+8
source

Source: https://habr.com/ru/post/1469263/


All Articles