I am trying to use stepExecution to retain data in a Spring batch. However I am running into some issues when I attempt to use it in my code execution:
ExecutionContext stepContext = this.stepExecution.getExecutionContext();
stepExecution was not recognized when I tired this. I imported org.springframework.batch.core.StepExecution and also tried:
ExecutionContext context = StepExecution.getJobExecution().getExecutionContext();
Here StepExecution was recognized but I get a "Cannot make static reference to non-static method" error. Is my project not set up right; what am I doing wrong here?
import TextFileReader;
import OracleService;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.batch.core.StepExecution;
public class myTask implements Tasklet {
private List<String> sourceQueries;
private List<String> targetQueries;
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    ExecutionContext context = StepExecution.getJobExecution().getExecutionContext();
    ExecutionContext stepContext = this.stepExecution.getExecutionContext();
    stepContext.put("theListKey", sourceQueries);
    return RepeatStatus.FINISHED;
}
public List<String> getSourceQueries() {
    return sourceQueries;
}
public void setSourceQueries(List<String> sourceQueries) {
    this.sourceQueries = sourceQueries;
}
public List<String> getTargetQueries() {
    return targetQueries;
}
public void setTargetQueries(List<String> targetQueries) {
    this.targetQueries = targetQueries;
}
}
You can use BeforeStep annotation to get stepExecution from your step-scoped bean:
@BeforeStep
public void setStepExecution(StepExecution stepExecution) {
    this.stepExcecution = stepExecution;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With