gpt4 book ai didi

spring-batch - 如何将上一步数据传递给分区程序

转载 作者:行者123 更新时间:2023-12-05 08:01:17 29 4
gpt4 key购买 nike

我正在尝试运行面向分区的作业,但在访问 stepExecutionContext 存储的数据时遇到了问题。这是我的工作定义

<batch:job id="job1" restartable="false" incrementer="idIncrementer">
<batch:step id="readwritestep" next="partitionStep">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="reader1"
writer="writer1"
commit-interval="500"/>
</batch:tasklet>
<batch:listeners>
<batch:listener ref="promotionListener"/>
</batch:listeners>
</batch:step>
<batch:step id="partitionStep" >
<batch:partition step="detailsStep" partitioner="partitioner">
<batch:handler grid-size="10" task-executor="taskExecutor" />
</batch:partition>
</batch:step>
</batch:job>
<batch:step id="detailsStep">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="reader2"
processor="processor"
writer="writer2"
commit-interval="1500"/>
</batch:tasklet>
</batch:step>

在处理 readwritestep 时,我将一些数据存储在步骤上下文中并提升到作业上下文,以便我可以在 partioner 中访问。但是我已经实现的自定义分区程序没有对父步骤的任何引用,我可以在父步骤中访问存储的数据......即使分区程序绑定(bind)到 STEP 它也无法访问父步骤数据......我在这里遗漏了什么吗?分区程序提供的一个选项是 jdbctemplate 以生成我不感兴趣的拆分器上下文。我试图注入(inject) @beforestep 注释来访问上下文数据,但它没有被调用。我不想执行 JDBC 读取来生成从属数据......我想获取存储在步骤/中的 LIST 数据作业上下文执行并生成拆分器上下文...有人可以帮我指出正确的方向,以便我可以访问该数据...

这是分区类...

     public class ProductDetailsPartitioner implements Partitioner {

private List<Product> prds;


@Override
public Map<String, ExecutionContext> partition(int gridSize) {
List<String> referencIds = new ArrayList<String>();
for (Product prd : prds) {
referencIds.add(prd.getReferenceId());
}
Map<String, ExecutionContext> results = new LinkedHashMap<String,ExecutionContext>();
for (String referencId : referencIds) {
ExecutionContext context = new ExecutionContext();
context.put("referenceId", referencId);
results.put("partition." + referencId, context);
}
return results;
}

@BeforeStep
public void retrieveInterstepData(StepExecution stepExecution) {
System.out.println("Entered Before step in partion");
JobExecution jobExecution = stepExecution.getJobExecution();
ExecutionContext jobContext = jobExecution.getExecutionContext();
System.out.println("ExecutionContext"+jobContext);
this.prds = (List<Product>) jobContext.get("products");

}
}

最佳答案

好的,我可以通过另一种方法解决这个问题。现在我正在处理基于 jobid 的 partioner,而不是通过执行上下文传递引用。它运行良好。很难解释真正的解决方案,因为它基于真正的业务需求。

关于spring-batch - 如何将上一步数据传递给分区程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14760744/

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