gpt4 book ai didi

java - 将变量传递给进程

转载 作者:行者123 更新时间:2023-11-30 02:03:39 26 4
gpt4 key购买 nike

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

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

这可能吗?

最佳答案

是的。我认为这是一个common pattern通过 StepExecution 移交信息。在这种情况下,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;
}
}

关于java - 将变量传递给进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51974505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com