0

I am new to Spring Batch and I am trying to run a linux sort command after the batch process using SystemCommandTasklet as a second step. However, its throwing NullPointerException when sorting bigger files (which takes some time, around 250 MB). It looks like SystemCommandTasklet is unable to initialize StepExecution in beforeStep() and throwing an error. Can someone check my configuration and let me know if I am missing some configuration which is causing this?

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;
}

Here is the link to SpringBatch source code for SystemCommandTasklet, its throwing NullPointerException at line 131.
https://github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/SystemCommandTasklet.java

Squashman
  • 13,649
  • 5
  • 27
  • 36
sj_24
  • 41
  • 2
  • 8

1 Answers1

2

You aren't registering the SystemCommandTasklet as a StepExecutionListener and since you aren't returning the implementing class on the @Bean method, Spring Batch doesn't know that the tasklet implements that interface. I'd recommend two things to be safe:

  1. Change the tasklet's configuration method signature to be: @Bean @StepScope public SystemCommandTasklet sortingTasklet() throws Exception {

  2. Register the tasklet as a listener on your step as well, similar to how you're doing it with the CustomStepExecutionListener.

Michael Minella
  • 20,843
  • 4
  • 55
  • 67