Spring Batch - @ BeforeStep未在分区程序中调用

时间:2014-02-06 08:29:40

标签: spring spring-batch

我们正在尝试使用spring批处理来实现批处理作业。在“步骤2”中这是一个分区步骤,我需要从步骤1处理一些数据进行处理。我使用了StepExecutionContext,它将被提升为作业执行上下文step1存储此数据。

我尝试在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);


    }

2 个答案:

答案 0 :(得分:0)

bean应该是步骤范围的。

Java,注释类:

@StepScope

XML,在bean定义中:

scope="step"

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

@Autowired
private NtfnPartitioner partitioner;    

...

final Step masterStep = stepBuilderFactory.get("master")
                    .listener(partitioner)
                    .partitioner("slave", partitioner)
                    .step(slave)
                    ...

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

final NtfnPartitioner partitioner = new NtfnPartitioner();    
final Step masterStep = stepBuilderFactory.get("master")
                    .listener(partitioner)
                    .partitioner("slave", partitioner)
                    .step(slave)
                    ...

答案 1 :(得分:0)

要获取Job Parameters的句柄,可以对NtfnPartitioner类实现StepExecutionListener,以使用重写的beforeStep和afterStep方法。请确保它应为StepScoped。

public class NtfnPartitioner implements Partitioner, StepExecutionListener {

   String prev_job_time  = null;
   String curr_job_time  = null;
   ....

   @Override
   public Map<String, ExecutionContext> partition(int gridSize)
   {
       ....
       /* Please use prev_job_time and curr_job_time in this 
          method which was fetched from context */
       ....
   }

   @BeforeStep
   public void beforeStep(StepExecution stepExecution) {
         System.out.println("Entered Before step in partion");
         ExecutionContext jobContext = stepExecution.getJobExecution().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);

   }

   @Override
   public ExitStatus afterStep(StepExecution stepExecution) {
       if (stepExecution.getStatus() == BatchStatus.COMPLETED) {
           return ExitStatus.COMPLETED;
       }
       return ExitStatus.FAILED;
   }

}