在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;
}
这可能吗?
最佳答案
是。我认为通过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;
}
}