将变量传递给过程

时间:2018-08-22 20:18:35

标签: java spring spring-batch

在Spring Batch中,我有一个自定义的ItemProcessor。我想将信息从afterStep传递给处理方法。这是代码

@Component
public class Clf010Processor implements ItemProcessor<Clf010Item, Clf010Item> {

    private StepExecution stepExecution;

    BatchProgram batch = new BatchProgram();

    @AfterStep
    public void afterStep(StepExecution stepExecution) {
        ExecutionContext je = stepExecution.getJobExecution().getExecutionContext();
        // Pass the counts up to the execution context.
        je.putLong("writeCount", stepExecution.getWriteCount());
        je.putLong("clfCount", stepExecution.getWriteCount());
    }

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

    @Override
    public Clf010Item process(Clf010Item item) throws Exception {
        batch.print("writeCount = ", stepExecution.getWriteCount());
        return item;
    }

}

我想从处理步骤之后访问writeCount和clfCount。喜欢:

@Override
public Clf010Item process(Clf010Item item) throws Exception {
    batch.print("writecount = ", stepExecution.getWriteCount());
    return item;
}

这可能吗?

3 个答案:

答案 0 :(得分:1)

是的。我认为通过StepExecution移交信息是common pattern。在这种情况下,StepExecution作为ItemProcessor中的成员变量保存,可以通过beforeStep方法进行设置。例如:

public class Clf010Processor implements ItemProcessor<Clf010Item, Clf010Item> {

    private StepExecution stepExecution;

    @AfterStep
    public void afterStep(StepExecution stepExecution) {
        ExecutionContext je = stepExecution.getJobExecution().getExecutionContext();
        // Pass the counts up to the execution context.
        je.putLong("writeCount", stepExecution.getWriteCount());
        je.putLong("clfCount", stepExecution.getWriteCount());
    }

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

    @Override
    public Clf010Item process(Clf010Item item) throws Exception {
        JobExecution jobExecution = this.stepExecution.getJobExecution();
        ExecutionContext jobContext = jobExecution.getExecutionContext();
        long writeCount = jobContext.getLong("writeCount");
        batch.print("writecount = ", writeCount);
        return item;
    }
}

答案 1 :(得分:1)

  

我想将信息从afterStep传递给处理方法

注释为AfterStep的方法将在整个步骤完成后执行(包括读取,处理和写入),但是process方法将被执行运行时。因此,您目前在processwriteCount)中请求的信息尚不可用。

答案 2 :(得分:0)

为回应Mahmoud Ben Hassine的回答,这是我的代码:

@Component
public class Clf010Processor implements ItemProcessor<Clf010Item, Clf010Item> {

    BatchProgram batch = new BatchProgram();

    @AfterStep
    public void afterStep(StepExecution stepExecution) {
        ExecutionContext je = stepExecution.getJobExecution().getExecutionContext();
        // Pass the counts up to the execution context.
        je.putLong("writeCount", stepExecution.getWriteCount());
        je.putLong("clfCount", stepExecution.getWriteCount());
        je.putLong("readCount", stepExecution.getReadCount());
        // Log the program results
        batch.print("");
        batch.print("**********************************************************");
        batch.print("INPUT RECORDS READ =  " + stepExecution.getReadCount());
        batch.print("**********************************************************");
        batch.print("");
        batch.print("**********************************************************");
        batch.print("OUTPUT RECORDS WRITTEN =  " + stepExecution.getWriteCount());
        batch.print("**********************************************************");
        batch.print("");
    }

    @BeforeStep
    public void beforeStep(StepExecution stepExecution) {}

    @Override
    public Clf010Item process(Clf010Item item) throws Exception {
        return item;
    }

}