【问题标题】:Spring batch SystemCommandTasklet throwing null pointer exceptionSpring批处理SystemCommandTasklet抛出空指针异常
【发布时间】:2017-06-27 18:47:30
【问题描述】:

我是 Spring Batch 的新手,我正在尝试在批处理之后使用 SystemCommandTasklet 作为第二步运行 linux 排序命令。但是,在对较大的文件进行排序时会抛出 NullPointerException(这需要一些时间,大约 250 MB)。看起来 SystemCommandTasklet 无法在 beforeStep() 中初始化 StepExecution 并引发错误。有人可以检查我的配置并让我知道我是否缺少某些导致此问题的配置吗?

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 源代码的链接,它在第 131 行抛出 NullPointerException。
https://github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/SystemCommandTasklet.java

【问题讨论】:

    标签: spring spring-boot spring-batch


    【解决方案1】:

    您没有将SystemCommandTasklet 注册为StepExecutionListener,并且由于您没有在@Bean 方法上返回实现类,因此Spring Batch 不知道tasklet 实现了该接口。为了安全起见,我建议做两件事:

    1. 将 tasklet 的配置方法签名更改为: @Bean @StepScope public SystemCommandTasklet sortingTasklet() throws Exception {

    2. 在您的步骤中也将 tasklet 注册为侦听器,类似于您使用 CustomStepExecutionListener 执行此操作的方式。

    【讨论】:

    • 非常感谢您的回复。它现在完美运行。另外,我是 Spring Batch 的忠实粉丝 :)
    • @Mihael Minella,您会将 tasklet 注册为侦听器的哪一步?
    • 使用的步骤。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2020-08-10
    • 2013-03-21
    • 1970-01-01
    相关资源
    最近更新 更多