Spring Batch-@ BeforeStep没有在Partitioner中被调用

Dob*_*bby 6 spring spring-batch

我们正在尝试使用Spring批处理分区来实现批处理作业。在“步骤2”中,这是一个分区的步骤,在该步骤中,我需要来自步骤1的一些数据进行处理。这个数据。

我试图在partitioner类中使用@BeforeStep批注来获取stepExecutionContext,从中可以提取先前存储的数据并将其放入分区器的ExecutionContext中。但是带有@BeforeStep批注的方法未在分区器中调用。

还有其他方法可以实现这一目标。

分区器实施

public class NtfnPartitioner implements Partitioner {

    private int index = 0;
    String prev_job_time  = null;
    String curr_job_time  = null;

    private StepExecution stepExecution ;
    ExecutionContext executionContext ;

    @Override
    public Map<String, ExecutionContext> partition(int gridSize)
    {

           System.out.println("Entered Partitioner");
           List<Integer> referencIds = new ArrayList<Integer>();
           for (int i = 0; i < gridSize;i++) {
            referencIds.add(index++);
           }
           Map<String, ExecutionContext> results = new LinkedHashMap<String,ExecutionContext>();
          for (int referencId : referencIds) {
            ExecutionContext context = new ExecutionContext();
            context.put("referenceId", referencId);
            context.put(NtfnConstants.PREVIOUS_JOB_TIME, prev_job_time);
            context.put(NtfnConstants.JOB_START_TIME, curr_job_time);
            results.put("partition." + referencId, context);
          }
            return results;
        }

    @BeforeStep
    public void beforeStep(StepExecution stepExecution) {
        // TODO Auto-generated method stub
          System.out.println("Entered Before step in partion");
          JobExecution jobExecution = stepExecution.getJobExecution();
          ExecutionContext jobContext = jobExecution.getExecutionContext();
          System.out.println("ExecutionContext"+jobContext);
          String prev_job_time  = (String) jobContext.get(NtfnConstants.PREVIOUS_JOB_TIME);
          String curr_job_time  = (String) jobContext.get(NtfnConstants.JOB_START_TIME);


    }
Run Code Online (Sandbox Code Playgroud)

小智 1

bean 应该是步骤范围的。

Java,注释类:

@StepScope
Run Code Online (Sandbox Code Playgroud)

XML,在 bean 定义中:

scope="step"
Run Code Online (Sandbox Code Playgroud)

另请查看有关代理 bean 的答案(不确定这是否适用于您,因为除了分区程序之外没有提供其他代码)。在这种情况下,您仍然可以在步骤构建期间显式添加分区程序作为侦听器:

@Autowired
private NtfnPartitioner partitioner;    

...

final Step masterStep = stepBuilderFactory.get("master")
                    .listener(partitioner)
                    .partitioner("slave", partitioner)
                    .step(slave)
                    ...
Run Code Online (Sandbox Code Playgroud)

或者,如果您的分区器不是 bean(例如,您基于动态内容创建它),您仍然可以将其添加为侦听器:

final NtfnPartitioner partitioner = new NtfnPartitioner();    
final Step masterStep = stepBuilderFactory.get("master")
                    .listener(partitioner)
                    .partitioner("slave", partitioner)
                    .step(slave)
                    ...
Run Code Online (Sandbox Code Playgroud)