Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using stepExecution in Spring Batch

Tags:

java

spring

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;
}

}

like image 452
user2665166 Avatar asked Oct 27 '25 20:10

user2665166


1 Answers

You can use BeforeStep annotation to get stepExecution from your step-scoped bean:

@BeforeStep
public void setStepExecution(StepExecution stepExecution) {
    this.stepExcecution = stepExecution;
}
like image 164
Egor Lyashenko Avatar answered Oct 30 '25 10:10

Egor Lyashenko