2017-02-09 85 views
0

我是Spring Batch的新手,我嘗試在使用SystemCommandTasklet作爲第二步的批處理後執行linux sort命令。但是,當排序更大的文件時(這需要一些時間,大約250 MB),它拋出NullPointerException異常。它看起來像SystemCommandTasklet無法在beforeStep()中初始化StepExecution並引發錯誤。有人可以檢查我的配置,並讓我知道如果我錯過了一些導致這種情況的配置?Spring批處理SystemCommandTasklet拋出空指針異常

BatchConfig.java

@Bean 
public Job job() throws Exception { 
    return jobs.get("job") 
      .incrementer(new RunIdIncrementer()) 
      .flow(step1()).on("FAILED").fail().on("COMPLETED").to(step2()) 
      .end() 
      .build(); 
} 

@Bean 
public Step step1() { 
    return steps.get("step1") 
      .<FileEntry,FileEntry>chunk(100) 
      .reader(reader()).faultTolerant().skipLimit(MAX_SKIP_LIMIT).skip(FlatFileParseException.class) 
      .processor(new Processor()) 
      .writer(compositeWriter()).stream(outputwriter()).stream(rejectwriter()) 
      .listener(new CustomStepExecutionListener()) 
      .build(); 
} 

@Bean 
public Step step2() throws Exception { 
    return steps.get("step2") 
      .tasklet(sortingTasklet()) 
      .build(); 
} 

@Bean 
@StepScope 
public Tasklet sortingTasklet() throws Exception { 
    SystemCommandTasklet tasklet = new SystemCommandTasklet(); 
    logger.debug("Sorting File : " + getOutputFileName()); 
    tasklet.setCommand(new String("sort " + getOutputFileName() + " -d -s -t \001 -k1,1 -o " + getOutputFileName() + ".sorted ")); 
    tasklet.setTimeout(600000l); 
    return tasklet; 
} 

這裏是鏈接到的SystemCommandTasklet SpringBatch源代碼,在其行拋出的NullPointerException 131
https://github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/SystemCommandTasklet.java

回答

0

您還沒有註冊SystemCommandTasklet作爲StepExecutionListener和由於您沒有返回@Bean方法的實現類,因此Spring Batch不知道該tasklet實現了該接口。我建議兩件事情是安全的:

  1. 更改微進程的配置方法的簽名是: @Bean @StepScope public SystemCommandTasklet sortingTasklet() throws Exception {

  2. 註冊微進程爲您的步監聽器以及類似於你怎麼」重新用CustomStepExecutionListener來做。

+1

非常感謝您的回覆。它現在完美。另外,我是Spring Batch的粉絲:) –

+0

@Mihael Minella,你會在哪一步註冊tasklet作爲監聽器? – mfunaro

+0

它使用的步驟。 –