在Spring Batch

时间:2015-09-21 13:37:59

标签: java spring

我正在尝试使用stepExecution来保留Spring批处理中的数据。但是当我尝试在代码执行中使用它时,我遇到了一些问题:

ExecutionContext stepContext = this.stepExecution.getExecutionContext();
当我厌倦了这个时,我没有认识到stepExecution。我导入了org.springframework.batch.core.StepExecution并尝试了:

ExecutionContext context = StepExecution.getJobExecution().getExecutionContext();

这里确认了StepExecution但是我得到了一个"无法对非静态方法进行静态引用"错误。我的项目没有正确设置;我在这做错了什么?

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

}

2 个答案:

答案 0 :(得分:2)

您可以使用BeforeStep注释从步骤范围的bean中获取stepExecution:

@BeforeStep
public void setStepExecution(StepExecution stepExecution) {
    this.stepExcecution = stepExecution;
}

答案 1 :(得分:0)

面向块的tasklet可以直接从ChunkContext访问StepExecution:

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    StepExecution stepExecution = chunkContext.getStepContext().getStepExecution();
    ...
}