Spring batch 如何将上一步数据传递给分区器

Spring batch 如何将上一步数据传递给分区器,spring-batch,Spring Batch,我试图运行面向分区的作业,但在访问stepExecutionContext存储的数据时遇到问题。这是我的工作定义 <batch:job id="job1" restartable="false" incrementer="idIncrementer"> <batch:step id="readwritestep" next="partitionStep"> <batch:tasklet transaction-manager="transac

我试图运行面向分区的作业,但在访问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中访问。但是我实现的自定义partitioner没有任何对父步骤的引用,我可以在其中访问存储的数据…即使partitioner绑定到步骤,但它无法访问父步骤数据…我是否缺少某些内容?partitioner提供的一个选项是jdbctemplate来生成拆分器上下文,我不在intrest中。我尝试注入@beforestep注释来访问上下文数据,但它没有被调用。我不想执行JDBC读取来生成从属数据…我想获取存储在步骤/作业上下文执行中的列表数据并生成拆分器上下文…有人能帮我指出正确的方向吗我可以访问这些数据

这是分区类

     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");

     }
  }
公共类ProductDetailsPartitioner实现分区器{
私人名单PRD;
@凌驾
公共地图分区(int gridSize){
List referenceds=new ArrayList();
适用于(产品珠三角:珠三角){
add(prd.getReferenceId());
}
映射结果=新建LinkedHashMap();
for(字符串referecid:referecids){
ExecutionContext=新的ExecutionContext();
put(“referenceId”,referenceId);
结果.put(“划分”+引用,上下文);
}
返回结果;
}
@先于
公共无效检索InterstepData(StepExecution StepExecution){
System.out.println(“在步进部分之前输入”);
JobExecution JobExecution=stepExecution.getJobExecution();
ExecutionContext jobContext=jobExecution.getExecutionContext();
System.out.println(“ExecutionContext”+jobContext);
this.prds=(List)jobContext.get(“产品”);
}
}

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

作为“分区器”的同一个单例“SpringBean”也可以是一个“侦听器”,以获取任何上下文键来决定分区算法stepExecution.getJobExecution().getExecutionContext().get(“键”)