Spring Batch 作业未结束

Ama*_*Dev 5 java spring spring-batch spring-boot

我刚刚开始使用 Spring 批处理并陷入了这个问题。我的工作永无止境,无限循环。以下是代码:-

@SpringBootApplication
@EnableBatchProcessing
public class Main implements CommandLineRunner{

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private JobLauncher jobLauncher;

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
    @Override
    public void run(String... args) throws Exception {
        jobLauncher.run(flattenPersonJob(),new JobParameters());
        System.out.println("Done");

    }

    @Bean
    public ItemReader itemReader() {
        return new PersonReader();
    }

    @Bean
    public ItemProcessor itemProcessor() {
        return new PersonProcessor();
    }

    @Bean
    public ItemWriter itemWriter() {
        return new PersonWriter();
    }

    @Bean
    public Step flattenPersonStep() {
        return stepBuilderFactory.get("flattenPersonStep").
                chunk(1).
                reader(itemReader()).
                processor(itemProcessor()).
                writer(itemWriter()).
                build();
    }

    @Bean
    public JobListener jobListener() {
        return new JobListener();
    }

    @Bean
    public Job flattenPersonJob() {
        return jobBuilderFactory.get("flattenPersonJob").
                incrementer(new RunIdIncrementer()).
                listener(jobListener()).
                flow(flattenPersonStep()).
                end().
                build();
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的读者班

public class PersonReader implements ItemReader<List<Person>> {

    @Override
    public List<Person> read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        System.out.println("This is the reader");
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("","",""));
        Thread.sleep(5000);
        return personList;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的作家课

public class PersonWriter implements ItemWriter<List<String>> {
@Override
public void write(List<? extends List<String>> items) throws Exception {
    System.out.println("This is the writer");
    //Thread.sleep(5000);
    items.forEach(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)

}

这是我的处理器类

public class PersonProcessor implements ItemProcessor<List<Person>, List<String>> {
@Override
public List<String> process(List<Person> item) throws Exception {
    System.out.println("This is the processor");
    //Thread.sleep(5000);
    return item.stream().map(n -> n.getName()).collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)

}

有没有我在这里缺少的配置?还是我的代码有问题?

我已经用谷歌搜索了一段时间,但找不到任何建设性的东西。

非常感谢这里的任何帮助。

谢谢,

阿马尔

Mic*_*lla 8

你的阅读器永远不会返回 null。Spring Batch 内部的约定ItemReader是读取,直到读取器返回 null(表明输入已耗尽)。因为你永远不会从你的ItemReader...返回 null,你的工作将永远读取。