简体   繁体   中英

How do I get the ID of the previous step execution in the next step of a spring batch job?

How do I get the ID of the previous step execution in the next step of a spring batch job?

I am currently trying to perform several steps on a single set of data and persisting them in different rows in a database.

Each step (except the first one) will be reading the previous steps results and doing further processing on it and then saving the updated results in a new row.

I have a custom reader which reads from the database but I need to pass the ID of the previous step execution to the current step so that it can query the repository accordingly.

How can I go about doing this?

Please follow following steps below :

1.From current Step, pass data to StepExecutionContext .

2.From Current StepExecutionContext , pass data to JobExecutionContext (through a listener )

3. JobExecutionContext is shared across all Steps and in this way you can now get data of previous Steps .

Alternative approach will be to store this temp Step data in a table in DB with columns as JobExecutionId,StepExecutionId,StepData

An option is to use a StepExecutionListener that will be called before and after the step is executed. The method afterStep has the StepExecution object. You can use this StepExecution to set all the parameters you need on the next step.

Ex.:

currentStep.registerStepExecutionListener(new StepExecutionListener() {
        @Override
        public void beforeStep(StepExecution stepExecution) {}

        @Override
        public ExitStatus afterStep(StepExecution stepExecution) {
            nextStep.setParameter(stepExecution.getExecutionContext().getString(param)
            return ExitStatus.COMPLETED;
        }
    });

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