How to loop a step until a condition in execution context is met?

  Kiến thức lập trình

My usecase is to fetch records from a sharded database and update. i have startstep that sets the counter and increment the loop and not sure how and where to. specify the loop and then decide if the criteria is met or not

Below approach only runs the loop twice.

How ever i have to specify the loop to n times and where can i specify the loop to run the flow 1 until the condition is met? process() has reader, processor and writer.

  @Bean
 public Job updateJob() throws Exception {
   Flow flow1 = new FlowBuilder<SimpleFlow>("flow1")
                .start(process())
                .next(incrementStep())
                .build();


 return jobBuilderFactory.get("job")
                .start(startStep())
                .next(process())
                .next(incrementStep())
                .next(decider())
                .on(String.valueOf(ExitStatus.EXECUTING.toString())).to(flow1)
                .on(String.valueOf(ExitStatus.COMPLETED.toString())).end().
                end().build();

}

@Bean
    public StepDecider decider(){
        return new StepDecider();
    }



    public Step startStep() throws Exception {
        return stepBuilderFactory.get("startStep").tasklet(new Tasklet() {
            @Override public RepeatStatus execute(final StepContribution stepContribution,
                    final ChunkContext chunkContext)
                    throws Exception {
                stepContribution.getStepExecution().getJobExecution().getExecutionContext().put("dbkey",1);
          
                return RepeatStatus.FINISHED;
            }
        }).build();
    }

    public Step incrementStep() throws Exception {
        return stepBuilderFactory.get("incrementStep").tasklet(new Tasklet() {
                @Override public RepeatStatus execute(final StepContribution stepContribution,
                    final ChunkContext chunkContext)
                    throws Exception {
                System.out.println("increment Step");
                Object shardKey = stepContribution.getStepExecution().getJobExecution().getExecutionContext().get(
                        "dbkey");
                dbkey = (int) dbkey + 1;
                stepContribution.getStepExecution().getJobExecution().getExecutionContext().put("dbkey", dbkey);

                return RepeatStatus.FINISHED;
            }

        }).build();
    }


@Component
@Scope("job")
@Slf4j
public class StepDecider implements JobExecutionDecider {
    @Override
    public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {

        Integer currentKey= (Integer)    stepExecution.getJobExecution().getExecutionContext().get("dbkey");

        System.out.println("Decider result is:  " + currentKey);

        if(currentKey > 5 ){
         return new FlowExecutionStatus(ExitStatus.COMPLETED.toString());
        }
        return new FlowExecutionStatus(ExitStatus.EXECUTING.toString());
    }

}

 @Bean
 public Job updateJob() throws Exception {
   Flow flow1 = new FlowBuilder<SimpleFlow>("flow1")
                .start(process())
                .next(incrementStep())
                .build();


 return jobBuilderFactory.get("job")
                .start(startStep())
                .next(process())
                .next(incrementStep())
                .next(decider())
                .on(String.valueOf(ExitStatus.EXECUTING.toString())).to(flow1)
                .on(String.valueOf(ExitStatus.COMPLETED.toString())).end().
                end().build();

}

New contributor

jyo rani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT