问题
In Spring Batch I have a custom ItemProcessor. I want to pass information from the afterStep to the process method. Here's the code
@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;
}
}
I want to access writeCount and clfCount from after step in process. Like:
@Override
public Clf010Item process(Clf010Item item) throws Exception {
batch.print("writecount = ", stepExecution.getWriteCount());
return item;
}
Is this possible?
回答1:
Yes. I think it is a common pattern to hand over infos via the StepExecution. In that case the StepExecution is held as a member variable in of the ItemProcessor and can be set via the beforeStep method. E.g:
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;
}
}
回答2:
I want to pass information from the afterStep to the process method
The method annotated with AfterStep will be executed after the entire step is finished (including reading, processing and writing), but the process method is executed while the step is running. So the information you are requesting in process (the writeCount) is not available yet at that moment.
回答3:
In response to Mahmoud Ben Hassine's answer, here is my code:
@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;
}
}
来源:https://stackoverflow.com/questions/51974505/passing-variables-to-process